前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >GridView 设置选中状态

GridView 设置选中状态

作者头像
再见孙悟空_
发布2023-02-10 19:33:08
9270
发布2023-02-10 19:33:08
举报
文章被收录于专栏:【Android开发基础】

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上拉下拉的功能。希望可以帮到大家。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-11-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • activity
  • adapter
  • bean
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档