我想设置默认情况下左侧显示的指示器图标,应显示在右侧。 我曾尝试过在谷歌上找到的所有技术,但似乎我的expandablelistview有些问题或者有其他方法可以做到这一点, 我试过了 : explistobj.setInicatorBounds(...)不起作用:( 在xml中添加属性 机器人:indicatorLeft =" 250dp" 机器人:indicatorRight =" 300dp"不起作用:(
代码:
public class MainActivity extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);
// preparing list data
prepareListData();
setDimens();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
// Listview Group click listener
expListView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("mylist0");
listDataHeader.add("mylist1");
listDataHeader.add("mylist2");
// Adding child data
List<String> top250 = new ArrayList<String>();
mylist0.add("item1");
mylist0.add("item2");
mylist0.add("item3");
List<String> mylist1 = new ArrayList<String>();
mylist1.add("Item1");
mylist1.add("Item2");
mylist1.add("Item3");
mylist1.add("Item4");
mylist1.add("Item5");
mylist1.add("Item6");
List<String> mylist2 = new ArrayList<String>();
mylist2.add("item1");
listDataChild.put(listDataHeader.get(0), mylist0); // Header, Child data
listDataChild.put(listDataHeader.get(1), mylist1);
listDataChild.put(listDataHeader.get(2), mylist2);
}
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);
}
public void setDimens()
{
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Display display2 = getWindowManager().getDefaultDisplay();
int width2 = display2.getWidth(); // deprecated
int height2 = display2.getHeight(); // deprecated
//expListView.setIndicatorBounds(width2-GetDipsFromPixel(35), width2-GetDipsFromPixel(5)); //not works
expListView.setIndicatorBounds(expListView.getRight()- 40, expListView.getWidth()); //not works
}
}
xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#f4f4f4" >
<ExpandableListView
android:id="@+id/lvExp"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:cacheColorHint="#00000000"
<!--
android:indicatorLeft="250dp"
android:indicatorRight="300dp"
-->
/>
</LinearLayout>
答案 0 :(得分:17)
请尝试这个,它在我的场景中起作用。 这是可扩展listview的xml编码 的 layout.xml 强>
<ExpandableListView
android:id="@+id/expandableListView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="50dp"
android:groupIndicator="@drawable/group_indicator"
android:cacheColorHint="#00000000"
android:childDivider="@android:color/transparent"
android:divider="#d4d4d4"
android:dividerHeight="1dp"
android:scrollbars="vertical|horizontal" >
</ExpandableListView>
<强> group_indicator.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<item android:drawable="@drawable/arrow_right" android:state_empty="true"></item>
<item android:drawable="@drawable/arrow_down" android:state_expanded="true"></item>
<item android:drawable="@drawable/arrow_right"></item>
然后在java文件中编码。 的 expandableExample.jav 强>
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
expandableListView.setIndicatorBounds(width - GetPixelFromDips(50), width - GetPixelFromDips(10));
searchExpListView.setIndicatorBounds(width - GetPixelFromDips(50), width - GetPixelFromDips(10));
} else {
expandableListView.setIndicatorBoundsRelative(width - GetPixelFromDips(50), width - GetPixelFromDips(10));
searchExpListView.setIndicatorBoundsRelative(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);
}
它是可扩展列表视图中右侧指示器的完整代码。
现在有可用的精美图书馆。如果您想使用recyclerview并希望使用自定义视图,请使用也解释可扩展示例的groupie库。 groupie。这个库需要android数据绑定。在recyclerview中处理多个视图,并具有可扩展功能。