具有指标图像的可扩展列表视图

时间:2017-04-14 21:11:20

标签: java android xml expandablelistview indicator

我有一个自定义的可扩展列表视图。 现在我想在每个组的左侧都有一个箭头。 我这样试过(请注意“getGroupView”部分):

 package com.aaron.waller.mrpolitik.tabs;

    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseExpandableListAdapter;
    import android.widget.ExpandableListView;
    import android.widget.ImageView;
    import android.widget.TextView;
    import android.widget.Toast;

    import com.aaron.waller.mrpolitik.R;

    /**
     * Created by Oclemmy on 5/10/2016 for ProgrammingWizards Channel and http://www.Camposha.com.
     */
    public class FragenFragment extends Fragment {
        View rootView;
        ExpandableListView lv;
        private String[] groups;
        private String[][] children;
        public ImageView groupholder;



        public FragenFragment() {

        }

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);


            groups = new String[]{"Ich habe einen Eiswürfel verschluckt und ihn noch nicht ausgekackt, steckt er fest?",
                    "Warum macht meine Katze komische Geräusche wenn ich micht auf sie stelle?", "Wird mein Laptop schwerer, wenn ich mehr Dateien draflade?",
                    "Warum vibriert meine Katze?",
                    "Platzhalterfrage","" +
                    "Platzhalterfrage",
                    "Platzhalterfrage",
                    "Platzhalterfrage",
                    "Platzhalterfrage",
                    "Platzhalterfrage",
                    "Platzhalterfrage",
                    "Platzhalterfrage",
                    "Platzhalterfrage",
                    "Platzhalterfrage"};

            children = new String[][]{
                    {"s simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."},
                    {"Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of comes from a line in section 1.10.32."},
                    {"It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like)."},
                    {"There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc."},
                    {"Platzhalterantwort"},
                    {"Platzhalterantwort"},
                    {"Platzhalterantwort"},
                    {"Platzhalterantwort"},
                    {"Platzhalterantwort"},
                    {"Platzhalterantwort"},
                    {"Platzhalterantwort"},
                    {"Platzhalterantwort"},
                    {"Platzhalterantwort"},
                    {"Platzhalterantwort"},

            };
        }

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragen_layout, container, false);

            return rootView;

        }

        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);

            lv = (ExpandableListView) view.findViewById(R.id.expListView);
            lv.setAdapter(new ExpandableListAdapter(groups, children));
            lv.setGroupIndicator(null);
            groupholder = (ImageView) view.findViewById(R.id.groupHolder);

    /*        DisplayMetrics metrics = new DisplayMetrics();
            ((MainActivity)getContext()).getWindowManager().getDefaultDisplay().getMetrics(metrics);
            int width = metrics.widthPixels;

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

        public class ExpandableListAdapter extends BaseExpandableListAdapter {

            private final LayoutInflater inf;
            private String[] groups;
            private String[][] children;

            public ExpandableListAdapter(String[] groups, String[][] children) {
                this.groups = groups;
                this.children = children;
                inf = LayoutInflater.from(getActivity());
            }

            @Override
            public int getGroupCount() {
                return groups.length;
            }

            @Override
            public int getChildrenCount(int groupPosition) {
                return children[groupPosition].length;
            }

            @Override
            public Object getGroup(int groupPosition) {
                return groups[groupPosition];
            }

            @Override
            public Object getChild(int groupPosition, int childPosition) {
                return children[groupPosition][childPosition];
            }

            @Override
            public long getGroupId(int groupPosition) {
                return groupPosition;
            }

            @Override
            public long getChildId(int groupPosition, int childPosition) {
                return childPosition;
            }

            @Override
            public boolean hasStableIds() {
                return true;
            }

            @Override
            public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

                ViewHolder holder;
                if (convertView == null) {
                    convertView = inf.inflate(R.layout.list_item, parent, false);
                    holder = new ViewHolder();

                    holder.text = (TextView) convertView.findViewById(R.id.lblListItem);
                    convertView.setTag(holder);


                } else {
                    holder = (ViewHolder) convertView.getTag();
                }



                holder.text.setText(getChild(groupPosition, childPosition).toString());

                return convertView;
            }

            @Override
            public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
                ViewHolder holder;





               if (convertView == null) {
                    convertView = inf.inflate(R.layout.list_group, parent, false);

                    holder = new ViewHolder();
                    holder.text = (TextView) convertView.findViewById(R.id.groupHolder1);
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();


                }

                    if (isExpanded) {
                        groupholder.setImageResource(R.drawable.arrow1);
                    } else {
                        groupholder.setImageResource(R.drawable.arrow2);
                    }

                }



                holder.text.setText(getGroup(groupPosition).toString());

                return convertView;



            }

            @Override
            public boolean isChildSelectable(int groupPosition, int childPosition) {
                return true;
            }

            private class ViewHolder {
                TextView text;
            }



        }

    }

但我不知道把这些行放在哪里:

if (isExpanded) {
            groupholder.setImageResource(R.drawable.arrow1);
        } else {
            groupholder.setImageResource(R.drawable.arrow2);
        }

如果我尝试使用可扩展列表视图打开片段,应用程序崩溃的那一刻。

这是我的可扩展列表视图:

<ExpandableListView
    android:layout_marginTop="45dp"
    android:id="@+id/expListView"
    android:layout_height="match_parent"
    android:groupIndicator="@null"
    android:layout_width="match_parent"/>

这是我的list_group.xml,我自定义了组设计:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >



    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="vertical" >


        <ImageView
            android:id="@+id/groupHolder"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/arrow2"
            android:layout_centerVertical="true"

            />

        <TextView
            android:id="@+id/groupHolder1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:paddingLeft="6dip"
            android:paddingTop="6dip"
            android:textColor="#282828"
            android:textAlignment="center"
            android:textSize="20dp"
            tools:ignore="SpUsage"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="10dp"/>

    </RelativeLayout>

</LinearLayout>

我的group_indicator.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:drawable="@drawable/arrow1"
        android:state_expanded="true" />

    <item
        android:drawable="@drawable/arrow2" />

</selector>

我如何重写它有效的代码? 我是Android Studio新手。

0 个答案:

没有答案