我创建自定义列表视图,并将复选框放入其中。
首先,我选中所有复选框,然后向下滚动列表视图,然后选中复选框取消选中。我该怎么办?
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>
答案 0 :(得分:5)
只需修改您的适配器类,就像这个架构我希望它可以工作: 重要的是设置标签和获取标签。
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;
}
}
答案 1 :(得分:0)
在ListView
中使用CheckBox
时,ListView
存在问题,Checked state
的{{1}}将无法维持CheckBox
}因为当你Android
时,它会使用Scroll ListView
方法为每一行生成新的View
,getView()
将删除所有上面Garbage Collector
以腾出空间{{1}状态也会被View
破坏或者垃圾。您可以通过以下示例解决此问题:
<强> main.xml中强>
CheckBox
<强> list_item.xml 强>
Garbage Collector
<强> Main.java 强>
<relativelayout
android:layout_height="match_parent"
android:layout_width="match_parent"
tools:context=".Main"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<listview
android:divider="#000"
android:dividerheight="0.8dp"
android:id="@+id/listView1"
android:layout_alignparentleft="true"
android:layout_alignparenttop="true"
android:layout_height="match_parent"
android:layout_width="match_parent">
</listview>
<button
android:id="@+id/button1"
android:layout_alignparenttop="true"
android:layout_centerhorizontal="true"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Check Selected">
</button>
</relativelayout>
<强> Listadapter.java 强>
<relativelayout
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<textview
android:id="@+id/textView1"
android:layout_alignparentleft="true"
android:layout_alignparenttop="true"
android:layout_height="wrap_content"
android:layout_marginbottom="10dp"
android:layout_margintop="10dp"
android:layout_width="wrap_content"
android:text="Medium Text"
android:textappearance="?android:attr/textAppearanceMedium">
</textview>
<checkbox
android:focusable="false"
android:focusableintouchmode="false"
android:id="@+id/checkBox1"
android:layout_alignbaseline="@+id/textView1"
android:layout_alignbottom="@+id/textView1"
android:layout_alignparentright="true"
android:layout_height="wrap_content"
android:layout_marginright="16dp"
android:layout_width="wrap_content">
</checkbox>
</relativelayout>
答案 2 :(得分:0)
为什么不使用setOnCheckedChangeListener()
来保持/保持每个头寸项目的检查状态?
请记住将setChecked()
放在setOnCheckedChangeListener()
之后,否则将触发先前的ViewHolder对象的setOnCheckedChangeListener()
,从而导致前一项未选中的问题。