)
UrlUtils(网址接口)
public interface UrlUtils {
public String DataBesnUrl = "http://www.zhaoapi.cn/product/getCatagory";
public String DataBesnUrl2 = "http://www.zhaoapi.cn/product/getProductCatagory?cid=";
public String DataBesnUrl3 = "http://www.zhaoapi.cn/product/getCarts?uid=71";
}
MainActivity
public class MainActivity extends FragmentActivity implements View.OnClickListener{
private ViewPager vp;
private ArrayList<Fragment> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
list = new ArrayList<>();
list.add(new FenFragment());
list.add(new ShopFragment());
vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
return list.get(position);
}
@Override
public int getCount() {
return list.size();
}
});
}
private void initView() {
vp = findViewById(R.id.vp);
findViewById(R.id.btn_fen).setOnClickListener(this);
findViewById(R.id.btn_shop).setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_fen:
vp.setCurrentItem(0);
break;
case R.id.btn_shop:
vp.setCurrentItem(1);
break;
}
}
}
MainActivity布局
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"></android.support.v4.view.ViewPager>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_fen"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="分组 " />
<Button
android:id="@+id/btn_shop"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="购物车 " />
</LinearLayout>
</LinearLayout>
代码布局
Fragment
fenFragment
public class FenFragment extends android.support.v4.app.Fragment implements FenView{
private RecyclerView recyclerViewLeft;
private RecyclerView recyclerViewRight;
private LeftAdapter leftAdapter;
private RightAdapter rightAdapter;
private FenPersenter fenPersenter;
private String url = "http://www.zhaoapi.cn/product/getProductCatagory?cid=";
int page=3;
private String url2 = "";
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = View.inflate(getContext(), R.layout.fragment_fen, null);
recyclerViewLeft = view.findViewById(R.id.recyclerView_left);
recyclerViewRight = view.findViewById(R.id.recyclerView_right);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
fenPersenter = new FenPersenter(this);
fenPersenter.show(UrlUtils.DataBesnUrl);
url2 = url+page;
fenPersenter.show(url2);
}
@Override
public void onData(final List<DataBean> list) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
recyclerViewLeft.setLayoutManager(staggeredGridLayoutManager);
leftAdapter = new LeftAdapter(list,getActivity());
recyclerViewLeft.setAdapter(leftAdapter);
leftAdapter.setItemClick(new LeftAdapter.ItemClick() {
@Override
public void onItem(int data) {
url2 = url + data;
fenPersenter.show2(url2);
}
});
}
});
}
@Override
public void onData2(final List<Things.DataBean> list) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerViewRight.setLayoutManager(linearLayoutManager);
rightAdapter = new RightAdapter(list,getActivity());
recyclerViewRight.setAdapter(rightAdapter);
// rightAdapter.setDate(list);
}
});
}
}
fenFragment布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView_left"
android:layout_weight="3"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
<android.support.v7.widget.RecyclerView
android:layout_weight="1"
android:id="@+id/recyclerView_right"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</LinearLayout>
ShopFragment
public class ShopFragment extends android.support.v4.app.Fragment implements ShoppingView {
private ExpandableListView expandableListView;
private CheckBox ckQuan;
private TextView tvSum;
private Button btnJiesuan;
private ShoppingPersenter shoppingPersenter;
private MyExpandAdapter myExpandAdapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = View.inflate(getContext(), R.layout.fragment_shop, null);
expandableListView = view.findViewById(R.id.expandable_listView);
expandableListView.setGroupIndicator(null);
ckQuan = view.findViewById(R.id.ck_quan);
tvSum = view.findViewById(R.id.tv_sum);
btnJiesuan = view.findViewById(R.id.btn_jiesuan);
shoppingPersenter = new ShoppingPersenter(this);
shoppingPersenter.shop(UrlUtils.DataBesnUrl3);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//全选监听
ckQuan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b){
setAll(1);
getTotal();
}else{
setAll(0);
getTotal();
}
}
});
}
private void setAll(int i) {
int groupCount = myExpandAdapter.getGroupCount();
for (int j = 0; j <groupCount ; j++) {
Phone.DataBean group = (Phone.DataBean) myExpandAdapter.getGroup(j);
List<Phone.DataBean.ListBean> itemlist = group.getList();
for (int k = 0; k < itemlist.size() ; k++) {
Phone.DataBean.ListBean listBean = itemlist.get(k);
listBean.setSelected(i);
}
}
myExpandAdapter.notifyDataSetChanged();
}
//计算价格
private void getTotal() {
double total = 0;
int groupCount = myExpandAdapter.getGroupCount();
for (int i = 0; i <groupCount ; i++) {
Phone.DataBean item = (Phone.DataBean) myExpandAdapter.getGroup(i);
List<Phone.DataBean.ListBean> list = item.getList();
for (int j = 0; j <list.size() ; j++) {
Phone.DataBean.ListBean listBean = list.get(j);
boolean checked = listBean.isChecked();
if (checked){
String price = listBean.getPrice();
double v = Double.parseDouble(price);
total += v * listBean.getNum();
}
}
}
tvSum.setText("合计:"+total);
}
@Override
public void onData(final List<Phone.DataBean> list) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
myExpandAdapter = new MyExpandAdapter(getActivity().getApplicationContext(),list);
expandableListView.setAdapter(myExpandAdapter);
//默认全部展开
for (int i = 0; i <myExpandAdapter.getGroupCount() ; i++) {
expandableListView.expandGroup(i);
}
initShopChartChange();
}
});
}
private void initShopChartChange() {
expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
Phone.DataBean group = (Phone.DataBean) myExpandAdapter.getGroup(i);
group.setChecked(!group.isChecked());
int c = 0;
if(group.isChecked()){
c = 1;
}
List<Phone.DataBean.ListBean> list = group.getList();
for (int j = 0; j <list.size() ; j++) {
Phone.DataBean.ListBean listBean = list.get(j);
listBean.setSelected(c);
}
getTotal();
myExpandAdapter.notifyDataSetChanged();
return true;
}
});
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
Phone.DataBean.ListBean listBean = (Phone.DataBean.ListBean) myExpandAdapter.getChild(i, i1);
boolean checked = listBean.isChecked();
if(checked){
listBean.setSelected(0);
}else {
listBean.setSelected(1);
}
myExpandAdapter.notifyDataSetChanged();
getTotal();
return true;
}
});
myExpandAdapter.setmOnNumChangedListener(new AddSub.OnNumChangedListener() {
@Override
public void onNumChange(View view, int curNum) {
getTotal();
}
});
}
}
shopFragment布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ExpandableListView
android:id="@+id/expandable_listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"></ExpandableListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/ck_quan"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_sum"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20dp"
android:text="总价" />
<Button
android:id="@+id/btn_jiesuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#f00"
android:textColor="#fff"
android:text="去结算"/>
</LinearLayout>
</LinearLayout>
Http
okhttp
public class OKHttp {
public void show(String url, final HttpCallBack httpCallBack){
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url(url).get().build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if(response.code() == 200){
ResponseBody body = response.body();
String string = body.string();
httpCallBack.setData(string);
}
}
});
}
public interface HttpCallBack{
void setData(String s);
void getData(List<DataBean> list);
void getData2(List<Things.DataBean> list);
}
}
okhttp2
public class OKHttpTwo {
public void shop(String url, final HttpCallBack httpCallBack){
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url(url).get().build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if(response.code() == 200){
ResponseBody body = response.body();
String string = body.string();
httpCallBack.setData(string);
}
}
});
}
public interface HttpCallBack{
void setData(String s);
void getData(List<Phone.DataBean> list);
}
}
MVP(第一个页面)
fenM
public class FenModel {
public void show(String url, final OKHttp.HttpCallBack callBack){
OKHttp okHttp = new OKHttp();
okHttp.show(url, new OKHttp.HttpCallBack() {
@Override
public void setData(String s) {
Gson gson = new Gson();
Goods goods = gson.fromJson(s, Goods.class);
List<DataBean> data = goods.getData();
callBack.getData(data);
}
@Override
public void getData(List<DataBean> list) {
}
@Override
public void getData2(List<Things.DataBean> list) {
}
});
}
public void show2(String url, final OKHttp.HttpCallBack callBack){
OKHttp okHttp = new OKHttp();
okHttp.show(url, new OKHttp.HttpCallBack() {
@Override
public void setData(String s) {
Gson gson = new Gson();
Things things = gson.fromJson(s, Things.class);
List<Things.DataBean> data2 = things.getData();
callBack.getData2(data2);
}
@Override
public void getData(List<DataBean> list) {
}
@Override
public void getData2(List<Things.DataBean> list) {
}
});
}
}
fenV
public interface FenView {
void onData(List<DataBean> list);
void onData2(List<Things.DataBean> list);
}
fenP
public class FenPersenter {
private FenModel fenModel;
private FenView fenView;
public FenPersenter(FenView fenView) {
this.fenView = fenView;
fenModel = new FenModel();
}
public void show(String url){
fenModel.show(url, new OKHttp.HttpCallBack() {
@Override
public void setData(String s) {
}
@Override
public void getData(List<DataBean> list) {
fenView.onData(list);
}
@Override
public void getData2(List<Things.DataBean> list) {
}
});
}
public void show2(String url){
fenModel.show2(url, new OKHttp.HttpCallBack() {
@Override
public void setData(String s) {
}
@Override
public void getData(List<DataBean> list) {
}
@Override
public void getData2(List<Things.DataBean> list) {
fenView.onData2(list);
}
});
}
}
Adapter
第一个页面的LeftAdapter
public class LeftAdapter extends RecyclerView.Adapter<LeftAdapter.MyViewHolder> {
private List<DataBean> list;
private Context context;
public LeftAdapter(List<DataBean> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_left, parent, false);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.tvLeft.setText(list.get(position).getName());
}
@Override
public int getItemCount() {
return list.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
private TextView tvLeft;
public MyViewHolder(View itemView) {
super(itemView);
tvLeft = itemView.findViewById(R.id.tv_left);
tvLeft.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
itemClick.onItem(getAdapterPosition());
}
});
}
}
private ItemClick itemClick;
public interface ItemClick{
void onItem(int data);
}
public void setItemClick(ItemClick itemClick) {
this.itemClick = itemClick;
}
}
第一个页面的第一个适配器的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_left"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"/>
</LinearLayout>
第一个页面的第二个RightAdapter
public class RightAdapter extends RecyclerView.Adapter<RightAdapter.RightViewHolder> {
private List<Things.DataBean> list;
private Context context;
public RightAdapter(List<Things.DataBean> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public RightViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_right, parent, false);
RightViewHolder rightViewHolder = new RightViewHolder(view);
return rightViewHolder;
}
@Override
public void onBindViewHolder(RightViewHolder holder, int position) {
holder.tvRight.setText(list.get(position).getName());
GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 3, RecyclerView.VERTICAL, false);
holder.recyclerViewChild.setLayoutManager(gridLayoutManager);
ChildAdapter childAdapter = new ChildAdapter(list.get(position).getList(),context);
holder.recyclerViewChild.setAdapter(childAdapter);
}
@Override
public int getItemCount() {
return list.size();
}
class RightViewHolder extends RecyclerView.ViewHolder{
private TextView tvRight;
private RecyclerView recyclerViewChild;
public RightViewHolder(View itemView) {
super(itemView);
tvRight = itemView.findViewById(R.id.tv_right);
recyclerViewChild = itemView.findViewById(R.id.recyclerView_child);
}
}
}
第一个页面的第二个适配器的布局
<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/tv_right"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView_child"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"></android.support.v7.widget.RecyclerView>
</LinearLayout>
第一个适配器的第二个布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/child_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/child_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/child_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="hhh" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/child_text1"
android:textColor="#000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<lqy.bwie.com.modeshopping.AddSub
android:id="@+id/child_addsub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:layout_marginLeft="10dp" />
</LinearLayout>
</LinearLayout>
第二个适配器的子ChildAdapter
public class ChildAdapter extends RecyclerView.Adapter<ChildAdapter.MyViewHolder> {
private List<Things.DataBean.ListBean> list;
private Context context;
public ChildAdapter(List<Things.DataBean.ListBean> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public ChildAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_child, parent, false);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(ChildAdapter.MyViewHolder holder, int position) {
Picasso.with(context).load(list.get(position).getIcon()).into(holder.imgChild);
holder.tvChild.setText(list.get(position).getName());
}
@Override
public int getItemCount() {
return list.size() ;
}
class MyViewHolder extends RecyclerView.ViewHolder{
private ImageView imgChild;
private TextView tvChild;
public MyViewHolder(View itemView) {
super(itemView);
imgChild = itemView.findViewById(R.id.img_child);
tvChild = itemView.findViewById(R.id.tv_child);
}
}
}
子适配器的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/img_child"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_child"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>