【问题标题】:How to start from marked color in listview?如何从列表视图中的标记颜色开始?
【发布时间】:2019-01-03 01:09:36
【问题描述】:

我有列表视图,例如:黄色标记在第 110 位。如何从第 110 个位置显示为列表视图中的起始位置。因为用户需要滚动才能看到第 110 位的黄色标记。

这里是代码。

public class TableOfContentsListAdapter extends BaseAdapter {


    @Override
    public final View getView(final int position, View convertView, final ViewGroup parent) {

  int type = getItemViewType(position);
        if ((convertView == null) || (((Integer) convertView.getTag()) != type)) {
            convertView = createView(position);
            convertView.setTag(type);
        }
  final BaseTableOfContents node = getItem(position);
         tocView = (Node) convertView;
  ((Checkable) convertView).setChecked(okayToHighlightAnchorNode(node, position));
            convertView.setActivated(okayToHighlightTouchNodes(node, position));
        return convertView;
}
   }

 public boolean okayToHighlightTouchNodes(BaseTableOfContents candidate, int position) {
        return mTouchNodePedigree.indexOf(candidate.getNodeId()) >= position;


    }


 MainActivity: (I am clueless how to use it to display.)
//returnValue is the object of listview. 
     returnValue.getListView().setOnScrollListener(new AbsListView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(AbsListView absListView, int position) {
                    TableOfContentsListAdapter adapter = (TableOfContentsListAdapter) absListView.getAdapter();
                    Log.v("scrolling","scrolling");
                }

                @Override
                public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                    Log.v("visibleItemCount","visibleItemCount"+visibleItemCount);
                    Log.v("visibleItemCounts","visibleItemCounts"+totalItemCount);
                }
            });

此外,列表仅显示 0 到 9,滚动时显示 10 到 20,再次滚动时显示 20 到 30,依此类推。所以我不能把高亮的黄色标记显示在其他位置。

【问题讨论】:

  • 您可以使用smoothScrollToPosition() recyclerview 的方法,将recycler view 滚动到随机位置。您可以将其用作yourRecyclerView.smoothScrollToPosition(110);

标签: android listview android-adapter


【解决方案1】:

如果可以的话,我建议你使用 RecyclerView 而不是 ListView。它提供了类似这种方法的独特功能:recyclerView.scrollToPosition(110); 在这种情况下可以为您提供帮助。顺便说一句,它在使用资源方面比 ListView 更有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-17
    • 2020-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-03
    相关资源
    最近更新 更多