在Android中将ExpandableListView的指示器放在右侧

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21706231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:57:18  来源:igfitidea点击:

Put on the right the indicator of an ExpandableListView in Android

androidandroid-fragmentsexpandablelistview

提问by Filnik

example

例子

I have to move the indicator from the left to the right (because of the plane image). I couldn't succeed also because the expandableviewlist is inside a fragment and not inside a whole activity. Any idea? Thanks!

我必须将指示器从左向右移动(因为平面图像)。我也无法成功,因为 expandableviewlist 位于片段内,而不是整个活动内。任何的想法?谢谢!

回答by Ahmed Zayed

I don't know a way to do that from XML but i'll tell you a way to do so dynamically in your adapter.

我不知道从 XML 中做到这一点的方法,但我会告诉您一种在您的适配器中动态执行此操作的方法。

First you have to remove group indicator from your xml

首先,您必须从 xml 中删除组指示符

 <ExpandableListView [...] 
    android:groupIndicator="@null" />

Then in your layout of the parent add an imageview in the right position of your layout.

然后在您的父布局中,在布局的正确位置添加一个图像视图。

Then in your custom adapter do the following

然后在您的自定义适配器中执行以下操作

public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
  ...

    if (isExpanded) {
        groupHolder.img.setImageResource(R.drawable.group_down);
    } else {
        groupHolder.img.setImageResource(R.drawable.group_up);
    }
  ...

}

回答by Rajeev Sahu

One more solution is: 1) First set groupIndicator in your ExpandableListView to @null:

另一种解决方案是:1)首先将您的 ExpandableListView 中的 groupIndicator 设置为@null:

<ExpandableListView [...] 
    android:groupIndicator="@null" />

2) Then create group_indicator.xml file with following details:

2) 然后创建 group_indicator.xml 文件,其中包含以下详细信息:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/down_icon" android:state_selected="false"></item>
    <item android:drawable="@drawable/up_icon" android:state_selected="true"></item>
    <item android:drawable="@drawable/down_icon"></item>
</selector>

3) Then create group_header.xml layout with following details and inflate this layout in getGroupView() method of ExpandableListAdapter.java:

3) 然后使用以下详细信息创建 group_header.xml 布局,并在 ExpandableListAdapter.java 的 getGroupView() 方法中扩展此布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp">

    <TextView
        android:id="@+id/tvHeader"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:textColor="@color/white"
        android:layout_centerVertical="true"
        android:textSize="16sp"/>

    <ImageView
        android:id="@+id/ivGroupIndicator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/group_indicator"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"/>
</RelativeLayout>

3) In getGroupView() method of your ExpandableListAdapter.java class, just set the following:

3) 在 ExpandableListAdapter.java 类的 getGroupView() 方法中,只需设置以下内容:

ivGroupIndicator.setSelected(isExpanded);

With this approach, your down_icon and up_icon will work properly. Hope this helps.

使用这种方法,您的 down_icon 和 up_icon 将正常工作。希望这可以帮助。

回答by Badr At

another solution to put the indicator on the right programmatically:

以编程方式将指标放在右侧的另一种解决方案:

expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);

        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int width = size.x;
        Resources r = getResources();
        int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                50, r.getDisplayMetrics());
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
            expandableListView.setIndicatorBounds(width - px, width);
        } else {
            expandableListView.setIndicatorBoundsRelative(width - px, width);
        }

where expandableListViewis your ExpandableListview

expandableListView你在哪里ExpandableListview

回答by maghfirzakir

put this into your xml view:

把它放到你的 xml 视图中:

<ExpandableListView
...
android:layoutDirection="rtl" />

than you can set the gravity of your text title in your layout item according your preference.

您可以根据自己的喜好在布局项中设置文本标题的重力。

your text view parent list item:

您的文本视图父列表项:

<TextView
...
android:gravity="center"/>

eg. result:

例如。结果:

enter image description here

在此处输入图片说明

回答by Mehul Ranpara

In your groupcustom.xml file you can use Relativelayout and put that image to

在您的 groupcustom.xml 文件中,您可以使用 Relativelayout 并将该图像放入

android:alignParentRight = "true";