左右列表

mvp

v層
public interface ShopListView {
    void success(int type,String data);

    void fail(String error);
}

m層
public interface ShopListModel {
    interface CallBackListener {
        void success(int type,String data);

        void fail(String error);
    }

    //請求左側數據
    void doShopLeftList(int type,CallBackListener listener);

    //請求右側數據
    void doShopRightList(int type,String id, CallBackListener listener);

}

  @Override
    public void doShopLeftList(final int type, final CallBackListener listener) {
        String url = "http://172.17.8.100/small/commodity/v1/findFirstCategory";
        new OkHttpUtils().get(url).result(new OkHttpUtils.HttpListener() {
            @Override
            public void success(String data) {
                listener.success(type,data);
            }

            @Override
            public void fail(String error) {
                listener.fail(error);
            }
        });
    }

    @Override
    public void doShopRightList(final int type, String id, final CallBackListener listener) {
        String url = "http://172.17.8.100/small/commodity/v1/findSecondCategory?firstCategoryId=" + id;
        new OkHttpUtils().get(url).result(new OkHttpUtils.HttpListener() {
            @Override
            public void success(String data) {
                listener.success(type,data);
            }

            @Override
            public void fail(String error) {
                listener.fail(error);
            }
        });
    }
p層
public interface ShopListPresenter {

    //請求左側數據
    void doShopLeftList(int type);

    //請求右側數據
    void doShopRightList(int type,String id);
}

public class ShopListPresebterIMl implements ShopListPresenter, ShopListModel.CallBackListener {

    private ShopListView shopListView;
    private ShopListModel shopListModel;

    public ShopListPresebterIMl(ShopListView shopListView, ShopListModel shopListModel) {
        this.shopListView = shopListView;
        this.shopListModel = shopListModel;
    }

    public void destory() {
        if (shopListModel != null) {
            shopListModel = null;
        }
        if (shopListView != null) {
            shopListView = null;
        }
        System.gc();
    }


    //左側
    @Override
    public void doShopLeftList(int type) {
        shopListModel.doShopLeftList(type,this);
    }

    //右側
    @Override
    public void doShopRightList(int type, String id) {

        shopListModel.doShopRightList(type,id,this);

    }

    @Override
    public void success(int type, String data) {

        shopListView.success(type,data);
    }

    @Override
    public void fail(String error) {

        shopListView.fail(error);
    }
}
public class FragmentOne extends Fragment implements ShopListView {

    private RecyclerView mRecyclerViewLeft, mRecyclerViewRight;
    private ShopListPresebterIMl mShopListPresenterIml;
    private ShopListLeftAdapter mShopListLeftAdapter;
    private ShopListRightAdapter mShopListRightAdapter;
    private List<ShopListLeftBean.ResultBean> mLeftList = new ArrayList<>();



    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment01, container, false);
        //獲取id
        mRecyclerViewLeft = (RecyclerView) view.findViewById(R.id.recycler_left);
        mRecyclerViewRight = (RecyclerView) view.findViewById(R.id.recycler_right);

        //添加布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerViewLeft.setLayoutManager(linearLayoutManager);

        //左側適配器
        mShopListLeftAdapter= new ShopListLeftAdapter(getActivity());

        mRecyclerViewLeft.setAdapter(mShopListLeftAdapter);

        //獲取p層
        mShopListPresenterIml = new ShopListPresebterIMl(this,new ShopListModelIml());

       //佈局管理器
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 3);
        mRecyclerViewRight.setLayoutManager(gridLayoutManager);
        //右側適配器
        mShopListRightAdapter = new ShopListRightAdapter(getActivity());
        mRecyclerViewRight.setAdapter(mShopListRightAdapter);

        //左側請求
        mShopListPresenterIml.doShopLeftList(0);

        mShopListLeftAdapter.setOnItemClickListener(new ShopListLeftAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                String id = mLeftList.get(position).getId();
                mShopListPresenterIml.doShopRightList(1,id);
                //顏色
                for (int a = 0; a < mLeftList.size(); a++) {
                    if (position == a) {
                        mLeftList.get(a).setClick(true);
                    } else {
                        mLeftList.get(a).setClick(false);
                    }
                }
                mShopListLeftAdapter.notifyDataSetChanged();
            }
        });
        return view;
    }





    @Override
    public void success(int type, String data) {

        if (type == 0) {
            //在這裏解析數據
            ShopListLeftBean bean = new Gson().fromJson(data, ShopListLeftBean.class);
            mLeftList = bean.getResult();
            mShopListLeftAdapter.setList(mLeftList);
            //右側請求
            bean.getResult().get(0).setClick(true);
            mShopListPresenterIml.doShopRightList(1, bean.getResult().get(0).getId());
        } else {
            ShopListRightBean bean = new Gson().fromJson(data, ShopListRightBean.class);
            mShopListRightAdapter.setList(bean.getResult());
        }

    }

    @Override
    public void fail(String error) {


    }
}

