Android - Color repeat on scrolling in RecyclerView - android

I have a RecyclerView in which a selected number of items show a different background color.
It gave me with many views with different color.
I used the position of adapter to change color and later I found that position changes when scrolling.
So I gave a unique code to every object of the ArrayList. And I gave the program to change background color if the unique code matches the given code.
Like,
uniqueCode = 5;
public void onBindViewHolder(//..){
Object object = objectArrayList.get(i);
if(object.uniqueCode() == uniqueCode ){
holder.layout.setBackgroundColor(//....);
}
}
But still I get some views changes the background color on scrolling which does not match that unique Id.
What is the solution to this problem ?

If i understand correctly there are multiple cells with the "unique" color but it shouldn't. This happens because you never reset the background of the layout to the default color.
if(object.uniqueCode() == uniqueCode){
holder.layout.setBackgroundColor(/*unique color*/);
}
else{
holder.layout.setBackgroundColor(/*default color*/);
}

Related

Recycler View repeats background colour

I have a songs list in recycler view. and i need to change the background colour of rows previously played.
so in my onBindViewHolder i have this.
if(activeAudioIndex > activeList.indexOf(currentSong)) {
currentRow.setBackgroundColor(R.color.colorPink)
}
in this activeAudioIndex is the actual index of playing song.
activeList is the array used for recyclerView
But i can see changed background colours repeating for unnecessary rows. when i scroll down.
HOW TO FIX THIS ISSUE ?
You need to add the 'else' part as well. So your code should look like this :
if(activeAudioIndex > activeList.indexOf(currentSong)) {
currentRow.setBackgroundColor(R.color.colorPink)
} else {
//DEFAULT BACKGROUND COLOR
currentRow.setBackgroundColor(R.color.another_color)
}

setImageResource() will not update my ImageView size/content directly

I have a row in a listview activity where an image should be changed when the row is selected. The new image is identical but larger (double size) than the original image. It is supposed to be reduced when it is deselected (Selection is implemented using my internal selection, not the android keyboard selection kind) again.
Problem is when (selected) I change the picture (at the time of bind) of my ImageView () using setImageResource() it does not update to the new larger version in my List row on the first click. Second execution of bind works fine. The same applies for the old item being deselected. The change to a smaller icon does not get in effect until a second click. The problem repeats itself each time I select another row than the currently selected and starts working correctly again on the second click.
I have stepped through code and seen that I pass the right id to the ImageView and this is prooved by the fact that another View showing a yellow border on the same row is changed to visible/hidden when selected/deselected.
To me it feels like the setImageResource() does not refresh the view immediately, but using invalidate() on the view or the row has no effect at all.
Is this a bug (how to work around) or do I do something wrong?
Code: (from my holder class using the holder pattern - executed by the adapter bind method)
#Override
public void refreshFromCursor(final Context context, final Cursor cursor) {
...
boolean selected = adapter.getSelectedPosition()==cursor.getPosition();
if (selected){
selectedIndicator.setVisibility(View.VISIBLE); // Show a "selected" yellow border indicator to the left
} else {
selectedIndicator.setVisibility(View.GONE); // Hide a "selected" yellow border indicator to the left
}
...
if (selected) completionResId = R.drawable.folder_selected;
else completionResId = R.drawable.folder;
...
statusIcon.setImageResource(completionResId); // !!!!! This is where I set the image that does not refresh/resize
...
}
Also tested exchanging the
statusIcon.setImageResource(completionResId);
with
statusIcon.setImageDrawable(context.getResources().getDrawable(completionResId));
but nothing changes in the problematic behaviour
In my opinion it is about the way you have implemented,
if you provide us the full code of refreshFromCursor(...) function we can have better understanding of what you did.
I found a hack. just make your drawable bigger
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="500dp"
android:height="500dp"
android:viewportWidth="139"
android:viewportHeight="139">
<path
android:fillColor="#000000"
android:pathData="M117.037,61.441L36.333,14.846c..." />
</vector>
here I set the drawable to be drawn on a 500dp canvas (instead of a 139dp one that was there originally) and it did the trick. no more weird tiny icons that later increase in size.

How to Apply different colors to list view items in Android

I have to show list view as like below image
Here I have the view in the xml as like this
<View
android:id="#id/margin"
android:background="#6DAAE9"
android:layout_width="20.0dip"
android:layout_height="fill_parent" />
I Have to change this background (#6DAAE9) dynamically when list is loaded and I have to fix these colors to corresponding item even the list items are increased.
Can any body help me. Thanks in advance
Basiclly you need to edit your getView method.
you can use
convertView.setBackgroundColor(Color.parseColor("#6DAAE9"));
With your appropriate logic, meaning if you want it to be random set random color or hold an array with the order, etc.
See this listview I added list item as per odd or even row and you can do it checking position in getView() method.And you should put imageview for that and set color rather that set color to row.
if(position==0){
//here set color for imageview which position is 0
}else if(position==1){
//here set color for imageview which position is 1
}
....

listview item background color change

I am working on an android application. I have created a listview by using
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayname));
getListView().setTextFilterEnabled(true);
Now i want to change the selected item's color. I could change the background of the selected item by placing
listviewobject.getChildAt(position).setBackgroundColor(Color.BLACK);
in onListItemClick()
this code is changing the background color but if I select any other list item then also the previously clicked list item's color is red.So I change the previously clicked listitem's color by
l.getChildAt(prevpos).setBackgroundColor(Color.BLACK);
now the problem is if i change the background of previously clicked listitems color to black.Then i can't see the text on that particular listitem.I i click again then only i can see the text on that item.So its look weired.please help me friends
After going through lots of posts and blogs i found this solution it works for me...
declare row variable global
public View row;
your_list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v,
int position, long id) {
if (row != null) {
row.setBackgroundResource(R.color.orange);
}
row = v;
v.setBackgroundResource(R.color.transparent_green);
)};
Well using this in the properties of your list view mught help:
android:listSelector="#drawable/list_selector"
It will automatically set the selected drawable as background of the selected item. Hope this works for you and drawable may be your own choice's color.
Explanation: Add an image strip to your drawables folder/s and set that image in listSelector attribute of the your list view. Now you will navigate through your list view, the list view's background will be of the color of the image strip you set instead of android's native color. Hope you get it now...:-)
I this This happening because you have put text color as black and your setting the background color also black that's why you can't see the difference. for setting the background color you can use the following line.
view.setBackgroundColor(Color.YELLOW);
use different color then text color.
By default the background is black. If you have customized your listview then when you scroll it would turn black. So when you define your list view set background cache color to the color you need as below:
yourlistView.setCacheColorHint(Color.WHITE);
What you can do is instead of using .setBackgroundColor() with a Color value, create a Color State List and assign it with .setBackgroundResource(). That way, you can define the various states that your list item can become, depending on the item's current state.

