gpt4 book ai didi

android - 滚动自定义 ListView 后复选框未选中...!

转载 作者:太空狗 更新时间:2023-10-29 16:36:22 24 4
gpt4 key购买 nike

我创建自定义 ListView ,并将复选框放入其中。
首先我选中所有复选框,然后向下滚动 ListView ,然后复选框未选中。
我该怎么办?
自定义列表项.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);

}
}


列表项.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>

最佳答案

只需像这种架构一样修改您的适配器类,我希望它能工作:重要的行是设置标签和获取标签。

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;
}
}

关于android - 滚动自定义 ListView 后复选框未选中...!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27896997/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com