public class ShopListLeftAdapter  extends RecyclerView.Adapter<ShopListLeftAdapter.ShopViewHolder> {
    private List<ShopListLeftBean.ResultBean> list = new ArrayList<>();
    private Context context;
    private OnItemClickListener mOnItemClickListener;

    public ShopListLeftAdapter(Context context) {
        this.context = context;
    }

    @NonNull
    @Override
    public ShopListLeftAdapter.ShopViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = View.inflate(context, R.layout.item_left, null);
        ShopViewHolder viewHolder = new ShopViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ShopListLeftAdapter.ShopViewHolder shopViewHolder, final int i) {
        shopViewHolder.mTitle.setText(list.get(i).getName());

        if (list.get(i).isClick()) {
            shopViewHolder.mTitle.setTextColor(Color.parseColor("#d43c3c"));
        } else {
            shopViewHolder.mTitle.setTextColor(Color.parseColor("#666666"));
        }

        shopViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClick(i);
            }
        });


    }

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

    //傳遞數據
    public void setList(List<ShopListLeftBean.ResultBean> list) {
        this.list = list;
    }

    public class ShopViewHolder extends RecyclerView.ViewHolder {
        TextView mTitle;

        public ShopViewHolder(@NonNull View itemView) {
            super(itemView);
            mTitle = (TextView) itemView.findViewById(R.id.tv_title);
        }
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;

    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }
}


public class ShopListLeftAdapter  extends RecyclerView.Adapter<ShopListLeftAdapter.ShopViewHolder> {
    private List<ShopListLeftBean.ResultBean> list = new ArrayList<>();
    private Context context;
    private OnItemClickListener mOnItemClickListener;

    public ShopListLeftAdapter(Context context) {
        this.context = context;
    }

    @NonNull
    @Override
    public ShopListLeftAdapter.ShopViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = View.inflate(context, R.layout.item_left, null);
        ShopViewHolder viewHolder = new ShopViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ShopListLeftAdapter.ShopViewHolder shopViewHolder, final int i) {
        shopViewHolder.mTitle.setText(list.get(i).getName());

        if (list.get(i).isClick()) {
            shopViewHolder.mTitle.setTextColor(Color.parseColor("#d43c3c"));
        } else {
            shopViewHolder.mTitle.setTextColor(Color.parseColor("#666666"));
        }

        shopViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClick(i);
            }
        });


    }

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

    //傳遞數據
    public void setList(List<ShopListLeftBean.ResultBean> list) {
        this.list = list;
    }

    public class ShopViewHolder extends RecyclerView.ViewHolder {
        TextView mTitle;

        public ShopViewHolder(@NonNull View itemView) {
            super(itemView);
            mTitle = (TextView) itemView.findViewById(R.id.tv_title);
        }
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;

    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }
}


<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/shop_bg"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        >

        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:textColor="#ffffff"
            />

    </RelativeLayout>
</RelativeLayout>
<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp">

        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#999999"
            android:layout_alignParentBottom="true"
            />
    </RelativeLayout>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章