ExpandableListView 折叠列表 案例
步骤:
1.创建一个Fragment
2.创建expandablelistview的布局
3.创建一个继承BaseExpandableListAdapter的class 适配器
4.创建一个父列表
5.创建一个子列表
1.第一步为fragment
ExpandableListView 的fragment
AllFragment.class
package com.culturer.guishi_shoper.pages.order.order_item;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import com.culturer.guishi_shoper.R;
public class AllFragment extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
private View contentView;
private ExpandableListView expandableListView;
private AllAdapter adapter; //Adapter在上面代码
public AllFragment() {
// Required empty public constructor
}
public static AllFragment newInstance(String param1, String param2) {
AllFragment fragment = new AllFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (contentView == null){
contentView = inflater.inflate(R.layout.fragment_all, container, false);
initBaseView(); //初始化基本布局
initList();
}
ViewGroup parent = (ViewGroup) contentView.getParent();
if ( parent!=null ){
parent.removeView(contentView);
}
return contentView;
}
private void initBaseView(){ //初始化expandableListView
expandableListView = contentView.findViewById(R.id.expandableListView);
}
private void initList(){ //初始化折叠列表
adapter = new AllAdapter(getContext());
expandableListView.setAdapter(adapter);
}
}
2.第二步为 ExpandableListView 布局
ExpandableListView布局 的 写法 fragment_all.xml
<com.scwang.smartrefresh.layout.SmartRefreshLayout
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"
android:id="@+id/refreshLayout"
tools:context="com.culturer.guishi_shoper.pages.order.order_item.AllFragment">
<ExpandableListView
android:id="@+id/expandableListView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
3.第三步,创建ExpandableList的一个 Adapter 继承BaseExpandableListAdapter
ExpandableListView的Adapter 为 BaseExpandableListAdapter
使用 class 来继承 BaseExpandableListAdapter
重写其中方法 AllAdapter.class
package com.culturer.guishi_shoper.pages.order.order_item;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import com.culturer.guishi_shoper.R;
/**
* Created by Administrator on 2018/7/9 0009.
*/
public class AllAdapter extends BaseExpandableListAdapter { //ExpandableListView的Adapter
private Context context;
public AllAdapter(Context context) {
this.context = context;
}
@Override
public int getGroupCount() { //父列表显示10条数据
return 10;
}
@Override
public int getChildrenCount(int i) { //子列表显示5条数据
return 5;
}
@Override
public Object getGroup(int i) {
return null;
}
@Override
public Object getChild(int i, int i1) {
return null;
}
@Override
public long getGroupId(int i) {
return 0;
}
@Override
public long getChildId(int i, int i1) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) { //父列表
View gruopView = LayoutInflater.from(context).inflate(R.layout.ok_group,null); //加载 ok_group 布局
return gruopView;
}
@Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) { //子列表
View childView = LayoutInflater.from(context).inflate(R.layout.ok_item,null); //加载 ok_item 布局
return childView;
}
@Override
public boolean isChildSelectable(int i, int i1) { //初始化为未被选中
return false;
}
}
4.创建父列表xml
ok_group.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/order_time"
android:text="2018.05.21 10:53"
android:textSize="12sp"
android:textColor="@color/white"
android:padding="5dp"
android:background="@color/black"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/group_id"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="start"
android:lines="1"
android:layout_marginLeft="25dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:padding="10dp"
android:text="订单编号:9527"
android:textSize="14sp"
android:textColor="@color/black"/>
<TextView
android:id="@+id/all_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:padding="10dp"
android:text="金额:¥ 952700"
android:textSize="14sp"
android:textColor="@color/black"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/group_tel"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="start"
android:lines="1"
android:layout_marginLeft="25dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:padding="10dp"
android:text="联系电话:18588263531"
android:textSize="14sp"
android:textColor="@color/black"/>
<TextView
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:padding="10dp"
android:text="8小时后自动签收"
android:textSize="14sp"
android:textColor="@color/black"/>
</LinearLayout>
</LinearLayout>
5.创建子列表布局
ok_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="start"
android:lines="1"
android:layout_weight="1"
android:layout_marginLeft="25dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:padding="10dp"
android:text="西瓜西瓜西瓜西瓜西瓜西瓜西瓜西瓜西瓜西瓜"
android:textSize="14sp"
android:textColor="@color/black"/>
<TextView
android:id="@+id/count"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="start"
android:lines="1"
android:layout_weight="1"
android:layout_marginLeft="25dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:padding="10dp"
android:text="数量: 1000"
android:textSize="14sp"
android:textColor="@color/black"/>
<TextView
android:id="@+id/price"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="start"
android:lines="1"
android:layout_weight="1"
android:layout_marginLeft="25dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:padding="10dp"
android:text="¥ 200000"
android:textSize="14sp"
android:textColor="@color/black"/>
</LinearLayout>

浙公网安备 33010602011771号