本文介绍了复选框歌厅滚动列表视图定制选中后...!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建自定义列表视图,并在它把复选框。结果起初我检查所有的复选框,然后我向下滚动列表视图,然后复选框未选中获得。结果我应该怎么办?结果
CustomListItem.java
结果
包com.example.customlistviewdemo;进口android.app.Activity;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.ArrayAdapter;
进口android.widget.CheckBox;
进口android.widget.ImageView;
进口android.widget.TextView;
进口android.widget.Toast;公共类CustomListItem扩展ArrayAdapter<串GT; { 私人最终活动范围内;
私人最终的String [] TitleArray;
私人最终的String [] SubTitlearray;
私人最终的String [] TimeArray;
私人最终整数[] MainImageId;
公共CustomListItem(Activity上下文,字符串[] TitleArray,
的String [] SubTitlearray,字符串[] TimeArray,整数[] MainImageId){
超(背景下,R.layout.list_item,TitleArray);
this.context =背景;
this.TitleArray = TitleArray; this.SubTitlearray = SubTitlearray;
this.TimeArray = TimeArray;
this.MainImageId = MainImageId;
} @覆盖
公共查看getView(INT位置,查看视图的ViewGroup父){
LayoutInflater吹气= context.getLayoutInflater();
查看rowView = inflater.inflate(R.layout.list_item,空,真);
ImageView的动物,汽车,环境,MAP1,警告1,微管相关蛋白,warning2,下载,共享,植物;
动物=(ImageView的)rowView.findViewById(R.id.imgAnimal);
汽车=(ImageView的)rowView.findViewById(R.id.imgCar);
环境=(ImageView的)rowView.findViewById(R.id.imgEnvironment);
厂=(ImageView的)rowView.findViewById(R.id.imgPlant);
MAP1 =(ImageView的)rowView.findViewById(R.id.imgMap);
警告1 =(ImageView的)rowView.findViewById(R.id.imgWarning);
MAP2 =(ImageView的)rowView.findViewById(R.id.imgMap2);
warning2 =(ImageView的)rowView.findViewById(R.id.imgWarining2);
下载=(ImageView的)rowView.findViewById(R.id.imgDownload);
复选框CHK =(复选框)rowView.findViewById(R.id.checkbox);
份额=(ImageView的)rowView.findViewById(R.id.imgShare); animal.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(视图v){
Toast.makeText(的getContext(),动物的ImageView点击
5000).show();
// TODO自动生成方法存根 }
});
car.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(视图v){
Toast.makeText(的getContext(),汽车ImageView的点击,5000)
。显示();
// TODO自动生成方法存根 }
});
environment.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(视图v){
Toast.makeText(的getContext(),
环境ImageView的点击,5000).show();
// TODO自动生成方法存根 }
});
-------------------------------------------------- ----------------
...........................等..................... .......
.................................................. ........ TextView的MAINTITLE =(TextView中)rowView.findViewById(R.id.txtMainTitle);
ImageView的mainImage =(ImageView的)rowView
.findViewById(R.id.imgMainImage); TextView的潜台词=(TextView中)rowView.findViewById(R.id.txtSubTitle);
TextView的TimeText =(TextView中)rowView.findViewById(R.id.txtTimeTitle); mainTitle.setText(TitleArray [位置]);
SubText.setText(SubTitlearray [位置]);
TimeText.setText(TimeArray [位置]);
mainImage.setImageResource(MainImageId [位置]); 返回rowView;
}
}
结果
ActivityMain.java
结果
包com.example.customlistviewdemo;进口android.app.Activity;
进口android.os.Bundle;
进口android.widget.ListView;公共类MainActivity延伸活动{
ListView控件列表;
的String [] = TitleArray {测试标题1,这是MAINTITLE
测试标题2,这是MAINTITLE,
测试TITLE3这是MAINTITLE,
测试TITLE4这是MAINTITLE,
测试TITLE5这是MAINTITLE,
测试TITLE6这是MAINTITLE};
的String [] = SubTitleArray {由人1报告,由人2日报道
依文3日报道报道,由人4,由人5日报道
依文6日报道}; 的String [] = TimeArray {约1小时前,大约2小时前
大约3小时前,大约4小时前,大约5小时前
大约6小时前}; 整数[] = MainImageId {R.drawable.android1,R.drawable.android2,
R.drawable.android3,R.drawable.android4,R.drawable.android5,
R.drawable.android6}; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
CustomListItem适配器=新CustomListItem(MainActivity.this,
TitleArray,SubTitleArray,TimeArray,MainImageId);
名单=(ListView控件)findViewById(R.id.listView1);
list.setAdapter(适配器); }
}
结果
list_item.xml
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:背景=#FFF
机器人:方向=垂直> <的LinearLayout
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginBottom =10dp> <的TextView
机器人:ID =@ + ID / txtMainTitle
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=这是示例文本
机器人:textAppearance =:/>中的Android ATTR / textAppearanceLarge?
< / LinearLayout中> <的LinearLayout
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:方向=垂直>
< / LinearLayout中> <的LinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =130dp> <的LinearLayout
机器人:layout_width =150dp
机器人:layout_height =WRAP_CONTENT
机器人:paddingRight =15dp> <复选框
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =FILL_PARENT/> < ImageView的
机器人:ID =@ + ID / imgMainImage
机器人:layout_width =100dp
机器人:layout_height =FILL_PARENT/>
< / LinearLayout中> <的LinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直> <的TextView
机器人:ID =@ + ID / txtSubTitle
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginBottom =5DP
机器人:文字=这是示例文本
机器人:textAppearance =机器人:ATTR / textAppearanceSmall
机器人:TEXTSIZE =16DP/> <的TextView
机器人:ID =@ + ID / txtTimeTitle
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=这是示例文本
机器人:textAppearance =机器人:ATTR / textAppearanceSmall
机器人:TEXTSIZE =12dp/> < HorizontalScrollView
机器人:ID =@ + ID / scrollView1
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:滚动条=无> <的LinearLayout
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginBottom =8DP
机器人:layout_marginTop =8DP> < ImageView的
机器人:ID =@ + ID / imgAnimal
机器人:layout_width =25dp
机器人:layout_height =25dp
机器人:layout_marginRight =10dp
机器人:SRC =@绘制/ channel_animal/> < ImageView的
机器人:ID =@ + ID / imgCar
机器人:layout_width =25dp
机器人:layout_height =25dp
机器人:layout_marginRight =10dp
机器人:SRC =@绘制/ channel_car/> < ImageView的
机器人:ID =@ + ID / imgEnvironment
机器人:layout_width =25dp
机器人:layout_height =25dp
机器人:layout_marginRight =10dp
机器人:SRC =@绘制/ channel_environment/> < ImageView的
机器人:ID =@ + ID / imgPlant
机器人:layout_width =25dp
机器人:layout_height =25dp
机器人:layout_marginRight =10dp
机器人:SRC =@绘制/ channel_plant/> < ImageView的
机器人:ID =@ + ID / imgMap
机器人:layout_width =25dp
机器人:layout_height =25dp
机器人:layout_marginRight =10dp
机器人:SRC =@绘制/ tab_icon_map/> < ImageView的
机器人:ID =@ + ID / imgWarning
机器人:layout_width =25dp
机器人:layout_height =25dp
机器人:layout_marginRight =10dp
机器人:SRC =@绘制/ warning_icon/>
< / LinearLayout中>
< / HorizontalScrollView> < HorizontalScrollView
机器人:ID =@ + ID / scrollView2
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:滚动条=无> <的LinearLayout
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT> < ImageView的
机器人:ID =@ + ID / imgMap2
机器人:layout_width =30dp
机器人:layout_height =30dp
机器人:layout_marginRight =10dp
机器人:SRC =@绘制/ tab_icon_map/> < ImageView的
机器人:ID =@ + ID / imgWarining2
机器人:layout_width =30dp
机器人:layout_height =30dp
机器人:layout_marginRight =10dp
机器人:SRC =@绘制/ warning_icon/> < ImageView的
机器人:ID =@ + ID / imgDownload
机器人:layout_width =30dp
机器人:layout_height =30dp
机器人:layout_marginRight =10dp
机器人:SRC =@绘制/ download_icon/> < ImageView的
机器人:ID =@ + ID / imgShare
机器人:layout_width =30dp
机器人:layout_height =30dp
机器人:layout_marginRight =10dp
机器人:SRC =@绘制/ share_icon/>
< / LinearLayout中>
< / HorizontalScrollView>
< / LinearLayout中>
< / LinearLayout中> <的LinearLayout
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT> <查看
机器人:ID =@ + ID /视图
机器人:layout_width =match_parent
机器人:layout_height =1DP
机器人:layout_toRightOf =@ + ID / text1中
机器人:背景=#FF9900/>
< / LinearLayout中>< / LinearLayout中>
解决方案
只要修改您的适配器类这样的架构,我希望它的工作原理:
重要的线路设置标签,并获取标签。
公共类MyAdapter扩展ArrayAdapter<模型与GT; { 私人最终名单<模型与GT;清单;
私人最终活动范围内;
布尔checkAll_flag = FALSE;
布尔checkItem_flag = FALSE; 公共MyAdapter(Activity上下文,列表<模型与GT;名单){
超(背景下,R.layout.row,清单);
this.context =背景;
this.list =清单;
} 静态类ViewHolder {
保护TextView的文本;
保护复选框复选框;
} @覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){ ViewHolder viewHolder = NULL;
如果(convertView == NULL){
LayoutInflater充气= context.getLayoutInflater();
convertView = inflator.inflate(R.layout.row,NULL);
viewHolder =新ViewHolder();
viewHolder.text =(TextView中)convertView.findViewById(R.id.label);
viewHolder.checkbox =(复选框)convertView
.findViewById(R.id.check);
viewHolder.checkbox
.setOnCheckedChangeListener(新CompoundButton.OnCheckedChangeListener(){ @覆盖
公共无效onCheckedChanged(CompoundButton buttonView,
布尔器isChecked){
INT为getPosition =(整数)buttonView.getTag(); // 这里
//我们得到了我们为使用setTag复选框的位置。
list.get(为getPosition).setSelected(
buttonView.isChecked()); //设置的值
//复选框
//保持其
//状态。
}
});
convertView.setTag(viewHolder);
convertView.setTag(R.id.label,viewHolder.text);
convertView.setTag(R.id.check,viewHolder.checkbox);
}其他{
viewHolder =(ViewHolder)convertView.getTag();
}
viewHolder.checkbox.setTag(位置); //这一行是很重要的。 viewHolder.text.setText(list.get(位置).getName());
viewHolder.checkbox.setChecked(list.get(位置).isSelected()); 返回convertView;
}
}
I Create custom listview, and put checkbox in it.
at First I check all checkbox and then I scroll down listview, then checkbox getting Unchecked.
what should I do?
CustomListItem.java
package com.example.customlistviewdemo;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class CustomListItem extends ArrayAdapter<String> {
private final Activity context;
private final String[] TitleArray;
private final String[] SubTitlearray;
private final String[] TimeArray;
private final Integer[] MainImageId;
public CustomListItem(Activity context, String[] TitleArray,
String[] SubTitlearray, String[] TimeArray, Integer[] MainImageId) {
super(context, R.layout.list_item, TitleArray);
this.context = context;
this.TitleArray = TitleArray;
this.SubTitlearray = SubTitlearray;
this.TimeArray = TimeArray;
this.MainImageId = MainImageId;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.list_item, null, true);
ImageView animal, car, environment, map1, warning1, map2, warning2, download, share, plant;
animal = (ImageView) rowView.findViewById(R.id.imgAnimal);
car = (ImageView) rowView.findViewById(R.id.imgCar);
environment = (ImageView) rowView.findViewById(R.id.imgEnvironment);
plant = (ImageView) rowView.findViewById(R.id.imgPlant);
map1 = (ImageView) rowView.findViewById(R.id.imgMap);
warning1 = (ImageView) rowView.findViewById(R.id.imgWarning);
map2 = (ImageView) rowView.findViewById(R.id.imgMap2);
warning2 = (ImageView) rowView.findViewById(R.id.imgWarining2);
download = (ImageView) rowView.findViewById(R.id.imgDownload);
CheckBox chk = (CheckBox) rowView.findViewById(R.id.checkbox);
share = (ImageView) rowView.findViewById(R.id.imgShare);
animal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "Animal ImageView is Clicked",
5000).show();
// TODO Auto-generated method stub
}
});
car.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "Cars ImageView is Clicked", 5000)
.show();
// TODO Auto-generated method stub
}
});
environment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(),
"environment ImageView is Clicked", 5000).show();
// TODO Auto-generated method stub
}
});
------------------------------------------------------------------
...........................SO on............................
..........................................................
TextView mainTitle = (TextView) rowView.findViewById(R.id.txtMainTitle);
ImageView mainImage = (ImageView) rowView
.findViewById(R.id.imgMainImage);
TextView SubText = (TextView) rowView.findViewById(R.id.txtSubTitle);
TextView TimeText = (TextView) rowView.findViewById(R.id.txtTimeTitle);
mainTitle.setText(TitleArray[position]);
SubText.setText(SubTitlearray[position]);
TimeText.setText(TimeArray[position]);
mainImage.setImageResource(MainImageId[position]);
return rowView;
}
}
ActivityMain.java
package com.example.customlistviewdemo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class MainActivity extends Activity {
ListView list;
String[] TitleArray = { "Testing Title1. This is MainTitle",
"Testing Title2. This is MainTitle",
"Testing Title3. This is MainTitle",
"Testing Title4. This is MainTitle",
"Testing Title5. This is MainTitle",
"Testing Title6. This is MainTitle" };
String[] SubTitleArray = { "Reported by Man 1", "Reported by Man 2",
"Reported by Man 3", "Reported by Man 4", "Reported by Man 5",
"Reported by Man 6" };
String[] TimeArray = { "about 1 hour ago", "about 2 hour ago",
"about 3 hour ago", "about 4 hour ago", "about 5 hour ago",
"about 6 hour ago" };
Integer[] MainImageId = { R.drawable.android1, R.drawable.android2,
R.drawable.android3, R.drawable.android4, R.drawable.android5,
R.drawable.android6 };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomListItem adapter = new CustomListItem(MainActivity.this,
TitleArray, SubTitleArray, TimeArray, MainImageId);
list = (ListView) findViewById(R.id.listView1);
list.setAdapter(adapter);
}
}
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFF"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp" >
<TextView
android:id="@+id/txtMainTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is Sample Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="130dp" >
<LinearLayout
android:layout_width="150dp"
android:layout_height="wrap_content"
android:paddingRight="15dp" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
<ImageView
android:id="@+id/imgMainImage"
android:layout_width="100dp"
android:layout_height="fill_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/txtSubTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="This is Sample Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="16dp" />
<TextView
android:id="@+id/txtTimeTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is Sample Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="12dp" />
<HorizontalScrollView
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp" >
<ImageView
android:id="@+id/imgAnimal"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginRight="10dp"
android:src="@drawable/channel_animal" />
<ImageView
android:id="@+id/imgCar"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginRight="10dp"
android:src="@drawable/channel_car" />
<ImageView
android:id="@+id/imgEnvironment"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginRight="10dp"
android:src="@drawable/channel_environment" />
<ImageView
android:id="@+id/imgPlant"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginRight="10dp"
android:src="@drawable/channel_plant" />
<ImageView
android:id="@+id/imgMap"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginRight="10dp"
android:src="@drawable/tab_icon_map" />
<ImageView
android:id="@+id/imgWarning"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginRight="10dp"
android:src="@drawable/warning_icon" />
</LinearLayout>
</HorizontalScrollView>
<HorizontalScrollView
android:id="@+id/scrollView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/imgMap2"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginRight="10dp"
android:src="@drawable/tab_icon_map" />
<ImageView
android:id="@+id/imgWarining2"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginRight="10dp"
android:src="@drawable/warning_icon" />
<ImageView
android:id="@+id/imgDownload"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginRight="10dp"
android:src="@drawable/download_icon" />
<ImageView
android:id="@+id/imgShare"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginRight="10dp"
android:src="@drawable/share_icon" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_toRightOf="@+id/text1"
android:background="#FF9900" />
</LinearLayout>
</LinearLayout>
解决方案
Just Modify your adapter class like this architecture i hope it works :the important lines are setting the tag and getting the tag.
public class MyAdapter extends ArrayAdapter<Model> {
private final List<Model> list;
private final Activity context;
boolean checkAll_flag = false;
boolean checkItem_flag = false;
public MyAdapter(Activity context, List<Model> list) {
super(context, R.layout.row, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.row, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.label);
viewHolder.checkbox = (CheckBox) convertView
.findViewById(R.id.check);
viewHolder.checkbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
int getPosition = (Integer) buttonView.getTag(); // Here
// we get the position that we have set for the checkbox using setTag.
list.get(getPosition).setSelected(
buttonView.isChecked()); // Set the value of
// checkbox to
// maintain its
// state.
}
});
convertView.setTag(viewHolder);
convertView.setTag(R.id.label, viewHolder.text);
convertView.setTag(R.id.check, viewHolder.checkbox);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.checkbox.setTag(position); // This line is important.
viewHolder.text.setText(list.get(position).getName());
viewHolder.checkbox.setChecked(list.get(position).isSelected());
return convertView;
}
}
这篇关于复选框歌厅滚动列表视图定制选中后...!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!