我想将当前点击的项目设置为我已实现的不同颜色。
@Override
public void onItemClick(StaggeredGridView parent, View view, int position,
long id) {
Toast.makeText(MainActivity.this, "Clicked Position "+position, Toast.LENGTH_LONG).show();
Log.d("Clicked","Clicked Position "+position+" Content "+contentList.get(position));
if(prevSelected !=null)
{
prevSelected.setBackgroundColor(getResources().getColor(android.R.color.white));
}
prevSelected = view;
view.setBackgroundResource(R.drawable.list_pressed_holo_light);
selectedPosition = position;
}
现在我面临的问题是,如果在getView()中回收所选视图,则所有这些视图也具有相同的背景。如果我改变他们的背景,那么这个视图的背景也会改变。任何人都有解决方案。
答案 0 :(得分:1)
如果您在getView()
方法中设置了所需的背景,一切都会正常工作;)
只需输入您的getView(int position, View convertView, ViewGroup parent)
方法:
if (convertView != null){
if (position == selectedPosition) {
convertView.setBackgroundColor(getResources().getColor(android.R.color.white));
} else {
convertView.setBackgroundResource(R.drawable.list_pressed_holo_light);
}
}
答案 1 :(得分:0)
我的朋友很容易去this site,你会找到答案。
单击时更改项目的颜色,甚至更改列表视图中文本的颜色
答案 2 :(得分:0)
这是我在getView()中最终得到的解决方案。
if (convertView != null){
// If the selected view is used somewhere else
if (convertView.equals(prevSelected) && position != selectedPosition)
{
convertView.setBackgroundColor(getResources().getColor(android.R.color.white));
Log.d("Yup", "Changing color");
Log.d("Yup", position + " " + selectedPosition);
}
// If the selected view is redrawn and the recycled view is not
// the same view again
else if (position == selectedPosition && !convertView.equals(prevSelected))
{
// Make all other views if any which were prev selected
// white
prevSelected.setBackgroundColor(getResources().getColor(android.R.color.white));
prevSelected = convertView;
convertView.setBackgroundResource(R.drawable.list_pressed_holo_light);
Log.d("Yup", "Setting pressed color");
Log.d("Yup", position + " " + selectedPosition);
}
// The case where pos == selected pos and same view was used
// again.Need to set it to that colour as it could have been
// changed in the first condition
else if(position == selectedPosition && convertView.equals(prevSelected))
{
prevSelected = convertView;
convertView.setBackgroundResource(R.drawable.list_pressed_holo_light);
Log.d("Yup!", "Setting that view colour");
Log.d("Yup!", position + " " + selectedPosition);
}
}
答案 3 :(得分:0)
尝试使用自定义适配器,这也可以帮助您完全控制项目并设置选择的默认项目; listView XML和item XML没有特殊设置。使用getViewTypeCount()和selectedViewType是避免回收视图的关键。
public class ListAdapter extends ArrayAdapter<MyObj> {
private final int layoutInflater;
private Context context;
private List<MyObj> items;
private int mSelectedItem = 0;
private int TAG_UNSELECTED = 0;
private int TAG_SELECTED = 1;
public ListAdapter(Context context, int resource, List<MyObj> items) {
super(context, resource, items);
this.context = context;
this.layoutInflater = resource;
this.items = items;
}
public void selectItem(int position) {
mSelectedItem = position;
notifyDataSetChanged();
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
return position == mSelectedItem ? TAG_SELECTED : TAG_UNSELECTED;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(layoutInflater, null);
}
MyObj myObj = items.get(position);
TextView textView = (TextView) v.findViewById(R.id.title);
textView.setText(myObj.title);
int type = getItemViewType(position);
if(type == TAG_SELECTED) {
v.setBackgroundColor(Color.parseColor("#1da7ff"));
textView.setTextColor(Color.parseColor("#ffffff"));
} else {
v.setBackgroundColor(Color.parseColor("#f8f8f8"));
textView.setTextColor(Color.parseColor("#474747"));
}
return v;
}
}
然后在你的活动中:
ListView listView = (ListView) findViewById(R.id.list_view);
ListAdapter adapter = new ListAdapter(mContext, R.layout.item_layout, list);
listView.setAdapter(adapter);
adapter.selectItem(0); // Default selected item
// Get selected item and update its background
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
adapter.selectItem(position);
}
});