【问题标题】:How to mark item in listView readable while scrolling?滚动时如何将listView中的项目标记为可读?
【发布时间】:2013-04-30 14:49:59
【问题描述】:

我正在构建我的第一个应用程序。它只是通过短信 (SMS) 接收到的服务器警报列表。只有一个列表视图操作(点击没有详细信息)。我希望能够通过阅读来标记已阅读的项目。当我打开我的应用程序时,我看到消息从旧到新排序,并且我的 listView 位置设置在 listView 的底部。当我向上滚动时,显示的项目我想标记为已读。

我是这样做的:

public class AlertsListFragment extends ListFragment {
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // ...
        Cursor c = context.getContentResolver().query(uri, AlertsContract.PROJECTION, selection, selectionArgs, null);
        adapter = new AlertAdapter(context, R.layout.row, c, from, to, 0);
        setListAdapter(adapter);
        ListView listView = getListView();
        listView.setSelected(listView.getCount());

        lv.setOnScrollListener(new AbsListView.OnScrollListener() {
            public void onScrollStateChanged(AbsListView view, int scrollState) { }

            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                for (int i = firstVisibleItem; i < firstVisibleItem + visibleItemCount; i++) {
                    Cursor cursor = (Cursor)view.getItemAtPosition(i);
                    long id = cursor.getLong(cursor.getColumnIndex(AlertsContract._ID));
                    String type = cursor.getString(cursor.getColumnIndex(AlertsContract.TYPE));
                    Log.d("VIEWED", "This is viewed "+ type + " id: " + id);

                    // here I can get the id and mark the item read
                }
            }
        });
    }
}

但我认为,这不是最好的解决方案。一个问题是,如果用户不打算滚动列表,即使用户可以从列表中看到至少 3 到 4 个项目,也不会将任何项目标记为已查看。

listView 项是否有任何事件,例如“显示”或类似的东西?每次listView项到达listView的可见区域时都会发生的一些事件?

【问题讨论】:

    标签: android listview


    【解决方案1】:

    也许您应该尝试使用适配器的getView 方法。 (Reference)

    每当您的ListView 尝试显示项目时都会调用它。

    但要小心,当项目没有完全显示时也会调用它,所以你应该再次检查这个,也许你的 onScroll 方法中的解决方案。

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 2010-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-06
      • 1970-01-01
      相关资源
      最近更新 更多