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

| 关于可扩展列表视图,组图标指示器位于左侧,我可以移动指示器并将其置于右侧吗?谢谢。 编辑:因为我不想扩展视图,所以我有了这种动态获取宽度的解决方法。只是分享我的解决方案。 显示newDisplay = getWindowManager()。getDefaultDisplay(); int width = newDisplay.getWidth(); newListView.setIndicatorBounds(width-50,width);     
已邀请:
根据ѭ0s的源代码,将指示器移到右侧的唯一方法是使用
ExpandableListView.setIndicatorBounds()
方法更改其范围。例如,您可以使用
onSizeChanged()
方法计算边界。     
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);
    }
}
    
最好的方法是在下面,但不适用于所有Android版本,因此请使用下面指定的第二种或第三种方法
ViewTreeObserver vto = mExpandableListView.getViewTreeObserver();

    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
                 mExpandableListView.setIndicatorBounds(mExpandableListView.getRight()- 40, mExpandableListView.getWidth());
        }
    });
或这个:
@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    mExpandableListView.setIndicatorBounds(mExpandableListView.getRight()- 40, mExpandableListView.getWidth());
} 
即使在设备和选项卡上,该指示器也会将其移动到列表视图的右侧。 或者您可以在后延迟线程中执行此操作。
 (new Handler()).post(new Runnable() {

        @Override
        public void run() {
              mExpandableListView.setIndicatorBounds(mExpandableListView.getRight()- 40, mExpandableListView.getWidth());
        }
    });
    
所有XML解决方案! 我找到了解决此问题的更好方法。这不需要您弄乱显示指标,也不需要以编程方式修改它。 这是您需要做的: 1)将布局方向设置为从右到左
<ExpandableListView
...
android:layoutDirection=\"rtl\" />
2)确保您的列表项布局包括以下内容(是的,您将需要自定义布局):
android:gravity=\"left\" //to adjust the text to align to the left again
android:layoutDirection=\"ltr\"  //OR this line, based on your layout
而且你很好走!     
我尝试使用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>
并在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;
    }
使用此方法,您可以正确调整展开/折叠图标的位置。 希望能帮助到你。     
可扩展列表视图,将组图标指示符向右移动 ѭ11用于设置可扩展列表视图的组视图的指标范围。
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);
} 
仅供参考:宽度等于setBounds方法中指定的宽度。在我的代码段中是35。否则,该图标会受到干扰。有关更多详细信息,请访问此处: https://developer.android.com/reference/android/widget/ExpandableListView.html#setIndicatorBounds(int,%20int) https://developer.android.com/reference/android/widget/ExpandableListView.html#setIndicatorBoundsRelative(int,%20int)     
在Expandable Base适配器中编写此代码。
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    ImageView imgs;

    String headerTitle = (String) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_group, null);
    }

    TextView lblListHeader = (TextView) convertView
            .findViewById(R.id.lblListHeader);
     imgs = (ImageView) convertView
            .findViewById(R.id.img_list);
    lblListHeader.setTypeface(null, Typeface.BOLD);
    lblListHeader.setText(headerTitle);
    if (isExpanded) {
  imgs.setImageResource(R.drawable.up_arrow);
    }
    else {
        imgs.setImageResource(R.drawable.downs_arrow);
    }

    return convertView;
}
项目清单     
FIX: 
他们在API 18及更高版本中引入了一种新方法,称为setIndicatorBoundsRelative(int,int)。 您应该检查Android版本,并将相应的方法与API配合使用:
expListView = (ExpandableListView) v.findViewById(R.id.laptop_list);

metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
width = metrics.widthPixels;

if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
    expListView.setIndicatorBounds(width - GetDipsFromPixel(50), width - GetDipsFromPixel(10));

} else {
    expListView.setIndicatorBoundsRelative(width - GetDipsFromPixel(50), width - GetDipsFromPixel(10));

}
    
如果它在片段中-onViewCreated()
    ViewTreeObserver vto = Expnlist.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new      ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Expnlist.setIndicatorBounds(Expnlist.getMeasuredWidth() - 80, Expnlist.getMeasuredWidth());
        }
    });
它对我有用! 干杯!     
setIndicatorBounds(int left, int right)
用于设置
ExpandableListView
的组视图的指标范围。
explvList.setIndicatorBounds(width-GetDipsFromPixel(35), width-GetDipsFromPixel(5));
此处的宽度表示设备的宽度。 转换像素为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);
} 
    
我使用的是受此示例启发的以下简短示例,该示例在文件夹上需要两个drawable:
if (isExpanded) {
    ListHeader.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.up, 0);
} else {
    ListHeader.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.down, 0);
} 
无论如何,我还必须采用goodKode的解决方案以及“摆脱”默认箭头。 因此,我建议您坚持使用以前提供的简单解决方案,因为它是最简单,最精确的方法。     
     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);
}
这个为我工作     
首先在您的可扩展列表视图中设置android:groupIndicator = \“ @ null \”,如下所示: 然后在您的标题布局中,如下所示:
<RelativeLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"
    android:layout_width=\"match_parent\"
    android:layout_height=\"wrap_content\"
    android:paddingLeft=\"@dimen/screen_margin\"
    android:paddingRight=\"@dimen/screen_margin\"
    android:paddingTop=\"@dimen/dot_margin\"
    android:paddingBottom=\"@dimen/dot_margin\"
    android:orientation=\"vertical\">




    <TextView
        android:id=\"@+id/tvQuestion\"
        android:layout_width=\"match_parent\"
        android:layout_height=\"wrap_content\"
        android:text=\"what is jodi?\"
        android:textColor=\"@color/textColor\"
        android:textSize=\"@dimen/font_large\"
        app:customFont=\"@string/font_toolbar\"
        android:layout_toLeftOf=\"@+id/imgDropDown\"
        android:layout_marginRight=\"@dimen/margin\"
       />


    <ImageView
        android:layout_width=\"wrap_content\"
        android:layout_height=\"wrap_content\"
        android:id=\"@+id/imgDropDown\"
        android:layout_alignParentRight=\"true\"
        android:layout_centerVertical=\"true\"
        android:src=\"@drawable/right_arrow\"/>

</RelativeLayout>
最后在getGroupView方法中的适配器中添加以下内容:
TextView lblListHeader = (TextView) convertView.findViewById(R.id.tvQuestion);

lblListHeader.setText(headerTitle);

ImageView img=convertView.findViewById(R.id.imgDropDown);

if (isExpanded) {
  img.setImageResource(R.drawable.down_arrow);
    lblListHeader.setTextColor(_context.getResources().getColor(R.color.colorPrimary));
} else {
 img.setImageResource(R.drawable.right_arrow);
    lblListHeader.setTextColor(_context.getResources().getColor(R.color.textColor));
}
    
我知道为时已晚,但是这里提供了一种解决方案,可以通过编程,简单易用的方式将指标正确放置:
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);
        }
expandableListView
是您的
ExpandableListview
    
尝试这个... 此代码用于将ExpandableList组指示器调整到视图的右侧。
private ExpandableListView expandableListView;
DisplayMetrics metrics;
int width;
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
width = metrics.widthPixels;
expandableListView = (ExpandableListView) findViewById(R.id.expandableListView1);
expandableListView.setIndicatorBounds(width - GetDipsFromPixel(50), width - GetDipsFromPixel(10));
并调用此方法,
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);
}
结果是..       

要回复问题请先登录注册