我想设置默认出现在左侧的指示器图标,应该出现在右侧。
我已经尝试了我在谷歌上找到的所有技术,但似乎我的可扩展 ListView 有问题,或者有其他方法可以做到这一点,
我试过了 :
explistobj.setInicatorBounds(...) 不起作用:(
在xml中添加属性
android:indicatorLeft="250dp"
android: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>

最佳答案

请试试这个,它在我的场景中有效。
这是可扩展 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);
}

它是可扩展 ListView 中正确指示器的完整代码。

现在有很好的库可用于相同的。如果您想使用 recyclerview 并想为此类事情使用自定义 View ,请使用还解释可扩展示例的 groupie 库。 groupie 。这个库需要android数据绑定(bind)。在 recyclerview 中处理多个 View ,并且还具有可扩展的功能。

关于android - 可扩展 ListView 指示器未设置在右侧,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22878813/

10-09 01:24