【发布时间】:2017-06-19 11:04:07
【问题描述】:
当我使用选中的复选框滚动 listView 时,当我向后滚动时它们未选中。我知道这是因为 Recycler View 但我不知道如何处理复选框状态。请在这个问题上帮助我。我通过查看不同的教程和视频尽力而为,但没有得到我需要的东西。我是android开发的新手。所有代码都在下面。请帮帮我。
Category.java
/**
* Created by Ali on 14-06-2017.
*/
public class Category {
private String categoryName;
private int imageResourceId;
private boolean isChecked;
public Category(String categoryName, int imageResourceId)
{
this.categoryName = categoryName;
this.imageResourceId = imageResourceId;
}
public Category(String categoryName, int imageResourceId, boolean isCheck)
{
this.categoryName = categoryName;
this.imageResourceId = imageResourceId;
this.isChecked = isCheck;
}
public void setCategoryName(String categoryName)
{
this.categoryName = categoryName;
}
public String getCategoryName()
{
return categoryName;
}
public void setImageResourceId(int imageResourceId)
{
this.imageResourceId = imageResourceId;
}
public int getImageResourceId()
{
return imageResourceId;
}
public boolean getIsChecked()
{
return this.isChecked;
}
public void setIsChecked(boolean value)
{
this.isChecked = value;
}
}
CategoryAdapter.java
import android.app.Activity;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Ali on 14-06-2017.
*/
public class CategoryAdapter extends ArrayAdapter<Category> {
private int imageResourceId;
public CategoryAdapter(Activity context, ArrayList<Category> categories, int newImageResourceId) {
super(context, 0, categories);
imageResourceId = newImageResourceId;
}
public CategoryAdapter(CategoryActivityScreen activityScreen, ArrayList<Category> categories) {
super(activityScreen, 0, categories);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Check if the existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
//Below code is before the extra code is written
// Get the {@link AndroidFlavor} object located at this position in the list
Category currentCategory = getItem(position);
ImageView imageView = (ImageView) listItemView.findViewById(R.id.image);
imageView.setImageResource(currentCategory.getImageResourceId());
imageView.setVisibility(View.VISIBLE);
TextView categoryTextView = (TextView) listItemView.findViewById(R.id.categoryNames);
// Get the version name from the current AndroidFlavor object and
// set this text on the name TextView
categoryTextView.setText(currentCategory.getCategoryName());
CheckBox checkBox = (CheckBox) listItemView.findViewById(R.id.checkbox);
//checkBox.setChecked(false);
checkBox.setChecked(currentCategory.getIsChecked());
return listItemView;
}
}
CategoryActivityScreen.java
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Ali on 14-06-2017.
*/
public class CategoryActivityScreen extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.category_list);
//Currently dummy list is populated
// Create a list of categories
final ArrayList<Category> categories = new ArrayList<Category>();
categories.add(new Category("Food", R.drawable.food_icon, false));
categories.add(new Category("Theater", R.drawable.theater_icon, false));
categories.add(new Category("Places", R.drawable.place_icon, false));
categories.add(new Category("Games", R.drawable.game_icon, false));
categories.add(new Category("Food", R.drawable.food_icon, false));
categories.add(new Category("Games", R.drawable.game_icon, false));
categories.add(new Category("Food", R.drawable.food_icon, false));
categories.add(new Category("Theater", R.drawable.theater_icon, false));
categories.add(new Category("Places", R.drawable.place_icon, false));
categories.add(new Category("Games", R.drawable.game_icon, false));
categories.add(new Category("Food", R.drawable.food_icon, false));
categories.add(new Category("Games", R.drawable.game_icon, false));
categories.add(new Category("Food", R.drawable.food_icon, false));
categories.add(new Category("Theater", R.drawable.theater_icon, false));
categories.add(new Category("Places", R.drawable.place_icon, false));
categories.add(new Category("Games", R.drawable.game_icon, false));
categories.add(new Category("Food", R.drawable.food_icon, false));
categories.add(new Category("Games", R.drawable.game_icon, false));
// Create an {@link WordAdapter}, whose data source is a list of {@link Word}s. The
// adapter knows how to create list items for each item in the list.
final CategoryAdapter adapter = new CategoryAdapter(this, categories);
// Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
// There should be a {@link ListView} with the view ID called list, which is declared in the
// word_list.xml layout file.
ListView listView = (ListView) findViewById(R.id.list);
//Code for adding footer to listView where list ends. Show's button at the end for next screen
//---Strats here-----
LayoutInflater inflater = getLayoutInflater();
ViewGroup footer = (ViewGroup) inflater.inflate(R.layout.footer_categories_button, listView, false);
listView.addFooterView(footer, null, true);
//---Ends here------
// Make the {@link ListView} use the {@link WordAdapter} we created above, so that the
// {@link ListView} will display list items for each {@link Word} in the list.
//// listView.setAdapter(adapter);
}
}
category_list.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal"
android:minHeight="60dp">
<ImageView
android:layout_width="88dp"
android:layout_height="88dp"
android:id="@+id/image" />
<LinearLayout
android:id="@+id/text_container"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:paddingLeft="16dp"
android:layout_toRightOf="@+id/image">
<TextView
android:id="@+id/categoryNames"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
tools:text="Food"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#000000"
android:textStyle="bold"
android:gravity="center" />
</LinearLayout>
<CheckBox
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="16dp"
android:id="@+id/checkbox"
/>
</RelativeLayout>
【问题讨论】:
-
您永远不会更新类别模型项中的“已检查”状态。您应该将侦听器附加到您的复选框并在“选中/未选中”事件上更新模型。此代码中也没有使用recyclerView,只有旧的“listview”。
-
@mhenryk 你的意思是我在成员布尔变量中将状态设置为“真”或“假”?
-
@mhenryk 如果可能,请告诉我在哪里进行更改。在哪个班级,否则
-
您必须使用
setOnCheckedChangeListener。示例setOnCheckedChangeListener. -
您的复选框需要 onCheckedChangeListener 或 OnClickedListener。在侦听器中,更新您的数据列表,然后调用 notifyDataSetChanged()。我有关于 ListView 的博客:programandroidlistview.blogspot.com。查看简单适配器的示例。希望有所帮助!
标签: android listview checkbox android-recyclerview android-arrayadapter