4

i have a custom list view, which displays users, and there photos i retrieve the data from API, Which gives JSON Output,

My Issue is that the list view is not scrolling smoothly, it hangs for a sec and scrolls, it repeats the same till we reach the end.

i thought it might me because i am running network related operation on the UI thread, but it continues to do that even after it completes loading?

the structure of my custom Listview is

 <TextView  style="@style/photo_post_text"
             android:id="@+id/photo_post_text"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:text="demotext"
           />


        <ImageView
            android:id="@+id/userimage"
           android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scaleType="fitXY"

            android:adjustViewBounds="true"
           android:src="@drawable/pi" 
           />
4

3 回答 3

7

使用AsyncTask加载图片。只要您在 UI 线程中执行此类任务,滚动就不会顺畅。

看看这个教程。它将帮助您了解需要实现的内容,而无需额外的库。另外请记住,滚动时会一直重绘行,没有真正的“完成”加载。您还可以考虑使用图像缓存,例如使用ConcurrentHashMap,您可以在其中放置加载的图片。

于 2013-11-18T17:58:14.283 回答
3

对于图像加载快速使用这个库加载图像非常快 https://github.com/bumptech/glide

对于列表视图滚动平滑使用 strictMode .IT 写入活动 onCreate() 方法

protected void onCreate(Bundle savedInstanceState) {

    //StrictMode for smooth list scroll
    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    /*----- 
        ListView 
        Custom Adapter set data....
    ------*/}

在 onCreate() 方法中添加更多内容

listView.setOnScrollListener(new OnScrollListener() {
public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCoun) {
}
public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (scrollState != 0)
            listView.getAdapter()).isScrolling = true;
        else {
            adapter.isScrolling = false;
            adapter.notifyDataSetChanged();
        }
} });

添加到适配器类

public static Boolean isScrolling = true;
于 2014-03-21T12:35:25.897 回答
2

ListView 滚动不那么流畅的原因可能有很多。您可以确保使用一些最佳实践,例如

  1. 使用 Holder 模式并重用getView()列表适配器中的视图
  2. 使用一些异步图像加载库来下载您在 ListView 中显示的图像?例如通用图像加载器
  3. 用于AsyncTask从网络下载 JSON,所有与网络相关的处理都在单独的线程中完成。

一旦你确定你已经涵盖了上述内容,它应该工作得很好。

于 2013-11-18T18:06:29.410 回答