如何在所有组的第一个孩子上设置 ImageView.VISIBLE,其他 ChildViews 应该隐藏在 ExpandableListView
Posted
技术标签:
【中文标题】如何在所有组的第一个孩子上设置 ImageView.VISIBLE,其他 ChildViews 应该隐藏在 ExpandableListView【英文标题】:How to set the ImageView.VISIBLE on the first child of all the Groups and other ChildViews should be hidden In ExpandableListView 【发布时间】:2019-11-22 00:49:58 【问题描述】:实现了 ExpandableListview,我在 childView 上有 ImageView,它应该在组的第一个孩子上是 VISIBLE 并且对于其他 ChildView 应该是 Hidden 并且在重新加载 listView 后如何我持有 childView 的 Image 状态。
找到下面我实现的代码
filterChild.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:orientation="vertical">
<TextView
android:id="@+id/lblListItemFilter"
android:layout_
android:layout_
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:focusable="false"
android:descendantFocusability="blocksDescendants"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/check"
android:layout_
android:layout_
android:layout_gravity="left"
android:layout_marginEnd="16dp"
android:background="@drawable/checkmark"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
活动中的侦听器
expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener()
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id)
Toast.makeText(getApplicationContext(),
"Group Clicked " + listDataHeader.get(groupPosition),
Toast.LENGTH_SHORT).show();
return false;
);
expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener()
@Override
public void onGroupExpand(int groupPosition)
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
);
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener()
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id)
// TODO Auto-generated method stub
System.out.println("childPosition"+listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition));
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return true;
);
BaseExpandableListAdapter 类
package com.conduent.conduentebi;
import android.content.Context;
import android.graphics.Typeface;
import android.icu.util.ULocale;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;
public class FilterExpandableListAdapter extends BaseExpandableListAdapter
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
private HashMap<Integer, boolean[]> mChildCheckStates;
private List<ULocale.Category> mGroupCollection;
public FilterExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData)
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
mChildCheckStates = new HashMap<Integer, boolean[]>();
@Override
public Object getChild(int groupPosition, int childPosititon)
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
@Override
public long getChildId(int groupPosition, int childPosition)
return childPosition;
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent)
final int mGroupPosition = groupPosition;
final int mChildPosition = childPosition;
ChildViewHolder viewHolder = null;
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null)
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.filter_child, null);
System.out.println("mChildPosition"+mChildPosition);
viewHolder = new ChildViewHolder();
// viewHolder.checkBox1 = (CheckBox) convertView.findViewById(R.id.check);
// viewHolder.checkBox1.setFocusable(false);
viewHolder.mChildText = (TextView) convertView
.findViewById(R.id.lblListItemFilter);
//viewHolder.checkBox1 = (CheckBox)convertView.findViewById(R.id.check);
convertView.setTag(R.layout.filter_child, viewHolder);
//viewHolder.checkBox2 = (CheckBox)convertView.findViewById(R.id.check);
else
viewHolder = (ChildViewHolder) convertView
.getTag(R.layout.filter_child);
// TextView txtListChild = (TextView) convertView
// .findViewById(R.id.lblListItemFilter);
viewHolder.mChildText.setText(childText);
convertView.setClickable(false);
return convertView;
@Override
public int getChildrenCount(int groupPosition)
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
@Override
public Object getGroup(int groupPosition)
return this._listDataHeader.get(groupPosition);
@Override
public int getGroupCount()
return this._listDataHeader.size();
@Override
public long getGroupId(int groupPosition)
return groupPosition;
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null)
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.filter_group, null);
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeaderFilter);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
convertView.setClickable(false);
return convertView;
class ChildViewHolder
TextView mChildText;
ImageView checkBox1;
@Override
public boolean hasStableIds()
return false;
@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
return true;
【问题讨论】:
【参考方案1】:如果您想保持视图的旧状态并在重新加载屏幕时相应地显示视图,我建议您构建一个模型来保存您的数据并列出它。
对于任何更改,您都应该使用该模型的列表来更新数据。这将帮助您维护列表项中不同视图的状态。
【讨论】:
是的,我会这样做来保持视图的状态,我如何才能 imageView.VISIBLE 只显示第一个 childView 列表? 添加 if(position == 0) imageView.VISIBLE 否则 iamgeVIew.GONE/INVISIBLE 这只是我无法获得的职位,您能否详细说明我需要实施才能获得职位的答案 getChildView() 方法将为您提供 childPosition。 我已经在 getChildView 方法中设置了,第一组图像显示第一个 childView,第二组图像显示在第二个 childView 而不是第一个....以上是关于如何在所有组的第一个孩子上设置 ImageView.VISIBLE,其他 ChildViews 应该隐藏在 ExpandableListView的主要内容,如果未能解决你的问题,请参考以下文章
当我将 ImageView 的可见性设置为线性布局消失时,它的一个孩子仍然存在,另一个在 android 中消失了
如何跳过数组的第一个和最后一个元素的循环并将它们设置为常量值?