我为儿童用品设计了ExpandableListView
CheckedTextView
,但每件事情都很好但是
当我随机检查其他组中的CheckedTextView
随机CheckedTextView
时
check.this ExpandableListView
让我疯了。这是我的代码
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> expandableListTitle;
private HashMap<String, List<String>> expandableListDetail;
public int lastExpandedGroupPosition;
public ExpandableListAdapter(Context context, List<String> expandableListTitle,
HashMap<String, List<String>> expandableListDetail) {
this.context = context;
this.expandableListTitle = expandableListTitle;
this.expandableListDetail = expandableListDetail;
}
@Override
//counts the number of group/parent items so the list knows how many times calls getGroupView() method
public int getGroupCount() {
return this.expandableListTitle.size();
}
@Override
//counts the number of children items so the list knows how many times calls getChildView()
method
public int getChildrenCount(int listPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.size();
}
@Override
//gets the title of each parent/group
public Object getGroup(int listPosition) {
return this.expandableListTitle.get(listPosition);
}
@Override
//gets the name of each item
public Object getChild(int listPosition, int expandedListPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.get(expandedListPosition);
}
@Override
public long getGroupId(int listPosition) {
return listPosition;
}
@Override
public long getChildId(int listPosition, int expandedListPosition) {
return expandedListPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
//in this method you must set the text to see the parent/group on the list
public View getGroupView(int listPosition, boolean isExpanded, View convertView, ViewGroup
parent) {
String listTitle = (String) getGroup(listPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_group, null);
}
// set category name as tag so view can be found view later
convertView.setTag(getGroup(listPosition).toString());
TextView textView = (TextView) convertView.findViewById(R.id.listTitle);
//"i" is the position of the parent/group in the list
textView.setText(getGroup(listPosition).toString());
TextView sub = (TextView) convertView.findViewById(R.id.list_item_text_subscriptions);
return convertView;
}
@Override
//in this method you must set the text to see the children on the list
public View getChildView(int listPosition, int expandedListPosition, boolean isLastChild, View
convertView, ViewGroup parent) {
final String expandedListText = (String) getChild(listPosition, expandedListPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_item, null);
}
CheckedTextView textView = (CheckedTextView)
convertView.findViewById(R.id.list_item_text_child);
textView.setText(expandedListText);
return convertView;
}
@Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
return true;
}
}
这是来自json回复的数据
public class ExpandableListDataPump {
public static HashMap<String, List<String>> getData() {
ServiceHandler sh = new ServiceHandler();
String url = "http://****/**/fetch_details/services/news_keywords.json";
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.POST);
ArrayList<HashMap<String, String>> dataHashMap = new ArrayList<HashMap<String, String>>();
try {
JSONArray jsonArray = new JSONArray(jsonStr);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject c = jsonArray.getJSONObject(i);
HashMap<String, String> dataArray = new HashMap<String, String>();
if(!c.isNull("name")){
String region = c.getString("name");
String state = c.getString("ank_name");
dataArray.put("name", region);
dataArray.put("ank_name", state);
dataHashMap.add(dataArray);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>();
Set<String> regionsList = new HashSet<String>();
for (HashMap<String, String> map : dataHashMap) {
for (Entry<String, String> mapEntry : map.entrySet()) {
String key = mapEntry.getKey();
String value = mapEntry.getValue();
if (key.equalsIgnoreCase("name")) {
regionsList.add(value);
}
}
}
try {
JSONArray jsonArray = new JSONArray(jsonStr);
for (String regionName : regionsList) {
Log.e("Keywords list", regionName);
List<String> name = new ArrayList<String>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject c = jsonArray.getJSONObject(i);
String regionTemp = c.getString("name");
String nameTemp = c.getString("ank_name");
if (regionTemp.equalsIgnoreCase(regionName)) {
name.add(nameTemp);
}
}
expandableListDetail.put(regionName, name);
}
} catch (JSONException e) {
e.printStackTrace();
}
return expandableListDetail;
}
}
是list_group.xml就是这个
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/listTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:textColor="#A4C739"
android:paddingTop="10dp"
android:paddingBottom="10dp" />
</LinearLayout>
list_item.xml就是这个
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckedTextView
android:id="@+id/list_item_text_child"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:textSize="15sp"
android:checkMark="?android:attr/textCheckMark"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
/>
</LinearLayout>
答案 0 :(得分:0)
每当滚动ListView / ExpandableListView时,它都会调用&#34; getView&#34;滚动时相应适配器在运行时的功能。假设您在ListView中有10个项目,其中前5个项目当前正在屏幕上显示。此时,ListView已调用&#34; getView&#34;仅适用于当前显示的项目。现在,当您将列表向上滚动时,它将开始调用&#34; getView&#34;适配器逐一适用于第6,第7,第8 ......项目。
另一方面,CheckedTextView将其检查状态设置为默认值(如果未在布局XML中设置,则为FALSE)。所以每当ListView调用&#34; getView&#34;适配器,它为该项创建一个新的布局,并根据新的布局,CheckedTextView的检查状态返回到DEFAULT。
要处理此问题,您必须保存每个CheckedTextView的Checked-State,并将此值设置为CheckedTextView,同时从&#34; getView&#34;返回视图。适配器。
编辑:
在ExpandableListAdapter的构造函数中,使用默认的检查状态值填充以下数组列表...
ArrayList<ArrayList<Boolean>> checkStates = new ArrayList<ArrayList<Boolean>>();
public void MyExpendableListAdapter()
{
checkStates = new ArrayList<ArrayList<Boolean>>();
//
for(int i=0; i<numberOfGroups; i++) // Number of total groups or headers
{
ArrayList<Boolean> childrenCheckedStates = new ArrayList<Boolean>();
for(int j=0; j<groups.get(i).size(); j++) // saving checked state for each children in each group
{
childrenCheckedStates.add(defaultCheckState); // true or false
}
checkStates.add(childrenCheckedStates);
}
}
现在在getChildView ...
public View getChildView(final int listPosition, final int expandedListPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String expandedListText = (String) getChild(listPosition,
expandedListPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_item, null);
}
CheckedTextView textView = (CheckedTextView) convertView
.findViewById(R.id.list_item_text_child);
textView.setText(expandedListText);
textView.setChecked(checkStates.get(listPosition).get(expandedListPosition));
textView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
CheckedTextView tv = (CheckedTextView)v;
checkStates.get(listPosition).set(expandedListPosition, tv.isChecked());
}
});
return convertView;
}
祝你好运。 :)
答案 1 :(得分:0)
我遇到过这个问题。实际上我们必须管理它。
我使用了以下方法
将已检查的子索引添加到列表
在getchildView中,如果选中,请检查checkedtextview。
((CheckedTextView) convertView).setChecked(getClicked(
groupPosition, childPosition));
下面的函数将返回一个布尔值,无论是否检查给定的子项
public boolean getClicked(int groupPosition, int childPosition) {
if (checkedPositions.get(groupPosition) != null) {
boolean isChecked = checkedPositions.get(groupPosition).get(
childPosition);
return isChecked;
}
return false;
}
您可以使用这样的函数来索引已检查的项目
public void setClicked(int groupPosition, int childPosition) {
SparseBooleanArray checkedChildPositionsSingle = checkedPositions
.get(groupPosition);
if (checkedChildPositionsSingle == null) {
checkedChildPositionsSingle = new SparseBooleanArray();
checkedChildPositionsSingle.put(childPosition, true);
checkedPositions.put(groupPosition, checkedChildPositionsSingle);
} else {
boolean oldState = checkedChildPositionsSingle.get(childPosition);
if (!oldState) {
checkedChildPositionsSingle.clear();
checkedChildPositionsSingle.put(childPosition, !oldState);
}
}
notifyDataSetChanged();
}
补充:
@Override
//in this method you must set the text to see the children on the list
public View getChildView(int listPosition, int expandedListPosition, boolean isLastChild, View
convertView, ViewGroup parent) {
final String expandedListText = (String) getChild(listPosition, expandedListPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_item, null);
}
CheckedTextView textView = (CheckedTextView)
convertView.findViewById(R.id.list_item_text_child);
textView.setText(expandedListText);
((CheckedTextView) convertView).setChecked(getClicked(
groupPosition, childPosition)); //Added
return convertView;
}
在创建适配器的类中创建onChildClickListener
expandableListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent,
View clickedView, int groupPosition,
int childPosition, long id) {
expandableListAdapter.setClicked(groupPosition,childPosition);
}
}
注意:checkedPositions是SparseBooleanArray
的数组SparseArray<SparseBooleanArray> checkedPositions;