我创建了一个listView行集,我已成功地将这些行成功交替。
但是,我希望能够在用户选择行后更改行的颜色。
我已将XML设置如下:
public class Task2Activity extends ListActivity {
ListView list;
String [] dogTitles;
String [] dogDescriptions;
String [] dogToastDescriptions;
int[] images = {R.drawable.husky, R.drawable.pug, R.drawable.yorkshire_terrier, R.drawable.french_bulldog,
R.drawable.border_collie, R.drawable.great_dane, R.drawable.staffordshire_bull_terrier, R.drawable.golden_retriever,
R.drawable.german_shepherd, R.drawable.doberman};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_task2);
Resources res = getResources();
dogTitles = res.getStringArray(R.array.titles);
dogDescriptions = res.getStringArray(R.array.descriptions);
dogToastDescriptions = res.getStringArray(R.array.toastdescriptions);
//list = (ListView) findViewById(R.id.listView);
dogAdapter <String> adapter = new dogAdapter<String>(this, dogTitles, images, dogDescriptions) {
//list.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if (position %2 == 1)
{
view.setBackgroundColor(Color.parseColor("#e6e9ef"));
}
else
{
view.setBackgroundColor(Color.parseColor("#c9d0db"));
}
return view;
}
};
setListAdapter(adapter);
registerForContextMenu(getListView());
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.my_context_menu, menu);
}
protected void onListItemClick(ListView l, View v, int position, long id){
String item = dogToastDescriptions[position];
Toast.makeText(this, item, Toast.LENGTH_LONG).show();
getListView().setSelected(true);
}
class dogAdapter<S> extends ArrayAdapter<String> {
Context context;
int[] images;
String[] titleArray;
String[] descriptionArray;
dogAdapter(Context c, String[] titles, int imgs[], String[] desc) {
super(c, R.layout.single_row, R.id.largeText, titles);
this.context = c;
this.images = imgs;
this.titleArray = titles;
this.descriptionArray = desc;
}
class MyViewHolder {
ImageView myImage;
TextView myTitle;
TextView myDescription;
MyViewHolder(View v) {
myImage = (ImageView) v.findViewById(R.id.imageView);
myTitle = (TextView) v.findViewById(R.id.largeText);
myDescription = (TextView) v.findViewById(R.id.smallText);
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
MyViewHolder holder = null;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.single_row, parent, false);
holder = new MyViewHolder(row);
row.setTag(holder);
Log.d("dogs", "Creating a new row");
} else {
holder = (MyViewHolder) row.getTag();
Log.d("dogs", "Recycling stuff");
}
//ImageView myImage = (ImageView) row.findViewById(R.id.imageView);
//TextView myTitle = (TextView) row.findViewById(R.id.largeText);
//TextView myDescription = (TextView) row.findViewById(R.id.smallText);
holder.myImage.setImageResource(images[position]);
holder.myTitle.setText(titleArray[position]);
holder.myDescription.setText(descriptionArray[position]);
return row;
}
}
}
我的完整java代码如下:
{{1}}
任何帮助非常感谢。非常感谢
答案 0 :(得分:2)
在onListItemClick()中,您可以设置所选项目的颜色:
protected void onListItemClick(ListView l, View v, int position, long id){
String item = dogToastDescriptions[position];
v.setBackgroundColor(ContextCompat.getColor(this, android.R.color.holo_green_light));//your color here
Toast.makeText(this, item, Toast.LENGTH_LONG).show();
getListView().setSelected(true);
}
Listview中还有一个选项,用于突出显示所选值:
<ListView android:id="@+id/my_listView"
android:choiceMode="singleChoice"
android:listSelector="@android:color/darker_gray" <!--your color here -->
/>
android:choiceMode =&#34; singleChoice&#34;是你在找什么