-1

I'm currently working on RecyclerView implementation on my project. When I set item on RecyclerView and scroll, I lost its position.

I Use Some if and else condition to click and change the color of text but its change when I'll not scroll the RecyclerView item. Please suggest me the right way how to handle this condition.

adapter.setOnItemClickListner(new LoadVehicleTypeAdapter.setOnitemclick() {
@Override
public void ImageClick(int position, String Name,String Description,int id) {
    LinearLayout linearLayout = null;
    CustomTextView customTextView = null;
    try {
        for (int i = 0; i < messages.size(); i++) {
            linearLayout = (LinearLayout) rvVehicleTypes.getChildAt(i).findViewById(R.id.root1);
            customTextView = (CustomTextView) rvVehicleTypes.getChildAt(i).findViewById(R.id.frag_cartypes_inflated_name);
            if (i==position) {
                linearLayout.setBackgroundColor(Color.parseColor("#999999"));
                customTextView.setTextColor(Color.parseColor("#FFFFFF"));


            } else {
                linearLayout.setBackgroundColor(Color.parseColor("#f3f3f3"));
                customTextView.setTextColor(Color.parseColor("#2196F3"));
            }
        }
        //Toast.makeText(getActivity(),Name, Toast.LENGTH_SHORT).show();
    }
    catch (Exception e)
    {

    }
}

});
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Mohit Lakhanpal
  • 1,309
  • 3
  • 11
  • 19

2 Answers2

0

Seems you are new to the Android development world, with this assumption let me take you through the journey of ListView to RecyclerView.

ListView, we were used to follow ViewHolder pattern to manage items being recycled when you scroll the listview.

Now RecyclerView have that functionality in-built, not in-built but you have to implement it forcefully, I am talking about onCreateViewHolder and onBindViewHolder.

So now let's take one step ahead in understanding how it works, as soon as you start scrolling down the list, inflated item views start getting recycled, with that your previously inflated ViewHolder (which gets actually created in onCreateViewHolder) is reused.

Now to solve this issue, you have to remember the position and for the same most of us used to use SparseBooleanArray

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • @ Paresh I'll also create a Recycle view Adapter class which include onCreateViewHolder and onBindViewHolde.The problem occurs when I scroll recycle view and that time I'll click 6th item then show click on 8th item ? – Mohit Lakhanpal Dec 04 '18 at 06:06
  • @MohitLakhanpal now I am actually expecting you to complete the research on the words I have shared. you will get the solution! – Paresh Mayani Dec 04 '18 at 06:07
  • I'm not changing the color of the text when use scroll view used otherwise it will work done for me. – Mohit Lakhanpal Dec 04 '18 at 09:17
0

Please read the documents of RecyclerView. Also I encourage you to read difference between ListView and RecyclerView

Are you using RecyclerView or ListView? From your code it seems that you are using ListView.

If you want to use RecyclerView please go through the tutorials.

Please go through this tutorial. It will tell you how to use RecyclerView. For click listener follow this tutorial.

Rohit
  • 2,646
  • 6
  • 27
  • 52
  • this link did'nt help me please share another one with related to my problem – Mohit Lakhanpal Dec 04 '18 at 09:13
  • can you exactly tell me your requirement? – Rohit Dec 04 '18 at 11:00
  • I'll use Recycle view to set an item horizontally and I'll need to change the layout background color and text color on click the item.that was done sometime and sometime it will not change. – Mohit Lakhanpal Dec 05 '18 at 04:24
  • Use RecyclerView Manager as LinearLayoutManager with Horizontal. To change color, change color in `BindViewHolder()` Method. Also Use `ContextCompact.setColor` to change layout color. – Rohit Dec 05 '18 at 07:13
  • I'll use this code to BindViewHolder() method but it will not change the color or background when it is in if the condition if(I==position){ holder.llRoot.setBackgroundColor(Color.parseColor("#999999")); holder.mCarType.setTextColor(Color.parseColor("#ffffff")); } else { holder.llRoot.setBackgroundColor(Color.parseColor("#f3f3f3")); holder.mCarType.setTextColor(Color.parseColor("#2196F3")); } – Mohit Lakhanpal Dec 06 '18 at 04:59
  • Instead of `Color.parseColor`, use `ContextCompact.setColor()` method – Rohit Dec 15 '18 at 10:11