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

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

编辑:由于我不想扩大视图,我得到这个dynamic获取宽度的解决方法。 只是分享我的解决scheme

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

根据ExpandableListView的源代码,将指标移动到右侧的唯一方法是使用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()); } 

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

或者你可以在postdelayed线程中做到这一点。

  (new Handler()).post(new Runnable() { @Override public void run() { mExpandableListView.setIndicatorBounds(mExpandableListView.getRight()- 40, mExpandableListView.getWidth()); } }); 

全部XML解决scheme! 我find了解决这个问题的更好方法。 这并不要求您混淆展示指标,也不需要以编程方式对其进行攻击。

这是你需要做的:

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,但图标不像预期的那么好。 所以我改变我的代码,如下所示:

创build一个组布局:

  <?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; } 

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

Expandable list view move group icon indicator to right

setIndicatorBounds(int left, int right)用于设置expandable列表视图的组视图的指标边界。

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

这里宽度意味着设备宽度。

 /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); } 

FYI :宽度等于setBounds method指定的宽度。 在这里,我的片段是35,否则聪明的图标是不安的。 欲了解更多详情,你可以访问这里

https://developer.android.com/reference/android/widget/ExpandableListView.html#setIndicatorBounds(int,int

https://developer.android.com/reference/android/widget/ExpandableListView.html#setIndicatorBoundsRelative(int,int

 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()); } }); 

它的作品给我! 干杯!

将此代码写入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; } 
  • 项目清单

find这个方法来获得右侧的指标。

 <ExpandableListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/expandableListView" android:indicatorLeft="320dp" /> 

setIndicatorBounds(int left, int right)用于设置ExpandableListView的组视图的指标边界。

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

这里宽度意味着设备宽度。

将像素转换为倾angular:

 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); } 

试试这个…这个代码将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); } 

结果是.. ExpandableList右侧组指标