, , , , . , .
:
listview.xml( )
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parent"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:id="@+id/text"
android:text="Text"
android:padding="20dp"/>
</LinearLayout>
getView() :
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
Holder holder = new Holder();
if (view == null) {
LayoutInflater inflater = LayoutInflater.from(context);
view = inflater.inflate(R.layout.listview, viewGroup, false);
} else {
holder = (Holder) view.getTag();
}
holder.parent = (LinearLayout) view.findViewById(R.id.parent);
setRandomListViewSelector(holder.parent);
holder.text = (TextView) view.findViewById(R.id.text);
holder.text.setText(strs.get(i));
return view;
}
, :
public static void setRandomListViewSelector(View parentView) {
int i = new Random().nextInt(2);
switch (i) {
case 0:
parentView.setBackgroundResource(R.drawable.list_selector_blue);
break;
case 1:
parentView.setBackgroundResource(R.drawable.list_selector_red);
break;
}
}
list_selector_blue.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<solid android:color="@android:color/holo_blue_dark"/>
</shape>
</item>
</selector>
Random 0 1. , 1, , .