Android开发上这样更新gridview,而当滑动时能正常显示。

jyshao 发布于 2014/06/06 12:41
阅读 1K+
收藏 0

我在页面上有一个gridview。 现在现实的都是TextView。

txt txt txt

txt txt txt

.............

1. 想把以上的最后一列改成CheckBox,这样做? (cb)的地方要CheckBox

txt txt (cb)

txt txt (cb)

.............

2. 在Activity类上的onCreate方法,有个叫onItemClick。在这里面,如果用户按了一个item,这样把gridview的第一列颜色变成红色?

我都试过,一当滑动后都乱了。 向开发高手请救。 以下是我的代码。

Activity类-------------------------------------------

public class MainActivity extends ActionBarActivity {
 
 static final String EXTRA_MESSAGE = "com.example.hellogridview.MESSAGE";
 GridView gridview, gridview2;
 CheckBox box;
 int visible;
 int count, position2, bgcolor = Color.WHITE;
 String[] txt = {"This1", "is", "test11111", "test11111","test11111", "This2", "is22323", "test111112","test11111","test11111", "This3", "is233", "test222222","test11111","test11111",
   "This4", "i23323s", "test121212", "test11111","test11111","This5", "i23323s", "test121212", "test11111","test11111", "This6", "i23323s", "test121212", "test11111","test11111",
   "This7", "i23323s", "test121212", "test11111","test11111","This8", "i23323s", "test121212", "test11111","test11111","This9", "i23323s", "test121212", "test11111","test11111",
   "This10", "i23323s", "test121212", "test11111","test11111","This11", "i23323s", "test121212", "test11111","test11111"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        box = (CheckBox) findViewById(R.id.checkbox1);
        gridview = (GridView) findViewById(R.id.gridview);
        gridview.setNumColumns(5);
       
        gridview.setAdapter(new MyAdapter(this, txt));
       
        visible = gridview.getFirstVisiblePosition();
        gridview2 = (GridView) findViewById(R.id.gridview2);
        gridview2.setNumColumns(5);
        gridview2.setAdapter(new MyAdapter2(this));
       
       gridview.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
          
             }
        });
    }

}

Adapter类----------------------------------------

public class MyAdapter extends BaseAdapter{
 private String[] texts;
 LayoutInflater inflater;
 
 public MyAdapter(Context context, String[] t) {
        this.texts = t;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
 
 class ViewHolder {         
   TextView tv;
  // CheckBox cb;
 }
 
 @Override
 public int getCount() {
  return texts.length;
 }

 @Override
 public Object getItem(int position) {
  return texts[position];
 }

 @Override
 public long getItemId(int position) {
  return position;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  ViewHolder holder;
  
  if (convertView == null) {
    holder = new ViewHolder();
    convertView = inflater.inflate(R.layout.a, null);
    holder.tv= (TextView) convertView.findViewById(R.id.textView3);  
   
    if(position%5 == 0) {
     convertView.setBackgroundColor(Color.RED);
     convertView.invalidate();
   }
   
    convertView.setTag(holder);
   } else {
    holder = (ViewHolder) convertView.getTag();           
   }
  if (holder.tv!= null) {
   holder.tv.setTextSize(8);               
   holder.tv.setGravity(Gravity.CENTER_HORIZONTAL);               
   holder.tv.setText(texts[position]);
  }
   return convertView;
 }
}

加载中
返回顶部
顶部