I want to have rows with gradient background in my ListView and i want them to change to a solid color when pressed. But i can't get it right. I know about the problem ListViews have and i read some solutions in other topics but none of them seems to work for me. Any help would be great.
When i run the following code i get this image that is ok:
But when i press on item 2, all rows will be green!:
Here is my Adapter:
public class MyAdapter extends BaseAdapter {
private final Context mContext;
private ArrayList<String> mMenuItems;
StateListDrawable mBackgroundDrawable;
public MyAdapter(Context context) {
this.mContext = context;
mMenuItems= new ArrayList<>();
for (int i = 0; i < 10; i++) {
mMenuItems.add(String.format("Item %d", i));
}
// make background drawable
GradientDrawable backgroundDrawable = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM,
new int[]{ Color.RED, Color.YELLOW, Color.RED});
backgroundDrawable.setCornerRadius(0f);
ColorDrawable selectedColorDrawable= new ColorDrawable(Color.GREEN);
mBackgroundDrawable = new StateListDrawable();
mBackgroundDrawable.addState(new int[] {android.R.attr.state_pressed}, selectedColorDrawable);
mBackgroundDrawable.addState(new int[] {android.R.attr.state_focused}, selectedColorDrawable);
mBackgroundDrawable.addState(new int[] {android.R.attr.state_selected}, selectedColorDrawable);
mBackgroundDrawable.addState(new int[] {}, backgroundDrawable);
}
@Override
public int getCount() {
return mMenuItems.size();
}
@Override
public Object getItem(int position) {
return mMenuItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewGroup item = getViewGroup(convertView);
if (Build.VERSION.SDK_INT<Build.VERSION_CODES.JELLY_BEAN) {
item.setBackgroundDrawable(mBackgroundDrawable);
} else {
item.setBackground(mBackgroundDrawable);
}
// set title
TextView titleView= (TextView) item.findViewById(R.id.menu_title);
titleView.setText(mMenuItems.get(position));
return item;
}
public ViewGroup getViewGroup(View reuse)
{
if(reuse instanceof ViewGroup)
return (ViewGroup)reuse;
LayoutInflater inflater = LayoutInflater.from(mContext);
ViewGroup item = (ViewGroup)inflater.inflate(R.layout.row_menu, null);
return item;
}
}
My row_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView
android:id="@+id/menu_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"/>
</RelativeLayout>
My activity's layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<ListView
android:id="@+id/activity_main_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
My activity:
public class MainActivity extends ActionBarActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list = (ListView) findViewById(R.id.activity_main_listview);
list.setAdapter(new MyAdapter(this));
}
}
mBackgroundDrawable is common for all your Rows. You need to attach a unique mBackgroundDrawable to every row (for example inside your loop where you add it). This is pretty much straightforward, but feel free to ask if you need a specific example ! ;)