可扩展列表视图将组图标指示器移动到右侧

exc*_*n01 63 android indicator expandablelistview

关于可扩展列表视图,组图标指示器位于左侧,我可以移动指示器并将其定位在右侧吗?谢谢.

编辑:由于我不想扩展视图,我得到了动态获取宽度的解决方法.只是分享我的解决方案

Display newDisplay = getWindowManager().getDefaultDisplay(); 
int width = newDisplay.getWidth();
newListView.setIndicatorBounds(width-50, width);

lub*_*ben 71

setIndicatorBounds(int,int)对Android 4.3无效.他们引入了一个新方法setIndicatorBoundsRelative(int,int),它适用于4.3.

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
       mExpandableListView.setIndicatorBounds(myLeft, myRight);
    } else {
       mExpandableListView.setIndicatorBoundsRelative(myLeft, myRight);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我疯了,为什么网上的每个人似乎都认为工作不起作用!这个答案肯定需要更多的可见性 (3认同)

var*_*waj 39

最好的方法是在下面但不适用于所有Android版本所以使用下面指定的第二或第三种方法

ViewTreeObserver vto = mExpandableListView.getViewTreeObserver();

    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
                 mExpandableListView.setIndicatorBounds(mExpandableListView.getRight()- 40, mExpandableListView.getWidth());
        }
    });
Run Code Online (Sandbox Code Playgroud)

或这个:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    mExpandableListView.setIndicatorBounds(mExpandableListView.getRight()- 40, mExpandableListView.getWidth());
} 
Run Code Online (Sandbox Code Playgroud)

即使在设备和选项卡上也会将该指示器移动到列表视图的右侧.

或者你可以在一个postdelayed线程中执行此操作.

 (new Handler()).post(new Runnable() {

        @Override
        public void run() {
              mExpandableListView.setIndicatorBounds(mExpandableListView.getRight()- 40, mExpandableListView.getWidth());
        }
    });
Run Code Online (Sandbox Code Playgroud)

  • 好方案!但是,**请注意**`setIndicatorBounds()`**设置左边界和右边界,而不是DIP.**你的答案不适用于具有不同屏幕密度的设备.确保[转换你的偏移](http://stackoverflow.com/a/9563438/886468)`40`从DP到像素_before_减去. (6认同)

goo*_*ode 39

所有XML解决方案! 我找到了解决这个问题的更好方法.这不要求你搞乱显示指标,不需要以编程方式破解它.

这是你需要做的:

1)将布局方向设置为从右到左

<ExpandableListView
...
android:layoutDirection="rtl" />
Run Code Online (Sandbox Code Playgroud)

2)确保您的列表项布局包含此内容(是的,您需要自定义布局):

android:gravity="left" //to adjust the text to align to the left again
android:layoutDirection="ltr"  //OR this line, based on your layout
Run Code Online (Sandbox Code Playgroud)

你很高兴去!

  • 它确实会影响子项,请确保也更新子项布局 (2认同)

Mic*_*ael 20

根据ExpandableListView源代码,将指标移动到右侧的唯一方法是使用ExpandableListView.setIndicatorBounds()方法更改其边界.onSizeChanged()例如,您可以计算方法中的绑定.


Vie*_*ila 11

我试图使用setIndicatorBounds和setIndicatorBoundsRelative,但图标显示不如预期的好.所以我按以下方式更改了我的代码:

创建一个组布局:

    <?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="48dp"
    android:background="@drawable/header_selector" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="25dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"
        android:contentDescription="@string/desc_list_item_icon"
        android:src="@drawable/ic_home" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_toRightOf="@id/icon"
        android:gravity="center_vertical"
        android:minHeight="?android:attr/listPreferredItemHeightSmall"
        android:paddingRight="40dp"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:textColor="@color/list_item_title" />

    <TextView
        android:id="@+id/counter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginRight="8dp"
        android:background="@drawable/counter_bg"
        android:textColor="@color/counter_text_color" />

    <ImageView
        android:id="@+id/icon_expand"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="200dp"
        android:layout_toRightOf="@+id/counter"
        android:contentDescription="@string/desc_list_item_icon"
        android:src="@drawable/navigation_expand"
        android:visibility="visible" />

    <ImageView
        android:id="@+id/icon_collapse"
        android:layout_width="25dp"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="200dp"
        android:layout_toRightOf="@+id/counter"
        android:contentDescription="@string/desc_list_item_icon"
        android:src="@drawable/navigation_collapse"
        android:visibility="gone" /> 

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

并在getGroupView方法中的适配器类中使用此布局:

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

        NavDrawerItem itemHeader = (NavDrawerItem) getGroup(groupPosition);

        LayoutInflater inflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View view = null;
        if (convertView == null) {
            view = (View) inflater.inflate(R.layout.drawer_header_item, null);
        } else {
            view = convertView;
        }

        ImageView icon = (ImageView) view.findViewById(R.id.icon);

        if (itemHeader.isIconVisible()) {
            icon.setImageResource(itemHeader.getIcon());
        } else {
            icon.setVisibility(View.GONE);
        }

        TextView textTitle = (TextView) view.findViewById(R.id.title);
        textTitle.setText(" " + itemHeader.getTitle());

        TextView textCount = (TextView) view.findViewById(R.id.counter);

        if (itemHeader.getCounterVisibility()) {
            textCount.setText(itemHeader.getCount());
        } else {
            textCount.setVisibility(View.GONE);
        }

        ImageView iconExpand = (ImageView) view.findViewById(R.id.icon_expand);
        ImageView iconCollapse = (ImageView) view
                .findViewById(R.id.icon_collapse);

        if (isExpanded) {
            iconExpand.setVisibility(View.GONE);
            iconCollapse.setVisibility(View.VISIBLE);
        } else {
            iconExpand.setVisibility(View.VISIBLE);
            iconCollapse.setVisibility(View.GONE);
        }

        if (getChildrenCount(groupPosition) == 0) {
            iconExpand.setVisibility(View.GONE);
            iconCollapse.setVisibility(View.GONE);
        }

        return view;
    }
Run Code Online (Sandbox Code Playgroud)

使用此方法,您可以正确调整展开/折叠图标的位置.希望能帮助到你.


Int*_*iya 5

可扩展列表视图,将组图标指示符向右移动

所述setIndicatorBounds(int left, int right)用于设置所述指示符界限为可扩展列表视图的组视图。

explvList.setIndicatorBounds(width-GetDipsFromPixel(35), width-GetDipsFromPixel(5));
Here width means device width.

/Convert pixel to dip 
public int GetDipsFromPixel(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);
} 
Run Code Online (Sandbox Code Playgroud)

仅供参考:宽度等于setBounds方法中指定的宽度。在我的代码段中是35。否则,该图标会受到干扰。有关更多详细信息,请访问此处: