[英]How to use Listview inside child layout of Expandablelistview in Android Studio
[英]How to override the child listview of a ExpandableListView
我希望ExpandableListView的子级在滚动视图中显示。 如果在一个组项目上有10条以上的评论,我希望能够在不滚动主屏幕的情况下滚动浏览孩子。
我曾尝试使用谷歌搜索,但发现的只是关于可扩展列表的标准教程。 我确实读过ExpandableListView对其子元素使用ListView,所以我认为我应该能够覆盖列表的布局,但是我不知道从哪里开始。
你们能指出我正确的方向吗?
为此,您将需要几节课,所以我将重点放在其中,只给您。
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类型(我不知道您需要什么,请指定和会进行编辑以满足您的需要)。
在我的ItemsActivity中,我将这样做(我使用的是基于Fragment的系统,而不是Activity类型的类,因此放在正确的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>
如果您感到懒惰/幸运,这里是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>
我假设您知道如何向可扩展视图中添加项目,因此希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.