电商分类展示

整体布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.example.fanjie.jdapp.view.SortHeader
        android:id="@+id/sort_header"
        android:layout_width="match_parent"
        android:layout_height="50dp">

    </com.example.fanjie.jdapp.view.SortHeader>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/sort_header">

        <FrameLayout
            android:id="@+id/fragment_left"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"></FrameLayout>

        <FrameLayout
            android:id="@+id/fragment_right"
            android:layout_weight="3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="#f3f5f7"></FrameLayout>
    </LinearLayout>
</RelativeLayout>
整体Fragment
public class Sort extends Fragment {
    private static final int REQUEST_CODE = 1;
    private View sortView;
    private SortHeader sortHeader;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        sortView = inflater.inflate(R.layout.sort, container, false);
        return sortView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //获取控件
        initView();
        //标题点击事件
        click();
        //加载fragment
        SortLeftFragment leftFragment = new SortLeftFragment();
        getFragmentManager().beginTransaction().add(R.id.fragment_left,leftFragment).commit();
        SortRightFragment rightFragment = new SortRightFragment();
        getFragmentManager().beginTransaction().add(R.id.fragment_right,rightFragment).commit();
    }

    private void initView() {
        sortHeader = (SortHeader) sortView.findViewById(R.id.sort_header);
    }
}
左布局
<?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="match_parent">
    <ListView
        android:id="@+id/listview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:divider="#00000000">

    </ListView>
</LinearLayout>

左布局操作

public class SortLeftFragment extends Fragment implements ISortLeft {

    private View view;
    private ListView listView;
    private SortLeftPresenter sortLeftPresenter;
    private SortLeftAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.sort_left, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        listView = (ListView) view.findViewById(R.id.listview);
        getData();
//        SortRightFragment sortRightFragment = new SortRightFragment(5);
//        getFragmentManager().beginTransaction().replace(R.id.fragment_right,sortRightFragment).commit();
    }

    private void getData() {
        sortLeftPresenter = new SortLeftPresenter(this, getActivity());
        sortLeftPresenter.get();
    }

    @Override
    public void onSuccess(HomeVpBean data) {
        final List<HomeVpBean.DataBean> list = data.getData();
        adapter = new SortLeftAdapter(getActivity(), list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override

            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                SortRightFragment sortRightFragment = new SortRightFragment(list.get(i).getCid());
//                Log.e(TAG, "onItemClick: "+ list.get(i).getCid());
                getFragmentManager().beginTransaction().replace(R.id.fragment_right,sortRightFragment).commit();
            }
        });
    }


    @Override
    public void onFiled() {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (sortLeftPresenter!=null){
            sortLeftPresenter.detatch();
        }
    }
}
左布局适配器布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/tv_shop"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:textSize="20dp"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="21dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>
左布局适配器
public class SortLeftAdapter extends BaseAdapter{
    private Context context;
    private List<HomeVpBean.DataBean> list;

    public SortLeftAdapter(Context context, List<HomeVpBean.DataBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }
    class ViewHolder{
        TextView tv;
    }
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder vh;
        if (view==null){
            vh = new ViewHolder();
            view=View.inflate(context, R.layout.sort_left_item,null);
            vh.tv=(TextView)view.findViewById(R.id.tv_shop);
            view.setTag(vh);
        }else{
            vh = (ViewHolder) view.getTag();
        }
        vh.tv.setText(list.get(i).getName());
        return view;
    }
}
右布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/sort_right_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</RelativeLayout>
右布局操作
public class SortRightFragment extends Fragment implements ISortRight {

    private View view;
    private int i = 1;
    private RecyclerView recyclerView;
    private SortRightPresenter presenter;
    private SortRightAdapter adapter;

    public SortRightFragment(int i){
        this.i=i;
    }

    public SortRightFragment() {

    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.sort_right, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        recyclerView = (RecyclerView) view.findViewById(R.id.sort_right_recyclerview);
        presenter = new SortRightPresenter(this, getActivity());
        presenter.get(i);
    }

    @Override
    public void onSuccess(SortRightBean data) {
        List<SortRightBean.DataBean> list1 = data.getData();
//        List<SortRightBean.DataBean.ListBean> list2=new ArrayList<>();
//        for (int i = 0;i<list1.size();i++){
//            SortRightBean.DataBean dataBean = list1.get(i);
//            List<SortRightBean.DataBean.ListBean> list = dataBean.getList();
//            list2.addAll(list);
//        }
        LinearLayoutManager manager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(manager);
        adapter = new SortRightAdapter(getActivity(), list1);
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onFiled() {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (presenter!=null){
            presenter.detatch();
        }
    }
}
右布局适配器布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/tv_name">

    </android.support.v7.widget.RecyclerView>
</RelativeLayout>
右布局适配器

public class SortRightAdapter extends RecyclerView.Adapter<SortRightAdapter.ViewHolder>{
    private Context context;
    private List<SortRightBean.DataBean> list1;

    public SortRightAdapter(Context context, List<SortRightBean.DataBean> list1) {
        this.context = context;
        this.list1 = list1;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.sort_right_item, null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.tvName.setText(list1.get(position).getName());
        List<SortRightBean.DataBean.ListBean> list = list1.get(position).getList();
        GridLayoutManager manager = new GridLayoutManager(context, 3, LinearLayoutManager.VERTICAL, false);
        holder.recyclerview.setLayoutManager(manager);
        SortRightAdapter2 adapter2 = new SortRightAdapter2(context, list);
        holder.recyclerview.setAdapter(adapter2);
    }

    @Override
    public int getItemCount() {
        return list1.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private final TextView tvName;
        private final RecyclerView recyclerview;
        public ViewHolder(View itemView) {
            super(itemView);
            tvName=(TextView)itemView.findViewById(R.id.tv_name);
            recyclerview = (RecyclerView)itemView.findViewById(R.id.recyclerview);
        }
    }
}
右布局适配器中的适配器布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/image"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="43dp"
        android:layout_marginStart="43dp"
        android:layout_marginTop="21dp"
        app:srcCompat="@mipmap/ic_launcher"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/image"
        android:layout_alignStart="@+id/image"
        android:layout_below="@+id/image"
        android:layout_marginTop="10dp"
        android:text="TextView" />
</RelativeLayout>
右布局适配器中的适配器
class SortRightAdapter2 extends RecyclerView.Adapter<SortRightAdapter2.ViewHolder>{
    private Context context;
    private List<SortRightBean.DataBean.ListBean> list;

    public SortRightAdapter2(Context context, List<SortRightBean.DataBean.ListBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.sort_right_item2, null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        Glide.with(context).load(list.get(position).getIcon()).into(holder.image);
        holder.tvView.setText(list.get(position).getName());
        holder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, GoodsListActivity.class);
                intent.putExtra("pscid",list.get(position).getPscid()+"");
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private final ImageView image;
        private final TextView tvView;
        public ViewHolder(View itemView) {
            super(itemView);
            image = (ImageView)itemView.findViewById(R.id.image);
            tvView = (TextView)itemView.findViewById(R.id.textView);
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值