使用这段代码,我在这里给你一个偶数和奇数位置的例子(你可以像你一样改变位置上的条件)
ArrayAdapter adapter = new MyListAdapter();
listView = (ListView) findViewById(R.id.myListView);
listView.setAdapter(adapter);这是MyListAdapter的实现
public class MyListAdapter extends ArrayAdapter {
private class MyListAdapter extends ArrayAdapter {
public MyListAdapter() {
super(context, R.layout.ifact1_help2, R.layout.ifact1_help1, liste);
}
@Override
public int getCount() {
return yourListe.size();
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
if (position % 2 == 0) {
return 1;
}
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if (position % 2 == 0) {
itemView = getLayoutInflater().inflate(R.layout.textViewRed, parent, false);
itemView.setText(" your text");
} else {
itemView = getLayoutInflater().inflate(R.layout.textViewGreen, parent, false);
itemView.setText(" your text");
}
return itemView;
}
}这里是textViewRed.xml和green,你必须在目录布局上定义
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:paddingLeft="10dp"
android:textColor="@color/black"
android:background="@color/red"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:paddingLeft="10dp"
android:textColor="@color/black"
android:background="@color/green"
/>