Reseting the background color of a View

I'm trying to restore the background Color of a View.
I have several selectable Views. When the user clicks one of those Views, the following code is executed and the View becomes Yellow:
View newSelection, previousSelection;
...
if(previousSelection != null) {
previousSelection.setBackgroundColor(Color.BLACK); // problem here
}
newSelection.setBackgroundColor(Color.YELLOW);
However, I want to reset the color of the previously selected View. However, I do not know which color it was (I'm setting it to Color.BLACK in the above code). I was not able to find a getBackgroundColor or similar method in the View class. If I had it, I could save the previous color and just put it back when the new View is selected.
use View.getBackground(), it returns the current 'Drawable' background of the view which can then be used in View.setBackgroundDrawable()
View theView;
Drawable originalBackground;
...
originalBackground = theView.getBackground();
theView.setBackgroundColor(Color.YELLOW);
...
theView.setBackgroundDrawable(originalBackground);
I'm not sure exactly what you are trying to accomplish but perhaps a ColorStateList would come in handy here.
You can try setting the previous color as a tag of the view.
For example
View newSelection, previousSelection;
newSelection.setTag(Color.Green);
previousSelection.setTag(Color.Black);
if(previousSelection != null) {
previousSelection.setBackgroundColor((int)previousSelection.getTag());
}
newSelection.setBackgroundColor(Color.YELLOW);
I haven't tried the code if there is an error but the flow on how to implement is there.

Categories

Resources