I'm trying to implements this method on my recyclerview method Changing background color of selected item in recyclerview
I followed the script correctly and it works the way i want to. Yes it changes the color but everytime i clicked on the item, seems like it keeps adding new data. I'm using 2 recyclerview.
My method on first recyclerview called another method to drawn a recyclerview again. It is like when we called first recyclerview it also calls second recyclerview.
This is my script :
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.viewItem.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,true);
holder.viewItem.setLayoutManager(layoutManager);
HoriSpacingDecoration horiSpacingDecoration = new HoriSpacingDecoration(Utils.convertDpToPixel(10,context));
holder.viewItem.addItemDecoration(horiSpacingDecoration);
adapterChart = new ChartItemAdapter(context);
holder.viewItem.setAdapter(adapterChart);
mItemsChart.clear();
holder.textView.setText(mBulan.get(position));
int dataTertinggi = Integer.parseInt(Collections.max(mItems));
int botol = dataTertinggi/5;
int ci = niceround(dataTertinggi);
Log.d(TAG, "onBindViewHolder: pembulatan " + ci);
ukuran = Integer.parseInt(mItems.get(position))/botol;
if(Integer.parseInt(mItems.get(position))%10 < botol)
{
ukuran2=1;
ukuran+=ukuran2;
}
else
{
ukuran2=1;
ukuran+=ukuran2;
}
for(int y=0;y<ukuran;y++)
{
mItemsChart.add(String.valueOf(y));
}
adapterChart.addData(mItemsChart);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context,mItems.get(position) + " Bottle" ,Toast.LENGTH_SHORT).show();
prevPos = position;
mItemsChart.clear();
notifyDataSetChanged();
}
});
if(prevPos == position)
{
holder.textView.setTextColor(context.getResources().getColor(R.color.colorPrimary));
}
else
{
holder.textView.setTextColor(context.getResources().getColor(R.color.colorAccent));
}
}
I already found the problem is it when i called notifydatasethaschanged() it also called the adapter to add data. I'm stuck and dont know what to do. Perhaps anyone could find a solution for me.