关于可展开列表视图,组图标指示器位于左侧,我可以将指示器移动到右侧吗?谢谢。
已编辑:由于我不想扩展视图,因此我得到了动态获取宽度的解决方法。只是分享我的解决方案。
显示 newDisplay = getWindowManager().getDefaultDisplay(); int 宽度 = newDisplay.getWidth(); newListView.setIndicatorBounds(width-50, width);
关于可展开列表视图,组图标指示器位于左侧,我可以将指示器移动到右侧吗?谢谢。
已编辑:由于我不想扩展视图,因此我得到了动态获取宽度的解决方法。只是分享我的解决方案。
显示 newDisplay = getWindowManager().getDefaultDisplay(); int 宽度 = newDisplay.getWidth(); newListView.setIndicatorBounds(width-50, width);
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);
}
}
所有 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
你很高兴去!
执行此操作的最佳方法如下,但不适用于所有 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());
}
});
根据ExpandableListView
的源代码,将指标向右移动的唯一方法是使用ExpandableListView.setIndicatorBounds()
方法更改其边界。例如,您可以计算onSizeChanged()
方法中的界限。
我曾尝试使用 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;
}
使用此方法,您可以正确调整展开/折叠图标的位置。希望能帮助到你。
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);
}
仅供参考:宽度等于setBounds方法中指定的宽度。在我的代码片段中,它是 35。否则图标会受到干扰。有关更多详细信息,您可以访问这里:
在可扩展基础适配器中编写此代码。
@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());
}
});
它对我有用!干杯!
首先,您必须从 xml 中删除组指示符
<ExpandableListView
android:id="@+id/expandableListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/nav_header_height"
android:background="@android:color/white"
android:dividerHeight="0dp"
android:focusable="false"
android:groupIndicator="@null" />
然后在您的自定义适配器中执行以下操作
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);
}}
setIndicatorBounds(int left, int right)
用于设置 的组视图的指示器边界ExpandableListView
。
explvList.setIndicatorBounds(width-GetDipsFromPixel(35), width-GetDipsFromPixel(5));
这里的宽度是指设备宽度。
将像素转换为倾角:
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);
}
首先在您的可扩展列表视图中设置 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));
}
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);
}
这个对我有用
我正在使用受此示例启发的以下短片,该示例需要文件夹上有两个可绘制对象:
if (isExpanded) {
ListHeader.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.up, 0);
} else {
ListHeader.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.down, 0);
}
无论如何,我不得不采用goodKode的解决方案以及“摆脱”默认箭头。
因此,我建议坚持使用之前提供的简单解决方案,因为它是最简约和最精确的方法。
我知道为时已晚,但这是一个以编程方式将指标放在正确位置的解决方案,简单易用:
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);
}
结果是..