如何在Android中的GridView滚动上保留复选框状态 [英] How to retain the checkbox state on scroll of gridview in android
本文介绍了如何在Android中的GridView滚动上保留复选框状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
- 我在做什么 ::我有一个
gridview
用来显示图像,gridview
的适配器具有图像和checkbox
- 正在发生的事情 ::当我选中复选框并向下滚动
gridview
时,当我scrollback
时,所选的checkbox
为 未选择 - 问题 ::如何防止这种情况,我需要在代码中进行哪些更改
- What i am doing:: I have a
gridview
where i am displaying images I adapter of thegridview
has image and acheckbox
- What is happening:: When i check the checkbox and i scroll the
gridview
down & when iscrollback
, the selectedcheckbox
is unselected - Question:: How can i prevent this What changes should i need to make in the code
grid_view_image_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/flag"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentRight="true"
android:scaleType="centerCrop"
android:background="#000000"
android:padding="1dp" />
<CheckBox
android:id="@+id/ch_bx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="CheckBox" />
</RelativeLayout>
AdapterGridViewImage.java
public class AdapterGridViewImage extends BaseAdapter {
// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
HashMap<String, String> resultp = new HashMap<String, String>();
public AdapterGridViewImage(Context context,
ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
//imageLoader = new ImageLoader(context);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ImageView flag;
CheckBox ch_bx;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.grid_view_image_item, parent, false);
resultp = data.get(position);
flag = (ImageView) itemView.findViewById(R.id.flag);
ch_bx=(CheckBox) itemView.findViewById(R.id.ch_bx);
//Picasso.with(context).load("http://10.0.2.2:3009/buffet_items/".trim()+resultp.get("item_image").trim()).into(flag);
Picasso.with(context)
.load("http://10.0.2.2:3009/buffet_items/".trim()+resultp.get("item_image").trim())
.resizeDimen(R.dimen.wanted_size,R.dimen.wanted_size).centerCrop().into(flag);
return itemView;
}
}
推荐答案
请修改您的代码.
1)我们必须使用ViewHolder类进行封装.
2)我们必须设置复选框的标签值,以便在那时重新创建视图时,我们可以从标签中获取值.
Please modify your code.
1) We have to use ViewHolder class for encapsulation.
2) We have to set tag values of checkbox so when view recreate at that time we can take values from tag.
public AdapterGridViewImage(Context context,
ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
//imageLoader = new ImageLoader(context);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
if(convertView==null)
{
holder=new ViewHolder();
convertView = inflater.inflate(R.layout.grid_view_image_item, parent, false);
holder.flag = (ImageView) convertView.findViewById(R.id.flag);
holder.ch_bx=(CheckBox) convertView.findViewById(R.id.ch_bx);
holder.ch_bx.setTag(false);
convertView.setTag(holder);
}
else
{
holder=(ViewHolder)convertView.getTag();
}
resultp = data.get(position);
if((Boolean)holder.ch_bx.getTag())
{
holder.ch_bx.setChecked(true);
}
else
{
holder.ch_bx.setChecked(false);
}
holder.ch_bx.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if(holder.ch_bx.isChecked())
{
holder.ch_bx.setTag(true);
}
else
{
holder.ch_bx.setTag(false);
}
}
});
//Picasso.with(context).load("http://10.0.2.2:3009/buffet_items/".trim()+resultp.get("item_image").trim()).into(flag);
Picasso.with(context)
.load("http://10.0.2.2:3009/buffet_items/".trim()+resultp.get("item_image").trim())
.resizeDimen(R.dimen.wanted_size,R.dimen.wanted_size).centerCrop().into(holder.flag);
return itemView;
}
class ViewHolder
{
ImageView flag;
CheckBox ch_bx;
}
这篇关于如何在Android中的GridView滚动上保留复选框状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文