更改 ExpandableListView 中的可扩展指示器
Posted
技术标签:
【中文标题】更改 ExpandableListView 中的可扩展指示器【英文标题】:Change expandable indicator in ExpandableListView 【发布时间】:2013-03-08 05:46:54 【问题描述】:尝试创建 ExpandableListView。组的初始视图显示良好。但是,当我单击列表项时,我的箭头不会改变。请参阅下面的图片。
如何改变箭头的方向?
布局 XML:
<ExpandableListView
android:id="@+id/expandable_list"
android:layout_
android:layout_
android:divider="@null"
android:background="#ffffff"
android:groupIndicator="@drawable/settings_selector"
android:transcriptMode="alwaysScroll" />
settings_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/arrow_down"
android:state_empty="true"/>
<item
android:drawable="@drawable/arrow_right"
android:state_expanded="true"/>
</selector>
</animation-list>
【问题讨论】:
发布listView行xml @RajeshCP 查看更新一 onItemClick 函数的 Onclick 您可以更改 group_indicator 的来源,因为您需要一个向上箭头按钮,或者您可以将位图旋转一些粗略并将其设置为该 ImageView 的来源 @RajeshCP 在左侧箭头工作得很好用这个hrupin.com/2012/08/… 一个但是如何解决右侧箭头 相应地编辑您的布局 【参考方案1】:可扩展的列表视图
<ExpandableListView
android:id="@+id/expandable_list"
android:layout_
android:layout_
android:groupIndicator="@drawable/group_indicator"
android:transcriptMode="alwaysScroll" />
setindicator
这里我正在使用这样的 setindicator 代码,这很好用
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
mExpandableList = (ExpandableListView)findViewById(R.id.expandable_list);
mExpandableList.setIndicatorBounds(width - GetPixelFromDips(50), width - GetPixelFromDips(10));
public int GetPixelFromDips(float pixels)
// Get the screen's density scale
final float scale = getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale
return (int) (pixels * scale + 0.5f);
res/drawable/group_indicator
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/arrow_right" android:state_empty="true"> </item>
<item android:drawable="@drawable/arrow_down" android:state_expanded="true"></item>
<item android:drawable="@drawable/arrow_right"></item>
</selector>
【讨论】:
mExpandableList.setIndicatorBounds(width - 50, width - 10);
给出相同的输出.. 为什么?????
在 android 4.3 中使用 expListView.setIndicatorBoundsRelative
您可以在dimens.xml中保存指标大小:试试你的settings_selector.xml
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/arrow_right"
android:state_expanded="true" />
<item
android:drawable="@drawable/arrow_down" />
</selector>
【讨论】:
当我发布我的答案时你没有发布你的答案:) 因为它对任何新开发人员都很有用。【参考方案3】:我采取了以下方式:根据 isExpanded 标志为您的 groupView 决定左/右可绘制对象。 这样,我们就更容易自定义指标drawable的padding/background等东西了。
希望对你有帮助。
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent)
TextView textView = (TextView) mLayoutInflater.inflate(R.layout.menu_group, null);
textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, isExpanded ? 0 : android.R.drawable.ic_menu_more, 0);
textView.setText(getGroup(groupPosition).toString());
return textView;
【讨论】:
【参考方案4】:import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
public class MyActivity extends Activity
private ExpandableListView mExpandableList;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
mExpandableList = (ExpandableListView)findViewById(R.id.expandable_list);
mExpandableList.setGroupIndicator(null);
ArrayList<Parent> arrayParents = new ArrayList<Parent>();
ArrayList<String> arrayChildren = new ArrayList<String>();
//here we set the parents and the children
for (int i = 0; i < 10; i++)
//for each "i" create a new Parent object to set the title and the children
Parent parent = new Parent();
parent.setTitle("Parent " + i);
arrayChildren = new ArrayList<String>();
for (int j = 0; j < 10; j++)
arrayChildren.add("Child " + j);
parent.setArrayChildren(arrayChildren);
//in this array we add the Parent object. We will use the arrayParents at the setAdapter
arrayParents.add(parent);
//sets the adapter that provides data to the list.
mExpandableList.setAdapter(new MyCustomAdapter(MyActivity.this,arrayParents));
public class Parent
private String mTitle;
private ArrayList<String> mArrayChildren;
public String getTitle()
return mTitle;
public void setTitle(String mTitle)
this.mTitle = mTitle;
public ArrayList<String> getArrayChildren()
return mArrayChildren;
public void setArrayChildren(ArrayList<String> mArrayChildren)
this.mArrayChildren = mArrayChildren;
public class MyCustomAdapter extends BaseExpandableListAdapter implements OnClickListener
private LayoutInflater inflater;
private ArrayList<Parent> mParent;
public MyCustomAdapter(Context context, ArrayList<Parent> parent)
mParent = parent;
inflater = LayoutInflater.from(context);
@Override
//counts the number of group/parent items so the list knows how many times calls getGroupView() method
public int getGroupCount()
return mParent.size();
@Override
//counts the number of children items so the list knows how many times calls getChildView() method
public int getChildrenCount(int i)
return mParent.get(i).getArrayChildren().size();
@Override
//gets the title of each parent/group
public Object getGroup(int i)
return mParent.get(i).getTitle();
@Override
//gets the name of each item
public Object getChild(int i, int i1)
return mParent.get(i).getArrayChildren().get(i1);
@Override
public long getGroupId(int i)
return i;
@Override
public long getChildId(int i, int i1)
return i1;
@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 i, boolean b, View view, ViewGroup viewGroup)
if (view == null)
view = inflater.inflate(R.layout.list_item_parent, viewGroup,false);
view.findViewById(R.id.button).setTag(i);
view.findViewById(R.id.button).setOnClickListener(this);
TextView textView = (TextView) view.findViewById(R.id.list_item_text_view);
//"i" is the position of the parent/group in the list
textView.setText(getGroup(i).toString());
//return the entire view
return view;
@Override
//in this method you must set the text to see the children on the list
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup)
if (view == null)
view = inflater.inflate(R.layout.list_item_child, viewGroup,false);
TextView textView = (TextView) view.findViewById(R.id.list_item_text_child);
//"i" is the position of the parent/group in the list and
//"i1" is the position of the child
textView.setText(mParent.get(i).getArrayChildren().get(i1));
//return the entire view
return view;
@Override
public boolean isChildSelectable(int i, int i1)
return true;
@Override
public void registerDataSetObserver(DataSetObserver observer)
/* used to make the notifyDataSetChanged() method work */
super.registerDataSetObserver(observer);
/* (non-Javadoc)
* @see android.view.View.OnClickListener#onClick(android.view.View)
* @since Mar 20, 2013
* @author rajeshcp
*/
@Override
public void onClick(View v)
if(mExpandableList.isGroupExpanded((Integer)v.getTag()))
mExpandableList.collapseGroup((Integer)v.getTag());
else
mExpandableList.expandGroup((Integer)v.getTag());
像这样更改您的MyActivity
,让我知道您还想要什么?
【讨论】:
在此没有onitemclick
在可扩展列表视图中使用 onitemclick
所必需的。
我真的不明白你想要什么?
见hrupin.com/2012/08/…这个例子代替左侧图像如何将该图像更改为右侧。
我正在使用该示例,只是该图像如何像从左到右一样改变,就像我的图像有问题一样有一个这样的图像..
嘿,运行应用程序后看到编辑后的答案,只需单击行中的每个按钮【参考方案5】:
主题:
int width = getResources().getDisplayMetrics().widthPixels;
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2)
listView.setIndicatorBounds(width - getPixelValue(40), width - getPixelValue(10));
else
listView.setIndicatorBoundsRelative(width - getPixelValue(40), width - getPixelValue(10));
和辅助方法:
public static int getPixelValue(int dp)
final float scale = getResources().getDisplayMetrics().density;
return (int) (dp * scale + 0.5f);
【讨论】:
【参考方案6】:只需在组项示例的 xml 中创建您想要的视图/图像视图:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parent_group"
android:layout_
android:layout_
android:orientation="horizontal"
android:padding="10dp">
<ImageView
android:id="@+id/expandable_icon"
android:layout_
android:layout_
android:layout_marginTop="6dp"
android:src="@drawable/group_icon_not_expanded" />
<TextView
android:id="@+id/group_name"
android:layout_
android:layout_
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:fontFamily="@font/roboto_thin"
android:textColor="@android:color/black"
android:textSize="17sp" />
然后为您的 ExpandableListView 使用 GroupClickListener 以编程方式更改 ImageView 的图像,例如:
listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener()
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id)
parent.smoothScrollToPosition(groupPosition);
if (parent.isGroupExpanded(groupPosition))
ImageView imageView = v.findViewById(R.id.expandable_icon);
imageView.setImageDrawable(getResources().getDrawable(R.drawable.group_icon_not_expanded));
else
ImageView imageView = v.findViewById(R.id.expandable_icon);
imageView.setImageDrawable(getResources().getDrawable(R.drawable.group_icon_expanded));
return false ;
);
【讨论】:
【参考方案7】:我参加聚会有点晚了,但我的要求与此类似,但有所不同。
我有 3 个要求:
-
如果一个组有孩子,则显示 down_arrow (V)
如果一个组没有孩子,则没有箭头/图像
如果扩展了包含子项的组,则显示向上箭头 (^)
要达到类似的效果:
您的ExpandableListView
看起来与android:groupIndicator
null 类似
<ExpandableListView
android:id="@+id/expandableListView"
android:layout_
android:layout_
android:divider="@android:color/darker_gray"
android:dividerHeight="0.5dp"
android:groupIndicator="@null" />
您的组/标题布局将如下所示,最后带有文本和图像:
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/ivDrawerHeaderItemIcon"
android:layout_
android:layout_
android:layout_gravity="center_vertical"
android:layout_marginStart="@dimen/dp_10"
android:layout_marginEnd="@dimen/dp_10"
android:src="@drawable/switch" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/lblListHeader"
android:layout_
android:layout_
android:layout_margin="@dimen/dp_5"
android:layout_weight="1"
android:fontFamily="@font/roboto_condensed_regular"
android:gravity="center_vertical"
android:padding="@dimen/dp_10"
android:textAllCaps="true"
android:textColor="@android:color/white"
android:textSize="@dimen/sp_16" />
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/ivIcon"
android:layout_
android:layout_
android:layout_gravity="center_vertical"
android:layout_marginStart="@dimen/dp_10"
android:layout_marginEnd="@dimen/dp_10" />
在您的BaseExpandableListAdapter
的getGroupView()
方法中,使用
if (Objects.requireNonNull(expandableListDetail.get(expandableListTitle.get(listPosition))).size() > 0)
if (isExpanded)
ivIcon.setImageResource(R.drawable.arrow_up);
else
ivIcon.setImageResource(R.drawable.arrow_down);
其中,expandableListTitle
是组,expandableListDetail
是其子项
【讨论】:
以上是关于更改 ExpandableListView 中的可扩展指示器的主要内容,如果未能解决你的问题,请参考以下文章
以 ViewPager 作为子元素的 ExpandableListView 执行 getChildView 两次
为 expandablelistview 的子级创建单独的 onClick() 方法