GridView 设置选中状态

简介: GridView 设置选中状态

GridView设置选中状态


设置原理 是将bean中的数据加入一个字段isSelect  默认填充数据的时候 ,默认选中的第一个值 设置为true,其余的都设置为false.


然后 点击单个item的时候 ,循环list 将选中的position 的item设置为true,其余的都设置为false  ,然后adapter notify.


activity

package com.example.refreshgridview;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;
import com.example.refreshgridview.PullToRefreshBase.OnRefreshListener;
public class MainActivity extends Activity {
  private PullToRefreshGridView mPullRefreshGridView;
  private GridView mGridView;
  private GridViewAdapter adapter;
  private List<GridInfo> list = new ArrayList<GridInfo>();
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mPullRefreshGridView = (PullToRefreshGridView) findViewById(R.id.video_gridView);
    mPullRefreshGridView.init(PullToRefreshGridView.MODE_BOTH);
    mGridView = mPullRefreshGridView.getRefreshableView();
    mPullRefreshGridView.setOnRefreshListener(refreshListener);
    for (int i = 0; i < 40; i++) {
      GridInfo gridInfo = new GridInfo();
      gridInfo.setTitle((i)+"");
      if(i==0){
        gridInfo.setSelect(true);
      }else{
        gridInfo.setSelect(false);
      }
      list.add(gridInfo);
    }
    adapter = new GridViewAdapter(MainActivity.this,list);
    mGridView.setAdapter(adapter);
    mGridView.setOnItemClickListener(new OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {
           for(int i=0;i<list.size();i++){
                   if (position == i) {//当前选中的Item改变背景颜色
                     list.get(i).setSelect(true);
                   } else {
                     list.get(i).setSelect(false);
                   }
                }
           adapter.notifyDataSetChanged();
      }
    });
  }
  private OnRefreshListener refreshListener = new OnRefreshListener() {
    @Override
    public void onRefresh(int mode) {
      if (PullToRefreshGridView.MODE_PULL_DOWN_TO_REFRESH == mPullRefreshGridView.getCurrentMode()) {
        Toast.makeText(MainActivity.this, "下拉刷新", Toast.LENGTH_SHORT).show();
        mPullRefreshGridView.onRefreshComplete();
      } else if (mode == PullToRefreshGridView.MODE_PULL_UP_TO_REFRESH) {
        // 加载更多
        Toast.makeText(MainActivity.this, "上拉加载更多", Toast.LENGTH_SHORT).show();
        mPullRefreshGridView.onRefreshComplete();
      }
    }
  };
}

adapter

package com.example.refreshgridview;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class GridViewAdapter extends BaseAdapter {
    private List<GridInfo> mList = new ArrayList<GridInfo>();
    private Context mContext;
    public GridViewAdapter(Context context,List<GridInfo> list) {
        super();
        this.mContext = context;
        this.mList = list;
    }
    @Override
    public int getCount() {
        return mList.size();
    }
    @Override
    public GridInfo getItem(int position) {
        return mList.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      ChildHolderOne holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_grid_live_show, parent, false);
            holder = new ChildHolderOne();
            holder.tvTitle = (TextView)convertView.findViewById(R.id.title_tv);
            convertView.setTag(holder);
        } else {
            holder = (ChildHolderOne) convertView.getTag();
        }
        final GridInfo gridInfo = mList.get(position);
        if(gridInfo.isSelect() == true){
          holder.tvTitle.setBackgroundColor(mContext.getResources().getColor(R.color.grays));
        } else {
          holder.tvTitle.setBackgroundColor(mContext.getResources().getColor(R.color.gray_max));
        }
        final String number = gridInfo.getTitle();
        holder.tvTitle.setText(number);
        return convertView;
    }
  class ChildHolderOne {
    TextView tvTitle;
  }
}

bean

package com.example.refreshgridview;
public class GridInfo {
  private String title;
  private boolean isSelect;
  public String getTitle() {
    return title;
  }
  public void setTitle(String title) {
    this.title = title;
  }
  public boolean isSelect() {
    return isSelect;
  }
  public void setSelect(boolean isSelect) {
    this.isSelect = isSelect;
  }
}

很简单效果 ,和listview单选 ,多选都是一个道理,就是记住并操作当前位置的数据 然后刷新。

目录
打赏
0
0
0
0
570
分享
相关文章
|
11月前
单选框设置默认选中
单选框设置默认选中
98 1
|
6月前
element组件库笔记一:element 框架中table表格复选框选中后,切换下一页之前选中复选框和数据消失的问题
这篇文章介绍了在Element UI框架中,如何解决表格组件复选框在分页时选中状态丢失的问题。
416 0
|
9月前
Element UI 多选表格--判断勾选数据行的 Checkbox 时为选中还是取消选中
Element UI 多选表格--判断勾选数据行的 Checkbox 时为选中还是取消选中
151 1
checkbox选中和取消,控制dom元素颜色变化
checkbox选中和取消,控制dom元素颜色变化
83 0
VUE-element-admin菜单栏图标选中状态跟随文字颜色状态变化
VUE-element-admin菜单栏图标选中状态跟随文字颜色状态变化
1186 0
VUE-element-admin菜单栏图标选中状态跟随文字颜色状态变化
【Layui】关于单选框的选中状态,下拉框默认显示
【Layui】关于单选框的选中状态,下拉框默认显示
681 0
【Layui】关于单选框的选中状态,下拉框默认显示
【Layui】对于列表页复选框只有选中时才会显示
【Layui】对于列表页复选框只有选中时才会显示
194 0
【Layui】对于列表页复选框只有选中时才会显示
C# 获取listview中选中一行的值
C# 获取listview中选中一行的值
898 0
QTableView如何取消选中行
QTableView如何取消选中行
1459 0

热门文章

最新文章