扩展列表视图指示器不会对右侧设置 [英] Expandable list view Indicator does not set on right side
问题描述
我想要设置来在默认情况下离开了我的指示图标,应该出现在右侧。 我已经尝试了所有的技巧我找到谷歌,但它似乎是有些事情是错我的expandablelistview或周围有一些其他的方式来做到这一点, 我试过了 : explistobj.setInicatorBounds(...)没有工作:( 在XML中添加属性 机器人:indicatorLeft =250dp 机器人:indicatorRight =300dp没有行为:(
code:
公共类MainActivity延伸活动{
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
名单<字符串> listDataHeader;
HashMap的<字符串列表与LT;字符串>> listDataChild;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
//获取列表视图
expListView =(ExpandableListView)findViewById(R.id.lvExp);
// preparing列表数据
prepareListData();
setDimens();
listAdapter =新ExpandableListAdapter(这一点,listDataHeader,listDataChild);
//设置列表适配器
expListView.setAdapter(listAdapter);
//列表视图组单击监听器
expListView.setOnGroupClickListener(新OnGroupClickListener(){
@覆盖
公共布尔onGroupClick(ExpandableListView父,视图V,
INT groupPosition,长ID){
// Toast.makeText(getApplicationContext(),
//集团已点击+ listDataHeader.get(groupPosition)
// Toast.LENGTH_SHORT).show();
返回false;
}
});
//列表视图集团扩大监听器
expListView.setOnGroupExpandListener(新OnGroupExpandListener(){
@覆盖
公共无效onGroupExpand(INT groupPosition){
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition)+膨胀,
Toast.LENGTH_SHORT).show();
}
});
//列表视图组collasped监听器
expListView.setOnGroupCollapseListener(新OnGroupCollapseListener(){
@覆盖
公共无效onGroupCollapse(INT groupPosition){
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition)+坍塌,
Toast.LENGTH_SHORT).show();
}
});
//列表视图儿童点击监听器
expListView.setOnChildClickListener(新OnChildClickListener(){
@覆盖
公共布尔onChildClick(ExpandableListView父,视图V,
INT groupPosition,诠释childPosition,长ID){
// TODO自动生成方法存根
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+:
+ listDataChild.get(
listDataHeader.get(groupPosition))。得到(
childPosition),Toast.LENGTH_SHORT)
。显示();
返回false;
}
});
}
/ *
* preparing列表数据
* /
私人无效prepareListData(){
listDataHeader =新的ArrayList<字符串>();
listDataChild =新的HashMap<字符串列表与LT;字符串>>();
//添加子数据
listDataHeader.add(mylist0);
listDataHeader.add(mylist1);
listDataHeader.add(mylist2);
//添加子数据
名单<字符串> TOP250 =新的ArrayList<字符串>();
mylist0.add(ITEM1);
mylist0.add(项目2);
mylist0.add(item3的);
名单<字符串> mylist1 =新的ArrayList<字符串>();
mylist1.add(项目1);
mylist1.add(项目2);
mylist1.add(项目3);
mylist1.add(ITEM4);
mylist1.add(Item5);
mylist1.add(Item6);
名单<字符串> mylist2 =新的ArrayList<字符串>();
mylist2.add(ITEM1);
listDataChild.put(listDataHeader.get(0),mylist0); //头,子数据
listDataChild.put(listDataHeader.get(1),mylist1);
listDataChild.put(listDataHeader.get(2),mylist2);
}
公众诠释GetDipsFromPixel(浮点像素)
{
//获取屏幕的密度天平
最终浮动规模= getResources()getDisplayMetrics()密度。
//将DPS像素,基于密度规模
返程(INT)(像素*规模+ 0.5F);
}
公共无效setDimens()
{
显示显示= getWindowManager()getDefaultDisplay()。
点大小=新的点();
display.getSize(大小);
INT宽度= size.x;
INT高= size.y;
显示DISPLAY2 = getWindowManager()getDefaultDisplay()。
INT宽2 = display2.getWidth(); //德precated
INT身高2 = display2.getHeight(); //德precated
//expListView.setIndicatorBounds(width2-GetDipsFromPixel(35),宽2-GetDipsFromPixel(5)); //没有行为
expListView.setIndicatorBounds(expListView.getRight() - 40,expListView.getWidth()); //没有行为
}
}
XML code:
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直
机器人:后台=#f4f4f4>
< ExpandableListView
机器人:ID =@ + ID / lvExp
机器人:layout_height =match_parent
机器人:layout_width =match_parent
机器人:cacheColorHint =#00000000
<! -
机器人:indicatorLeft =250dp
机器人:indicatorRight =300dp
- >
/>
< / LinearLayout中>
请试试这个,这是我的情况下工作。 这是可扩展列表视图的XML编码 layout.xml
< ExpandableListView
机器人:ID =@ + ID / expandableListView1
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentLeft =真
机器人:layout_marginTop =50dp
机器人:groupIndicator =@可绘制/ group_indicator
机器人:cacheColorHint =#00000000
机器人:childDivider =@机器人:彩色/透明
机器人:分隔=#d4d4d4
机器人:dividerHeight =1DP
机器人:滚动条=纵|横>
< / ExpandableListView>
group_indicator.xml
< XML版本=1.0编码=UTF-8&GT?;
<项目机器人:可绘制=@可绘制/ arrow_right机器人:state_empty =真正的>< /项目>
<项目机器人:可绘制=@可绘制/ arrow_down机器人:state_expanded =真正的>< /项目>
<项目机器人:可绘制=@可绘制/ arrow_right>< /项目>
然后code中的java文件。 expandableExample.jav
DisplayMetrics指标=新DisplayMetrics();
。getWindowManager()getDefaultDisplay()getMetrics(度量)。
INT宽度= metrics.widthPixels;
如果(android.os.Build.VERSION.SDK_INT< android.os.Build.VERSION_ codeS.JELLY_BEAN_MR2){
expandableListView.setIndicatorBounds(宽 - GetPixelFromDips(50),宽度 - GetPixelFromDips(10));
searchExpListView.setIndicatorBounds(宽 - GetPixelFromDips(50),宽度 - GetPixelFromDips(10));
} 其他 {
expandableListView.setIndicatorBoundsRelative(宽 - GetPixelFromDips(50),宽度 - GetPixelFromDips(10));
searchExpListView.setIndicatorBoundsRelative(宽 - GetPixelFromDips(50),宽度 - GetPixelFromDips(10));
}
公众诠释GetPixelFromDips(浮点像素){
//获取屏幕的密度天平
最终浮动规模= getResources()getDisplayMetrics()密度。
//将DPS像素,基于密度规模
返程(INT)(像素*规模+ 0.5F);
}
这是完整的code在扩展列表视图右侧的指示器。
I want to set my indicator icon that comes on left by default, should appear on right side. I had tried all the techniques i find on google but it seems like some thing is wrong with my expandablelistview or there is some other way around to do it, I tried : explistobj.setInicatorBounds(...) not works:( adding attribute in xml android:indicatorLeft="250dp" android:indicatorRight="300dp" not works:(
code:
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>
Please try this, it was work in my scenario. this is xml coding of expandable listview 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>
then code in java file. 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);
}
it's complete code for right indicator in expandable listview.
这篇关于扩展列表视图指示器不会对右侧设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!