GridView设置选中状态
设置原理 是将bean中的数据加入一个字段isSelect 默认填充数据的时候 ,默认选中的第一个值 设置为true,其余的都设置为false.
然后 点击单个item的时候 ,循环list<bean> 将选中的position 的item设置为true,其余的都设置为false ,然后adapter notify.
activity
代码语言:javascript复制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
代码语言:javascript复制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
代码语言:javascript复制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单选 ,多选都是一个道理,就是记住并操作当前位置的数据 然后刷新。
DEMO源码
这个源码 是在上一篇给GridView添加 下拉刷新 下拉加载的demo基础上加的,所以这个里面也有GridView上拉下拉的功能。希望可以帮到大家。