RecyclerView:在点击自定义行

时间:2016-09-05 12:21:50

标签: android android-recyclerview android-viewholder

期望的结果

维护滚动位置并在自定义行的特定视图上给出正确的位置。

我正在使用RecyclerView,我试图在列表的开头和列表末尾添加需求数据。

通过滚动底部获取数据非常有效。

但滚动排名问题如下:

  1. 在Adapter的onBindViewHolder中设置Click侦听器。
  2. 在Recyclerview的OnItemTouchListener中设置Click侦听器。
  3. 以下是这种情况:

    当我向上滚动列表然后它加载15个数据并将其添加到列表顶部。 我们的目的是保持滚动位置。 哪个可以通过

    实现
    adapter.notifyItemRangeInserted(0, stringArrayListTemp.size());
    

    问题是在点击任何项目而不滚动时,View的OnClickListener会给出错误的位置。  您可以检查适配器的方法:

        @Override
        public void onBindViewHolder(MyViewHolder holder, final int position) {
            String item = moviesList.get(position);
            holder.title.setText(item);
            holder.title.setTag(position);
            holder.title.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Log.e("onClick", "stringArrayList position: " + moviesList.get(position) + " - view position: " + moviesList.get((int) view.getTag()));
                }
            });
        }
    

    但在Recyclerview的同时,addOnItemTouchListener使用stringArrayList.get(position)给出了正确的位置,但使用getTag给出了错误的位置。

    mRecyclerView.addOnItemTouchListener(
                    new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    Log.e("onItemClick", "stringArrayList position: "+stringArrayList.get(position)+ " - view position: "+stringArrayList.get((int)view.getTag()));
                }
            }));
    

    当我尝试在列表的开头添加数据时,我得到了确切的问题。

    // retain scroll position and gives wrong position onClick
        adapter.notifyItemRangeInserted(0, stringArrayListTemp.size()); 
    
    // does not retain scroll position, scroll to top & gives right position onClick
        //adapter.notifyDataSetChanged();   
    

1 个答案:

答案 0 :(得分:0)

这段代码对我有用,所以试试这样的事情

    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
            private final Typeface font;
            public TextView mTitle, mDetail, mDate, mCategory;
            public CheckBox mCheckBox;
            public ImageButton mImageButton;
            public ImageView mImage;
            public View mview;
            private SparseBooleanArray selectedItems = new SparseBooleanArray();

            public MyViewHolder(View view) {
                super(view);
                mTitle = (TextView) view.findViewById(R.id.item_title);
                mDetail = (TextView) view.findViewById(R.id.item_detail);
                mImage = (ImageView) view.findViewById(R.id.item_thumbnail);
                mDate = (TextView) view.findViewById(R.id.date);
                mCategory = (TextView) view.findViewById(R.id.category);
                mCheckBox = (CheckBox) view.findViewById(R.id.checkbox);
                mImageButton = (ImageButton) view.findViewById(R.id.imageButton);
                mview = view;


view.setOnClickListener(this);
            view.setOnLongClickListener(this);

        }

        @Override
        public void onClick(View v) {
            CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
            CustomTabsIntent customTabsIntent = builder.build();
            customTabsIntent.launchUrl(MainActivity.activity, Uri.parse(mDataSet.get(getAdapterPosition()).getPostLink()));
        }

我如何维持recyclerview的位置

public void setRecyclerViewLayoutManager(LayoutManagerType layoutManagerType) {
    int scrollPosition = 0;

    if (mRecyclerView.getLayoutManager() != null) {
        scrollPosition = ((LinearLayoutManager) mRecyclerView.getLayoutManager())
                .findFirstCompletelyVisibleItemPosition();
    }

    switch (layoutManagerType) {
        case GRID_LAYOUT_MANAGER:
            mLayoutManager = new GridLayoutManager(getActivity(), SPAN_COUNT);
            mCurrentLayoutManagerType = LayoutManagerType.GRID_LAYOUT_MANAGER;
            break;
        case LINEAR_LAYOUT_MANAGER:
            mLayoutManager = new LinearLayoutManager(getActivity());
            mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
            break;
        default:
            mLayoutManager = new LinearLayoutManager(getActivity());
            mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
    }

    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.scrollToPosition(scrollPosition);
}