-1

Desired Result

Maintain scroll position and give right position on specific view of custom row.

I am using RecyclerView in which I am trying to add on Demand Data on beginning of list as well as end of list.

Fetching data through Scrolling bottom is working perfectly.

But Scrolling top having issues as follows:

  1. Set Click listener in Adapter's onBindViewHolder.
  2. Set Click listener in Recyclerview's OnItemTouchListener.

Here is the case:

When I scroll up the list and then It loads 15 data and add it to the top of list. Our intention is to Maintain scroll position. Which can be achieved through

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

But the Issue is while I am clicking on any Item without scrolling then View's OnClickListener gives wrong position. You can check adapter's method:

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

But at the same time in Recyclerview's addOnItemTouchListener gives right position with stringArrayList.get(position) but gives wrong position using 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()));
            }
        }));

When I try to add data in the beginning of the list by following I am getting the exact issue.

// 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();   
Sagar Shah
  • 4,272
  • 2
  • 25
  • 36
  • Remove unnecessary code from question because it is making more confusing. post only that part where you get issue exactly. – Jay Rathod Sep 05 '16 at 13:14

1 Answers1

0

This code work for me, so try some thing like that

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

how i maintain the recyclerview position

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);
}
Hamza
  • 2,017
  • 1
  • 19
  • 40
  • Have you checked by running my code? I don't find something new in ur code. No offense Cause tricky part here is I have to load data in the beginning of the list and maintains it's position. Otherwise it will perfectly work with my code too. – Sagar Shah Sep 05 '16 at 12:52
  • let you have 50 item in your Recyclerview and when 51 item is come you want to add 51 item to beginning of recyclerview (at 0 position?) – Hamza Sep 05 '16 at 13:01
  • yes to be exact I have 15 data(Item0 to Item14) in the beginning and then I go up to load more data(Item -1 to Item -15). So my list will be like Item -15, Item -14....Item0,Item1,...,Item15. – Sagar Shah Sep 05 '16 at 14:34
  • why you comment adapter.notifyItemRangeInserted(0, stringArrayListTemp.size());? – Hamza Sep 05 '16 at 14:42
  • When are you calling setRecyclerViewLayoutManager? – Sagar Shah Sep 05 '16 at 14:45
  • inside the fragment onCreateView setRecyclerViewLayoutManager(mCurrentLayoutManagerType); – Hamza Sep 05 '16 at 14:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122678/discussion-between-ameer-hamza-and-sagar-shah). – Hamza Sep 05 '16 at 14:50