当我想要更改我点击的项目的图像时,我有列表问题。我尝试使用drawable更改背景颜色和文本颜色,但我无法更改ListView
的图像。那么当我点击它时如何更改该图像呢?
我的目的是想要将橙色图像更改为白色图像。
ListAdapter
public class ListAdapter extends BaseAdapter {
private String[] titles;
private int[] images;
private Context context;
public ListAdapter(Context context, String[] titles, int[] images) {
this.context = context;
this.titles = titles;
this.images = images;
}
@Override
public int getCount() {
return titles.length;
}
@Override
public Object getItem(int position) {
return titles[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.navigation_list_item, parent, false);
} else {
view = convertView;
}
TextView textView = (TextView) view.findViewById(R.id.tv_navigation_item);
ImageView imageView = (ImageView) view.findViewById(R.id.iv_navigation_item);
textView.setText(titles[position]);
imageView.setImageResource(images[position]);
return view;
}
}
ListActivity
public class ListActivity extends AppCompatActivity {
ListView listView;
ListAdapter adapter;
String [] title = new String[]{"Item A","Item B","Item C","Item D"};
int [] image = new int[]{R.drawable.ic_android_red_500_24dp,
R.drawable.ic_add_shopping_cart_red_500_24dp,
R.drawable.ic_android_red_500_24dp,
R.drawable.ic_accessibility_red_500_24dp};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
listView = (ListView) findViewById(R.id.lv_listview);
setNavigationList();
}
private void setNavigationList() {
adapter = new ListAdapter(this, title, image);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
view.setSelected(true);
}
});
}
}
答案 0 :(得分:0)
您可以在适配器上添加selectedPosition参数,并在点击
上更新活动public class ListAdapter extends BaseAdapter {
private String[] titles;
private int[] images;
private Context context;
private int selectedPosition;
public ListAdapter(Context context, String[] titles, int[] images, int selectedPosition) {
this.context = context;
this.titles = titles;
this.images = images;
this.selectedPosition = selectedPosition;
}
public void updateSelectedPosition(int selectedPosition){
this.selectedPosition = selectedPosition;
notifyDataSetChanged();
}
@Override
public int getCount() {
return titles.length;
}
@Override
public Object getItem(int position) {
return titles[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.navigation_list_item, parent, false);
} else {
view = convertView;
}
TextView textView = (TextView) view.findViewById(R.id.tv_navigation_item);
ImageView imageView = (ImageView) view.findViewById(R.id.iv_navigation_item);
textView.setText(titles[position]);
if(selectedPosition == position){
// change the image view to orange here
}else{
imageView.setImageResource(images[position]);
}
return view;
}
然后在活动
public class ListActivity extends AppCompatActivity {
ListView listView;
ListAdapter adapter;
String [] title = new String[]{"Item A","Item B","Item C","Item D"};
int [] image = new int[]{R.drawable.ic_android_red_500_24dp,
R.drawable.ic_add_shopping_cart_red_500_24dp,
R.drawable.ic_android_red_500_24dp,
R.drawable.ic_accessibility_red_500_24dp};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
listView = (ListView) findViewById(R.id.lv_listview);
setNavigationList();
}
private void setNavigationList() {
adapter = new ListAdapter(this, title, image,-1);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
view.setSelected(true);
adapter.updateSelectedPosition(position);
}
});
}
}