仿购物车(二级列表)

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 ();
    }
}


©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,968评论 6 482
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,601评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 153,220评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,416评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,425评论 5 374
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,144评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,432评论 3 401
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,088评论 0 261
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,586评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,028评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,137评论 1 334
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,783评论 4 324
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,343评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,333评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,559评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,595评论 2 355
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,901评论 2 345

推荐阅读更多精彩内容