如何提取ExpandableListView以打开特定片段

时间:2018-09-09 12:14:13

标签: android android-studio android-fragments expandablelistview

我试图在我的应用程序上实现一个expandablelistview,以便当我单击listview时,它将从下一个活动中选择片段。该如何在应用程序中实现。

这是我的第一个expandablelist适配器。

    public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<String> listDataHeader;
    private HashMap<String,List<String>> listHashMap;

    public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listHashMap) {
        this.context = context;
        this.listDataHeader = listDataHeader;
        this.listHashMap = listHashMap;
    }

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

    @Override
    public int getChildrenCount(int i) {
        return listHashMap.get(listDataHeader.get(i)).size();
    }

    @Override
    public Object getGroup(int i) {
        return listDataHeader.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return listHashMap.get(listDataHeader.get(i)).get(i1); // i = Group Item, i1 = ChildItem
    }

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

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

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

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        String headerTitle = (String) getGroup(i);
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.list_group, null);
        }
        TextView lblListHeader = (TextView)view.findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);
        return view;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        final String childText = (String) getChild(i, i1);
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.list_item, null);
        }

        TextView txtListChild = (TextView)view.findViewById(R.id.lblListItem);
        txtListChild.setText(childText);
        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }
}

这是ExpandableListView。我在其中实现列表并设置onclicklistener的位置,还添加了带onclicklistener的putextra,它应在下一个活动中打开一个片段,其中一个子项是int Expandablelistview,单击该

public class GeneralIndex extends Fragment {

    private ExpandableListView listView;
    private ExpandableListAdapter listAdapter;
    private List<String> listDataHeader;
    private HashMap<String, List<String>> listHash;

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

        listView = (ExpandableListView) view.findViewById(R.id.lvExpand);
        initData();
        listAdapter = new ExpandableListAdapter(getContext(), listDataHeader, listHash);

listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
                String generalIndex = (String) listView.getItemAtPosition(i).toString();
                Intent intent = new Intent(getActivity(), HymnActivity.class);
                intent.putExtra("pageNo", i);
                startActivity(intent);
                return false;
            }
        });

        listView.setAdapter(listAdapter);

        return view;
    }

    private void initData() {
        listDataHeader = new ArrayList<>();
        listHash = new HashMap<>();

        listDataHeader.add("A");
        listDataHeader.add("B");


        List<String> A = new ArrayList<>();
        A.add("A Day to come");
        A.add("Another Visit to the eastt");

        List<String> B = new ArrayList<>();
        B.add("Expandable List Viewdfd");
        B.add("Expandabsdfsdle List View");
        B.add("Expandable Listdfd View");
        B.add("Expandasfdfsble List View");

        listHash.put(listDataHeader.get(0),A);
        listHash.put(listDataHeader.get(1),B);
    }
}

在本活动中,我想解开从第一个片段传递过来的多余部分。我该怎么做?

 Bundle bundle = getIntent().getExtras();
    if(bundle != null) {
        String string = bundle.getString("pageNo");

0 个答案:

没有答案