问题是,点击后,变色,先前点击的不能恢复以前的颜色。
我写的如下:
listview.setOnItemClickListener(new OnItemClickListener () {public void onItemClick(AdapterView<?> arg0, View view, int position,
long arg3) {
index = position;
//Iterator i = DataList.iterator();for (int i = 0; i < DataList.size(); i ++) {
listview.getChildAt(i).setBackgroundColor(Color.BLACK);
//设置被选中状态的颜色,当取消选中的时候,又变回黑色
}view.setBackgroundColor(Color.GRAY);}
  
  });
问题关键是:getChildAt()只对当前页面管用,滚动条新拉出来的item就会出现异常。
怎样取得指定的view??

解决方案 »

  1.   

    你可以设置一个boolean值,当单击为true时,背景变色。为false时,不变。
      

  2.   

     想了一下,似乎不可行,能不能具体点解释一下??
    我现在的思路是这样的:定义一个成员变量int formerViewID来记录上次被点击过的view的ID,然后当点击时候,view.setBackgroundColor(Color.GREY),
    接下来,把这次点击的view的position赋值给formerViewID,当下次点击时候,先为position为formerViewID的view恢复成原来的颜色。总结起来就是:
    取得指定position=formerViewID的view.setBackgroundColor(Color.BLACK);
    view.setBackgroundColor(Color.GREY);
    formerViewID=position;
      

  3.   

    现在问题的关键是:如何取得指定的view
      

  4.   

    好吧,我来告诉你2个方法。都是可行的我做过。第一个:设置listview的getview中的最外层界面的监听事件。
    private class 最外层界面 implements OnClickListener {
    private int position;
    public SingleChoiceListener(int position) {
    this.position = position;
    } public void onClick(View arg0) {
    items.get(position).checked = true;
    for (int i = 0; i < items.size(); i++) {
    if (i != position) {
    items.get(i).checked = false;
    }
    }
    WallPaperAdapter.this.notifyDataSetChanged();
    }
    }
    第2个方法:在setOnItemClickListener中class BottomListener implements OnItemClickListener { public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
    long arg3) {
    adapter类.SetFocus(arg2);
    }
    }public void SetFocus(int index) {
    this.index = index;
    // this.notifyDataSetChanged();
    this.notifyDataSetInvalidated();//刷新界面
    }
    然后在getview中设置你的背景颜色呵呵。
    public View getView(int position, View convertView, ViewGroup parent) {
    myview = layoutinflater.inflate(R.layout.group_bottom_item, null);
    ImageView imageview = (ImageView) myview.findViewById(R.id.imageview_bottom);
    TextView textview = (TextView) myview.findViewById(R.id.textview_bottom);
    if (position == 0) {
    imageview.setBackgroundResource(android.R.drawable.ic_menu_mapmode);
    textview.setText(R.string.Group_Item_Main);
    }
    if (position == 1) {
    imageview
    .setBackgroundResource(android.R.drawable.ic_menu_myplaces);
    textview.setText(R.string.Group_Item_My);
    }
    if (position == 2) {
    imageview.setBackgroundResource(android.R.drawable.ic_menu_share);
    textview.setText(R.string.Group_Item_square);
    }
    if (position == 3) {
    imageview.setBackgroundResource(android.R.drawable.ic_menu_more);
    textview.setText(R.string.Group_Item_More);
    }
    if(position==index){
    myview.setBackgroundResource(R.drawable.css_biaoqian);
    }else{
    myview.setBackgroundResource(0);
    }
    return myview;
    }
    }
    希望对你有帮助。