Android 当我在检查checkedtextview后滚动时,checkedtextview会被随机检查和取消检查
我为子项设计了一个Android 当我在检查checkedtextview后滚动时,checkedtextview会被随机检查和取消检查,android,expandablelistview,checkedtextview,Android,Expandablelistview,Checkedtextview,我为子项设计了一个ExpandableListView和CheckedTextView,一切都很好,但是 当我随机检查另一组中的CheckedTextView时CheckedTextView 检查.thisExpandableListView让我发疯。这是我的密码 public class ExpandableListAdapter extends BaseExpandableListAdapter { private Context context; private List<S
ExpandableListView
和CheckedTextView
,一切都很好,但是当我随机检查另一组中的
CheckedTextView
时CheckedTextView
检查.thisExpandableListView
让我发疯。这是我的密码
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;
}
}
公共类ExpandableListAdapter扩展了BaseExpandableListAdapter{
私人语境;
私有列表expandableListTitle;
私有HashMap expandableListDetail;
公共内部职位;
公共ExpandableListAdapter(上下文,列表expandableListTitle,
HashMap expandableListDetail){
this.context=上下文;
this.expandableListTitle=expandableListTitle;
this.expandableListDetail=expandableListDetail;
}
@凌驾
//统计组/父项的数量,以便列表知道调用getGroupView()方法的次数
public int getGroupCount(){
返回此.expandableListTitle.size();
}
@凌驾
//统计子项的数量,以便列表知道调用getChildView()的次数
方法
公共int getChildrenCount(int listPosition){
返回this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.size();
}
@凌驾
//获取每个父级/组的标题
公共对象getGroup(int listPosition){
返回此.expandableListTitle.get(listPosition);
}
@凌驾
//获取每个项的名称
公共对象getChild(int listPosition,int expandedListPosition){
返回this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.get(expandedListPosition);
}
@凌驾
公共长getGroupId(int-listPosition){
返回列表位置;
}
@凌驾
公共长getChildId(int-listPosition、int-expandedListPosition){
返回expandedListPosition;
}
@凌驾
公共布尔表ID(){
返回true;
}
@凌驾
//在此方法中,必须设置文本以查看列表上的父/组
公共视图getGroupView(int listPosition、布尔值isExpanded、视图convertView、视图组
(家长){
字符串listTitle=(字符串)getGroup(listPosition);
if(convertView==null){
LayoutInflater LayoutInflater=(LayoutInflater)this.context。
getSystemService(上下文布局\充气机\服务);
convertView=LayoutFlater.充气(R.layout.list\u组,空);
}
//将类别名称设置为标记,以便以后可以在视图中找到视图
setTag(getGroup(listPosition.toString());
TextView TextView=(TextView)convertView.findViewById(R.id.listTitle);
//“i”是父/组在列表中的位置
setText(getGroup(listPosition.toString());
TextView sub=(TextView)convertView.findViewById(R.id.list\u item\u text\u订阅);
返回视图;
}
@凌驾
//在此方法中,必须设置文本以查看列表中的子项
公共视图getChildView(int-listPosition,int-expandedListPosition,boolean-isLastChild,视图
convertView,视图组父级){
最终字符串expandedListText=(字符串)getChild(listPosition,expandedListPosition);
if(convertView==null){
LayoutInflater LayoutInflater=(LayoutInflater)this.context
.getSystemService(上下文布局\充气机\服务);
convertView=LayoutFlater.充气(R.layout.list_项,空);
}
CheckedTextView textView=(CheckedTextView)
convertView.findViewById(R.id.list\u item\u text\u child);
textView.setText(expandedListText);
返回视图;
}
@凌驾
公共布尔值isChildSelectable(int listPosition,int expandedListPosition){
返回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;
}
}
公共类ExpandableListDataPump{
公共静态HashMap getData(){
ServiceHandler sh=新的ServiceHandler();
String url=“http://**/**/fetch\u details/services/news\u keywords.json”;
//向url发出请求并获得响应
字符串jsonStr=sh.makeServiceCall(url,ServiceHandler.POST);
ArrayList dataHashMap=新的ArrayList();
试一试{
JSONArray JSONArray=新JSONArray(jsonStr);
for(int i=0;i
Yes 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>
<?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>
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);
}
}
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;
}
((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;
}
expandableListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent,
View clickedView, int groupPosition,
int childPosition, long id) {
expandableListAdapter.setClicked(groupPosition,childPosition);
}
}
SparseArray<SparseBooleanArray> checkedPositions;