xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/color_bg_selected">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="全部分类"
android:textColor="@color/white"
android:textSize="15sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ListView
android:id="@+id/listView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="4dp"
android:layout_weight="1.3"
android:background="@color/white"
android:choiceMode="singleChoice"
android:divider="@color/aaa"
android:listSelector="#00000000"
android:scrollbars="none">
</ListView>
<ExpandableListView
android:id="@+id/elv"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:numColumns="2"
android:layout_marginTop="10dp"
android:horizontalSpacing="8dp"
android:verticalSpacing="8dp"
android:layout_marginLeft="10dp"
android:divider="@null"
android:layout_marginRight="10dp"
android:scrollbars="none">
</ExpandableListView>
</LinearLayout>
</LinearLayout>
二级列表的适配器
package com.example.day_one.adapter;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.example.day_one.R;
import com.example.day_one.activity.Web;
import com.example.day_one.bean.FlBean;
import com.example.day_one.presenter.FlPresenter;
import java.util.ArrayList;
import java.util.List;
public class GwcAdapter extends BaseExpandableListAdapter {
private List<FlBean.ClassFristShopBean> groupList;
private List<FlBean.ClassFristShopBean> mItemList;
private LayoutInflater mInflater;
private Context context;
private Activity activity;
public GwcAdapter(List<FlBean.ClassFristShopBean> groupList, List<FlBean.ClassFristShopBean> mItemList, Context context ) {
this.groupList = groupList;
this.mItemList = mItemList;
this.context = context;
mInflater = LayoutInflater.from (context);
}
@Override
public int getGroupCount() {
return groupList.size ();
}
@Override
public int getChildrenCount(int groupPosition) {
return mItemList.get (groupPosition).getShopListBeans ().size ();
}
@Override
public Object getGroup(int groupPosition) {
return groupList.get (groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return mItemList.get (groupPosition).getShopListBeans ().get (childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
final GroupHolder holder;
if (convertView == null) {
convertView = mInflater.inflate (R.layout.group_item, null);
holder = new GroupHolder ();
holder.tv_frist_name = convertView.findViewById (R.id.tv_frist_name);
convertView.setTag (holder);
} else {
holder = (GroupHolder) convertView.getTag ();
}
//赋值
FlBean.ClassFristShopBean classFristShopBean = groupList.get (groupPosition);
holder.tv_frist_name.setText (classFristShopBean.getFrist_name ());
return convertView;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildHolder holder;
if (convertView == null) {
convertView = mInflater.inflate (R.layout.child_item, null);
holder = new ChildHolder ();
holder.cb_child = convertView.findViewById (R.id.cb_child);
holder.iv_shop_image_url = convertView.findViewById (R.id.iv_shop_image_url);
holder.tv_shop_name = convertView.findViewById (R.id.tv_shop_name);
holder.tv_shop_introd = convertView.findViewById (R.id.tv_shop_introd);
holder.tv_shop_pirce = convertView.findViewById (R.id.tv_shop_pirce);
convertView.setTag (holder);
} else {
holder = (ChildHolder) convertView.getTag ();
}
FlBean.ClassFristShopBean.ShopListBeansBean shopListBeansBean = mItemList.get (groupPosition).getShopListBeans ().get (childPosition);
holder.tv_shop_introd.setText (shopListBeansBean.getShop_introd ());
holder.tv_shop_name.setText (shopListBeansBean.getShop_name ());
holder.tv_shop_pirce.setText ("¥"+shopListBeansBean.getShop_pirce ());
Glide.with (context).load (shopListBeansBean.getShop_image_url ()).into (holder.iv_shop_image_url);
//二级列表条目点击事件,返回数据
holder.iv_shop_image_url.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("id",groupList.get(groupPosition).getShopListBeans ().get(childPosition).getShop_ID ());
intent.putExtra("name",groupList.get(groupPosition).getShopListBeans ().get(childPosition).getShop_name ());
// activity.setResult(Activity.RESULT_OK,intent);
// activity.finish();
}
});
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
private class GroupHolder {
TextView tv_frist_name;
}
private class ChildHolder {
CheckBox cb_child;
ImageView iv_shop_image_url;
TextView tv_shop_name;
TextView tv_shop_introd;
TextView tv_shop_pirce;
}
}
ListView的适配器
package com.example.day_one.adapter;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.example.day_one.R;
import com.example.day_one.bean.FlBean;
import java.util.ArrayList;
public class ListAdapter extends BaseAdapter {
private ArrayList<FlBean.ClassFristShopBean> list;
private Context context;
LayoutInflater inflater;
private int selectedPosition = 0;
public ListAdapter(ArrayList<FlBean.ClassFristShopBean> list, Context context) {
this.list = list;
this.context = context;
inflater = LayoutInflater.from (context);
}
@Override
public int getCount() {
return list.size ();
}
@Override
public Object getItem(int position) {
return list.get (position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = LayoutInflater.from (context).inflate (R.layout.list_item, null);
holder = new ViewHolder ();
holder.tv_shangjia = (TextView) convertView.findViewById (R.id.tv_shangjia);
holder.relayout = (RelativeLayout) convertView.findViewById (R.id.relayout);
convertView.setTag (holder);
} else {
holder = (ViewHolder) convertView.getTag ();
}
// 设置选中效果
if (selectedPosition == position) {
holder.tv_shangjia.setTextColor (Color.parseColor ("#ff8400"));
holder.relayout.setBackgroundColor (Color.parseColor ("#fff3f3f3"));
} else {//默认效果
holder.tv_shangjia.setTextColor (Color.BLACK);
holder.relayout.setBackgroundColor (Color.WHITE);
}
holder.tv_shangjia.setText (list.get (position).getFrist_name ());
return convertView;
}
public static class ViewHolder {
public TextView tv_shangjia;
public RelativeLayout relayout;
}
//设置选中的条目
public void setSelectedPosition(int position) {
selectedPosition = position;
}
}
Activity里的实现方法
public class TwoFragment extends Fragment implements FlView {
private View view;
private ExpandableListView mElv;
private FlBean flBean;
private ArrayList<FlBean.ClassFristShopBean> groupList;
private ArrayList<FlBean.ClassFristShopBean> mItemList;
private GwcAdapter adapter;
private ListView mListView;
private ArrayList<FlBean.ClassFristShopBean> list;
private ListAdapter listAdapter;
private FlBean.ClassFristShopBean classFristShopBean;
public TwoFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflate = inflater.inflate (R.layout.fragment_two, container, false);
initView (inflate);
initData ();
initLinster ();
return inflate;
}
private void initLinster() {
//左边ListView 的点击事件,关联右边的多级列表
mListView.setOnItemClickListener (new AdapterView.OnItemClickListener () {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//点击ListView的条目,展示与LIstView中该条目对应的ExpandableListView的列表
classFristShopBean = list.get (position);
adapter = new GwcAdapter (groupList,mItemList, getActivity ());
groupList.clear ();
mItemList.clear ();
groupList.add (classFristShopBean);
mItemList.add (classFristShopBean);
adapter.notifyDataSetChanged ();
mElv.setAdapter (adapter);
mElv.setGroupIndicator (null);
for (int i = 0; i < adapter.getGroupCount (); i++) {
mElv.expandGroup (i);//多级列表初始化展开状态
// expendlistview.collapseGroup(i);//关闭
}
listAdapter.setSelectedPosition (position);//设置选中的条目
listAdapter.notifyDataSetInvalidated ();
}
});
}
private void initData() {
ImpFlPresenter impFlPresenter = new ImpFlPresenter (new ImpFlModel (), this);
impFlPresenter.getFl ();
}
private void initView(View inflate) {
mElv = (ExpandableListView) inflate.findViewById (R.id.elv);
mListView = (ListView) inflate.findViewById (R.id.listView);
groupList = new ArrayList<> ();
mItemList = new ArrayList<> ();
list = new ArrayList<> ();
listAdapter = new ListAdapter (list, getActivity ());
mListView.setAdapter (listAdapter);
adapter = new GwcAdapter (groupList, mItemList, getActivity ());
mElv.setAdapter (adapter);
mElv.setOnChildClickListener (new ExpandableListView.OnChildClickListener () {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Intent intent = new Intent (getActivity (), Web.class);
getActivity ().startActivity (intent);
return false;
}
});
if (list != null && list.size () > 0) {
//Expandable 的Adapter ,get(0).dataList 默认展示ListView中第一个条目对应的ExpandableListView列表。
GwcAdapter gwcAdapter = new GwcAdapter (groupList, mItemList, getActivity ());
groupList.add (classFristShopBean);
mItemList.add (classFristShopBean);
mElv.setAdapter (gwcAdapter);
for (int i = 0; i < gwcAdapter.getGroupCount (); i++) {
mElv.expandGroup (i);//多级列表初始化展开状态
// expendlistview.collapseGroup(i);//多级列表初始关闭状态
}
}
}
@Override
public void onSuccess(FlBean flBean) {
groupList.addAll (flBean.getClassFristShop ());
mItemList.addAll (flBean.getClassFristShop ());
list.addAll (flBean.getClassFristShop ());
adapter.notifyDataSetChanged ();
}
@Override
public void onFail(String error) {
Toast.makeText (getActivity (), error, Toast.LENGTH_SHORT).show ();
}
}