如何覆盖 ExpandableListView 的子列表视图

How to override the child listview of a ExpandableListView

我希望 ExpandableListView 的子项显示在滚动视图中。如果对组项目的评论超过 10 条,我希望能够在不滚动主屏幕的情况下滚动子项。

我试过用谷歌搜索这个,但我发现的都是关于可扩展列表的标准教程。我读过 ExpandableListView 对其子元素使用 ListView,所以我认为我应该能够覆盖列表的布局,但我不太知道从哪里开始。

你们能给我指出正确的方向吗?

为此,你需要几个 classes,所以我会剪下来给你。

import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class ItemsExpandableListAdapter extends BaseExpandableListAdapter {

 private Activity context;
    private Map<String, List<String>> ItemsCategoryCollections;
    private List<String> ItemsCategories;

    public ItemsExpandableListAdapter(Activity context, List<String> Categories,
            Map<String, List<String>> CategoryCollections) {
        this.context = context;
        this.ItemsCategoryCollections = CategoryCollections;
        this.ItemsCategories = Categories;
    }

    public Object getChild(int groupPosition, int childPosition) {
        return ItemsCategoryCollections.get(ItemsCategories.get(groupPosition)).get(childPosition);
    }

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

    public View getChildView(final int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        final String Itemscategory = (String) getChild(groupPosition, childPosition);
        LayoutInflater inflater = context.getLayoutInflater();

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.items_child_item, parent, false);
        }


        return convertView;
    }

    public int getChildrenCount(int groupPosition) {
        return ItemsCategoryCollections.get(ItemsCategories.get(groupPosition)).size();
    }

    public Object getGroup(int groupPosition) {
        return ItemsCategories.get(groupPosition);
    }

    public int getGroupCount() {
        return ItemsCategories.size();
    }

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

    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        String ItemsName = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.items_group_item, parent, false);
        }

        return convertView;
    }

    public boolean hasStableIds() {
        return true;
    }

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

在上面的 getChildView 方法中,您可以将布局中的视图更改为 Listview,然后您可以使用快速 google 搜索您想要的内容来实现您想要的类型的 Listview(我不不知道您需要什么,请指定,我会根据您的需要进行编辑)。

在我的项目Activity中,我将这样做(我使用的是基于片段的系统,而不是 Activity 类型 class,所以放入正确的 OnCreate方法)

ExpandableListView expListView;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    expListView = (ExpandableListView) getActivity().findViewById(R.id.exLstItems);
    final ExpandableListAdapter expListAdapter = new ItemsExpandableListAdapter(getActivity(), groupItemList, ItemCollection);
    expListView.setAdapter(expListAdapter);

    expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
            if (groupPosition == 0){
                if (childPosition == 0){
                }
            }
         }
    });
}

对于 items_child_item.xml,您的 Listview 的简单实现应该可以解决问题。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<ListView
    android:id="@+id/lstItemsChild"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:gravity="center|left"/>

</RelativeLayout>

如果你觉得 lazy/lucky,这里是 items.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp" >

<ExpandableListView
    android:id="@+id/exLstItems"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:childDivider="@color/red"
    android:divider="@color/green"
    android:dividerHeight="2dp"
    android:fastScrollEnabled="true"
    android:groupIndicator="@null"
    android:overScrollMode="always"
    android:scrollbars="none"
    android:smoothScrollbar="true" >
</ExpandableListView>

</RelativeLayout>

我假设您知道如何将项目添加到可扩展视图中,所以我希望这对您有所帮助。