Android 使用片段和选项卡布局的可扩展列表视图

Android 使用片段和选项卡布局的可扩展列表视图,android,android-fragments,expandablelistview,android-tablayout,expandablelistadapter,Android,Android Fragments,Expandablelistview,Android Tablayout,Expandablelistadapter,嘿,伙计们,基本上我正在尝试在我的应用程序的一个选项卡(比如“活动”选项卡)中创建一个可扩展列表视图,所以它当然是由片段组成的,所以我尝试了这段代码,使可扩展列表视图与片段一起工作,它显示标题项,但单击时仅第一个标题项可展开第二个和第三个标题项没有响应,第一个标题项的子项在单击时也没有响应,请有人帮我解决这个问题 片段布局文件: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http:

嘿,伙计们,基本上我正在尝试在我的应用程序的一个选项卡(比如“活动”选项卡)中创建一个可扩展列表视图,所以它当然是由片段组成的,所以我尝试了这段代码,使可扩展列表视图与片段一起工作,它显示标题项,但单击时仅第一个标题项可展开第二个和第三个标题项没有响应,第一个标题项的子项在单击时也没有响应,请有人帮我解决这个问题

片段布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#ff8400" >

    <ExpandableListView
        android:id="@+id/lvExp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ExpandableListView>

</RelativeLayout>

Frag1 Java类:

package com.example.home.designbosmrohit;

import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.NonNull;
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.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Frag1 extends Fragment {
    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;




    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.frag1_layout, container, false);

        // get the listview
        expListView = (ExpandableListView) view.findViewById(R.id.lvExp);

        // preparing list data
        prepareListData();

        listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild);

        // setting list adapter
        expListView.setAdapter(listAdapter);

        return view;
    }
    private void prepareListData()
    {
        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<String>>();

        // Adding child data
        listDataHeader.add("Top 250");
        listDataHeader.add("Now Showing");
        listDataHeader.add("Coming Soon..");

        // Adding child data
        List<String> top250 = new ArrayList<String>();
        top250.add("The Shawshank Redemption");
        top250.add("The Godfather");
        top250.add("The Godfather: Part II");
        top250.add("Pulp Fiction");
        top250.add("The Good, the Bad and the Ugly");
        top250.add("The Dark Knight");
        top250.add("12 Angry Men");

        List<String> nowShowing = new ArrayList<String>();
        nowShowing.add("The Conjuring");
        nowShowing.add("Despicable Me 2");
        nowShowing.add("Turbo");
        nowShowing.add("Grown Ups 2");
        nowShowing.add("Red 2");
        nowShowing.add("The Wolverine");

        List<String> comingSoon = new ArrayList<String>();
        comingSoon.add("2 Guns");
        comingSoon.add("The Smurfs 2");
        comingSoon.add("The Spectacular Now");
        comingSoon.add("The Canyons");
        comingSoon.add("Europa Report");

        listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
        listDataChild.put(listDataHeader.get(1), nowShowing);
        listDataChild.put(listDataHeader.get(2), comingSoon);
    }



}
package com.example.home.designbosmrohit;
导入android.content.Context;
导入android.graphics.Typeface;
导入android.os.Bundle;
导入android.support.annotation.NonNull;
导入android.support.annotation.Nullable;
导入android.support.v4.app.Fragment;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseExpandableListAdapter;
导入android.widget.ExpandableListView;
导入android.widget.TextView;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
公共类Frag1扩展片段{
可扩展列表适配器;
ExpandableListView解释视图;
列表列表数据头;
HashMapListDataChild;
@可空
@凌驾
创建视图时的公共视图(@NonNull LayoutInflater inflater、@Nullable ViewGroup container、@Nullable Bundle savedInstanceState){
视图=充气机。充气(R.layout.frag1_布局,容器,假);
//获取列表视图
expListView=(ExpandableListView)view.findViewById(R.id.lvExp);
//准备列表数据
prepareListData();
listAdapter=新的ExpandableListAdapter(getActivity(),listDataHeader,listDataChild);
//设置列表适配器
expListView.setAdapter(listAdapter);
返回视图;
}
私有void prepareListData()
{
listDataHeader=新的ArrayList();
listDataChild=newHashMap();
//添加子数据
添加(“前250名”);
添加(“现在显示”);
添加(“即将推出…”);
//添加子数据
List top250=新的ArrayList();
top250.添加(“肖申克的救赎”);
添加(“教父”);
top250.添加(“教父:第二部分”);
top250.添加(“低俗小说”);
添加(“好、坏、丑”);
添加(“黑暗骑士”);
添加(“12个愤怒的男人”);
List nowShowing=新建ArrayList();
现在显示。添加(“变戏法”);
添加(“卑鄙的我2”);
现在显示。添加(“Turbo”);
现在显示。添加(“成年人2”);
现在显示。添加(“红色2”);
现在显示。添加(“狼獾”);
List comingSoon=新建ArrayList();
很快就来。添加(“2支枪”);
很快就来。添加(“蓝精灵2”);
即将到来。添加(“现在的壮观”);
即将到来。添加(“峡谷”);
即将出版。添加(“欧罗巴报告”);
listDataChild.put(listDataHeader.get(0),top250);//头,子数据
listDataChild.put(listDataHeader.get(1),现在显示);
listDataChild.put(listDataHeader.get(2),即将出现);
}
}
以及输出图像:

列表项不可点击,我无法展开“现在显示”和“即将到来”标题,有人能帮我解决这个问题吗

试试这个


不,伙计,这也不起作用不幸的是,我替换了getApplicationContext,因为它不起作用,所以我尝试将您放在onCreateView方法中的代码记录下来,但它仍然不起作用,项目的github链接我尝试了github中的代码,发现activity_main.xml中有另一个id相同的ExpandableListView。删除它后,child click可以正常工作。非常感谢兄弟,它可以正常工作:D
expListView.setOnChildClickListener(new OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            Toast.makeText(
                    getApplicationContext(),
                    listDataHeader.get(groupPosition)
                            + " : "
                            + listDataChild.get(
                                    listDataHeader.get(groupPosition)).get(
                                    childPosition), Toast.LENGTH_SHORT)
                    .show();
            return false;
        }
    });