GridView项目颜色在Android中重复OnScroll视图?

时间:2016-10-07 06:55:12

标签: android gridview android-adapter android-gridview onitemclicklistener

我在我的Activity中使用Gridview和自定义适配器扩展BaseAdapter类。

一切正常,我们设定值得到了理想的结果。但是当我们点击gridview的特定单元格并且我想要改变那个单元格中按钮的颜色时出现问题.OnClickListner我们改变了按钮的颜色,如下所示:

 gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

            Toast.makeText(BookingActivity.this, "Hi click working" + position, Toast.LENGTH_SHORT).show();

            try {

                Button button = (Button) view.findViewById(R.id.button3);
                button.setBackgroundColor(Color.GREEN);

            } catch (Exception e) {
                e.printStackTrace();
            }

         }
    });

然后按钮的颜色已经改变,但是当我们向上滚动网格视图时,然后在另一个单元格中重复颜色。我不希望这种颜色进一步重复。 也许细胞被重复使用,但如何避免这种类型的重复。

这是我的适配器类

public class CustomGrid extends BaseAdapter {

private Context mContext;
private final ArrayList<String> web;



public CustomGrid(Context c,ArrayList<String> web) {
    mContext = c;
    this.web = web;

}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return web.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return web.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View grid = convertView;

    CoachHolder holder = null;

    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (grid == null) {

        grid = inflater.inflate(R.layout.grid_single,parent,false);


        holder = new CoachHolder(grid);

        grid.setTag(holder);

    } else {
        holder =(CoachHolder)grid.getTag();
    }

    holder.button.setText(web.get(position));



    return grid;
}

static class CoachHolder {


    Button button;


    //TextView descriptions;


    public CoachHolder(View v) {


        button = (Button)v.findViewById(R.id.button3);

    }


}
 }

这是我的grid_item.xml文件:

  <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="wrap_content"
  android:layout_height="wrap_content"
 android:background="@android:color/white">
 <Button
    android:layout_width="150dp"
    android:layout_height="100dp"
    android:text="New Button"
    android:id="@+id/button3"
    android:clickable="false"
    android:focusable="false"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />
 </RelativeLayout>

我想要多个细胞选择,当我点击细胞特定细胞颜色改变但不重复。 我想我无法使用位置获得特定的细胞按钮。

请帮我摆脱这个问题,已经花了太多时间。 在此先感谢您的帮助

1 个答案:

答案 0 :(得分:0)

试试这个

  1. 在getView方法中设置单元格的颜色。
  2. 在适配器中添加方法以保存当前选定的位置
  3. onItemClickListener中,请致电saveSelectedItemPosition,然后致电notifyDatasetChanged
  4. 伪代码

    public void saveSelectedItemPosition(int position){
        currentSelectedPosition = position;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    // do what you are currently doing
    if(position == currentSelectedPosition){
        //change color 
    } else {
        //use default color
    }
    
    
    
    
    return grid;
    

    }