2013-06-19 56 views
0

我想将当前点击的项目设置为我以前实现的不同颜色。ListView更改选定项目的背景

@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

使用选择此puspose – Raghunandan

回答

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); 
      } 
      } 
+0

是在3.0以下的Android版本的工作? –

+0

是的。它应该适用于所有版本的Android。 – Rajul

+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); 
    } 
} 
+0

但是这是没有的。你应该遵循的方式,而不是你应该添加view.setBackgroundResource(R.drawable.some_state_list_drawable) – pskink

+0

你是对的,但那不是重点。重要的是在'getView()'方法中设置背景。当谈到如何做到这一点时,我只是复制了Rajul正在使用的代码来为选定和未选择的项目设置背景 – marcin

+0

问题在于,它是无望的低效率,因为所有的循环视图都必须重新绘制。 – Rajul

0

那么容易我的朋友去this site,你会找到答案。

变化单击时的物品的颜色和列表视图

+0

多数民众赞成只用于悬停。我希望它保持选定。 – Rajul

0

尝试使用自定义适配器,这也可以帮助您完全控制您的项目并设置默认项目; listView XML和项目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); 
       } 
      }); 
相关问题