繁体   English   中英

ExpandableListView没有展开

[英]ExpandableListView is not expanding

我在Fragment创建了ExpandableList视图,列表中显示了组项目,但是当我触摸该组项目时,它并没有扩展。

以下是我的片段

public class InstituteFragment extends Fragment {
    ExpandableListView expandableListView;
    ArrayList<String> subjectName;
    HashMap<String,ArrayList<String>> topicName;
    public InstituteFragment() {

    }

    public static InstituteFragment newInstance(){
        InstituteFragment fragment = new InstituteFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        subjectName = new ArrayList<>();
        topicName = new HashMap<>();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.fragment_institute, container, false);
        prepData();
        expandableListView = (ExpandableListView)view.findViewById(R.id.expd_list_view);
        SubjectAdapter adapter = new SubjectAdapter(getActivity(),subjectName,topicName);
        expandableListView.setAdapter(adapter);
        return view;
    }


    private void prepData() {

        subjectName.add("Physics");
        subjectName.add("Chemistry");
        subjectName.add("Biology");

        ArrayList<String> physics = new ArrayList<>();
        physics.add("Demo");
        physics.add("Demo1");
        physics.add("Demo2");
        physics.add("Demo3");

        ArrayList<String> chemistry = new ArrayList<>();
        chemistry.add("Demo");
        chemistry.add("Demo1");
        chemistry.add("Demo2");
        chemistry.add("Demo3");

        ArrayList<String> biology = new ArrayList<>();
        biology.add("Demo");
        biology.add("Demo1");
        biology.add("Demo2");
        biology.add("Demo3");

        topicName.put(subjectName.get(0), physics);
        topicName.put(subjectName.get(1),chemistry);
        topicName.put(subjectName.get(2),biology);


    }


}

下面是适配器

public class SubjectAdapter extends BaseExpandableListAdapter {
    Context context;
    ArrayList<String> subjectName;
    HashMap<String,ArrayList<String>> topicName;

    public SubjectAdapter(Context context, ArrayList<String> subjectName, HashMap<String, ArrayList<String>> topicName) {
        this.context = context;
        this.topicName = topicName;
        this.subjectName = subjectName;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return this.topicName.get(this.subjectName.get(groupPosition))
                .get(childPosition);
    }

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



    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final String childText = (String) getChild(groupPosition, childPosition);

        LayoutInflater infalInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.chapter_name, null);

        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.chapter_name);

        txtListChild.setText(childText);
        return convertView;
    }


    @Override
    public int getChildrenCount(int groupPosition) {
        return this.topicName.get(this.subjectName.get(groupPosition)).size();
    }

    @Override
    public int getGroupCount() {
        return this.subjectName.size();
    }


    @Override
    public Object getGroup(int groupPosition) {
        return this.subjectName.get(groupPosition);
    }



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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        String subjectName = (String)getGroup(groupPosition);
        LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.subject_name,null);
        TextView textView = (TextView)convertView.findViewById(R.id.subject_name);
        textView.setText(subjectName);
        return convertView;
    }


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





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

下面是Institute_fragment.xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Fragment.InstituteFragment">


    <ExpandableListView
        android:id="@+id/expd_list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ExpandableListView>

</RelativeLayout>

以下是我的群组视图

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="@dimen/_10sdp"
    android:layout_margin="@dimen/_5sdp"
    android:background="@android:color/white"
    android:id="@+id/subject_name_cardview">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/subject_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Subject Name"
            android:textAppearance="?android:textAppearanceMedium"
            android:padding="@dimen/_10sdp"
            android:layout_marginTop="@dimen/_10sdp"
            android:layout_marginBottom="@dimen/_10sdp"
            android:layout_marginLeft="@dimen/_10sdp"
            android:textColor="@android:color/black" />

        <ImageButton
            android:id="@+id/subject_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:src="@drawable/ic_keyboard_arrow_down_24dp"
            android:background="#00000000"
            android:layout_marginRight="@dimen/_20sdp"
            />


    </RelativeLayout>

</android.support.v7.widget.CardView>

下面是childView xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/_5sdp"
    android:background="@color/colorPrimary"
    android:id="@+id/chapter_name_cardview">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/chapter_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Subject Name"
            android:textAppearance="?android:textAppearanceSmall"
            android:padding="@dimen/_10sdp"
            android:layout_marginTop="@dimen/_5sdp"
            android:layout_marginBottom="@dimen/_5sdp"
            android:layout_marginLeft="@dimen/_10sdp"
            android:textColor="@android:color/black" />

        <ImageButton
            android:id="@+id/chapter_name_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:src="@drawable/ic_keyboard_arrow_right_24dp"
            android:background="#00000000"
            android:layout_marginRight="@dimen/_20sdp"
            />


    </RelativeLayout>

</android.support.v7.widget.CardView>

好吧,简单一。

您在imagebutton中有groupview正在拦截触摸事件。 添加以下参数:

    android:focusable="false"
    android:focusableInTouchMode="false"

并为childview做同样的childview 那里的情况完全一样。

PS:为什么您仍然使用imagebuttons 您可以改用imageviews

您需要这样做。

expandableListView = (ExpandableListView)view.findViewById(R.id.expd_list_view);
SubjectAdapter adapter = new SubjectAdapter(getActivity(),subjectName,topicName);
expandableListView.setAdapter(adapter);

expandableListView .setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
         @Override
         public boolean onGroupClick(ExpandableListView expandableListView, View view, int groupPosition, long l) {
          expandableListView.expandGroup(groupPosition);
          return false;
        }
  });

希望这会帮助你。

尝试在适配器中传递您的ExpandableListView:

SubjectAdapter adapter = new SubjectAdapter(getActivity(),subjectName,topicName, expandableListView);

然后,您的适配器应如下所示:

...
ExpandableListView expandableListView;

    public SubjectAdapter(Context context, ArrayList<String> subjectName, HashMap<String, ArrayList<String>> topicName, ExpandableListView expandableListView) {
        this.context = context;
        this.topicName = topicName;
        this.subjectName = subjectName;
        this.expandableListView = expandableListView;
    }

在您的组视图中添加以下内容:

textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    expandableListView.expandGroup(groupPosition);    
            }
        });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM