【问题标题】:List View Scroll Not Smooth列表视图滚动不流畅
【发布时间】:2013-11-18 17:56:17
【问题描述】:

我有一个自定义列表视图,它显示用户和照片 我从 API 中检索数据,提供 JSON 输出,

我的问题是列表视图滚动不顺畅,它会挂起一秒钟并滚动,它会重复相同的操作直到我们到达终点。

我认为可能是因为我在 UI 线程上运行与网络相关的操作,但即使在完成加载后它也会继续这样做?

我的自定义Listview的结构是

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

【问题讨论】:

标签: android android-listview android-scrollview android-scroll


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    为了快速加载图像,使用这个库加载图像非常快 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;
    

    【讨论】:

      【解决方案3】:

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

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

      一旦你确定你已经涵盖了上述内容,它应该可以正常工作。

      【讨论】:

      • 很高兴它很有用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-23
      • 1970-01-01
      • 2018-01-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多