0

我想将当前单击的项目设置为我已经实现的不同颜色。

@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() 中被回收,所有这些视图也具有相同的背景。如果我改变他们的背景,那么这个视图的背景也会改变。任何人都有解决此问题的方法。

4

4 回答 4

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);
    }
}
于 2013-06-19T07:00:36.157 回答
1

尝试使用自定义适配器,这也可以帮助您完全控制您的项目并设置选择的默认项目;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);
                }
            });
于 2017-04-14T14:04:51.577 回答
0

很容易我的朋友去这个网站,你会找到你的答案。

单击时更改项目的颜色,甚至更改列表视图中文本的颜色

于 2013-06-19T13:29:18.897 回答
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);
            }
            }
于 2013-06-20T15:24:27.513 回答