List View Scroll Not Smooth

扶醉桌前 提交于 2020-01-21 11:48:21

问题


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" 
           />

回答1:


Use an AsyncTask for the loading of your pictures. The scrolling will not be smooth as long as you do such tasks in the UI thread.

Have a look at this tutorial. It will help you understand what you need to implement without the need of additional libraries. Also please keep in mind that the rows are redrawn all the time while you scroll, there's no real "finishing" of loading. You can additionally consider an image-cache, e.g. with a ConcurrentHashMap in which you could put your loaded pictures.




回答2:


For Image Load quickly use this lib it is very faster to load images https://github.com/bumptech/glide

For List view Scroll smooth use strictMode .IT write in activity onCreate() method

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....
    ------*/}

on more thing to add in to onCreate() method

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();
        }
} });

add into Adapter class

public static Boolean isScrolling = true;



回答3:


There can be quite a few reasons for not so smooth scrolling of ListView. You can make sure you are using some of the best practices like

  1. Use Holder pattern and re-using the views in the getView() of list adapter
  2. Use some async image loading library for downloading images that you display in the ListView? For e.g. Universal Image Loader
  3. Use AsyncTask to download JSON from network and all the network related processing is done in separate thread.

Once you make sure you have covered the above, it should work good.



来源:https://stackoverflow.com/questions/20054465/list-view-scroll-not-smooth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!