Recycleview+XRecycleView+購物車
先看效果圖
因為第一個頁面有兩次請求資料 所以成功的方法添加了個 int type型別區分
1.MainActivity
public class MainActivity extends BaseActivity implements View.OnClickListener {
private FragmentManager manager; private AFragment aFragment; private BFragment bFragment; private Button mShouBtn; private Button mDingBtn; @Override protected int getLayout() { return R.layout.activity_main; } @Override protected void initView() { mShouBtn = findViewById(R.id.ShouYe_Btn); mDingBtn = findViewById(R.id.DingDan_Btn); manager = getSupportFragmentManager(); aFragment = new AFragment(); bFragment = new BFragment(); } @Override protected void setOnClick() { mShouBtn.setOnClickListener(this); mDingBtn.setOnClickListener(this); } @Override protected void pageLogic() { manager.beginTransaction().replace(R.id.Frame_Layout,aFragment).commit(); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.ShouYe_Btn: manager.beginTransaction().replace(R.id.Frame_Layout,aFragment).commit(); break; case R.id.DingDan_Btn: manager.beginTransaction().replace(R.id.Frame_Layout,bFragment).commit(); break; } }
}
2.activity_main
<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"> <LinearLayout android:id="@+id/Line" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true"> <Button android:id="@+id/ShouYe_Btn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="首頁" /> <Button android:id="@+id/DingDan_Btn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="訂單" /> </LinearLayout> <FrameLayout android:id="@+id/Frame_Layout" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/Line"></FrameLayout>
/RelativeLayout>
3.AFragment recycleview Xrecycleview
public class AFragment extends Fragment implements IVIew {
private ArrayList<MyData1.ResultsBean> mList = new ArrayList<>(); private ArrayList<MyData2.DataBean> mList2=new ArrayList<>(); private RecyclerView mRecy; private MyAdapter1 myAdapter1; private PresenterImpl presenter; private String mUrl1 = "https://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/1"; private String mUrl2 = "http://www.wanandroid.com/tools/mockapi/6523/restaurants_offset_0_limit_4"; private XRecyclerView mXrecy; private XRecyAdapter xRecyAdapter; private LinearLayoutManager manager1; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_a, null); initView(view); pageLogic(); return view; } private void pageLogic() { presenter = new PresenterImpl(this); presenter.startRequest(mUrl1, null); presenter.startXrecy(mUrl2); } private void initView(View view) { mRecy = view.findViewById(R.id.Recy_View1); myAdapter1 = new MyAdapter1(mList, getContext()); mRecy.setAdapter(myAdapter1); GridLayoutManager manager = new GridLayoutManager(getContext(), 2, GridLayoutManager.HORIZONTAL, false); mRecy.setLayoutManager(manager); //xrecy mXrecy = view.findViewById(R.id.Xrecy_View); xRecyAdapter = new XRecyAdapter(mList2,getContext()); mXrecy.setAdapter(xRecyAdapter); //管理類 LinearLayoutManager manager1= new LinearLayoutManager(getContext()); mXrecy.setLayoutManager(manager1); //XRecyclerView監聽事件 mXrecy.setLoadingListener(new XRecyclerView.LoadingListener() { @Override public void onRefresh() { mList2.clear(); presenter.startXrecy(mUrl2); mXrecy.refreshComplete(); } @Override public void onLoadMore() { presenter.startXrecy(mUrl2); mXrecy.loadMoreComplete(); } }); } @Override public void success(Object data, int type) { switch (type) { case 0: MyData1 myData1 = (MyData1) data; mList.addAll(myData1.getResults()); myAdapter1.notifyDataSetChanged(); break; case 1: MyData2 myData2= (MyData2) data; mList2.addAll(myData2.getData()); xRecyAdapter.notifyDataSetChanged(); break; } } @Override public void error(Object error) { } //切換頁面清空集合 @Override public void onStop() { super.onStop(); mList2.clear(); mList.clear(); } //防止記憶體洩漏 @Override public void onDestroy() { super.onDestroy(); presenter.onDetach(); }
}
4.fragment_a
<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment.AFragment">
<android.support.v7.widget.RecyclerView
android:id="@+id/Recy_View1"
android:layout_width="match_parent"
android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
<com.jcodecraeer.xrecyclerview.XRecyclerView
android:id="@+id/Xrecy_View"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/Recy_View1"></com.jcodecraeer.xrecyclerview.XRecyclerView>
/RelativeLayout>
5.GridLayoutManager 介面卡
public class MyAdapter1 extends
RecyclerView.Adapter<MyAdapter1.ViewHolder> {
private ArrayList<MyData1.ResultsBean>mList;
private Context mContext;
public MyAdapter1(ArrayList<MyData1.ResultsBean> mList, Context mContext) {
this.mList = mList;
this.mContext = mContext;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = View.inflate(mContext, R.layout.item1, null);
ViewHolder holder=new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
Glide.with(mContext).load(mList.get(i).getUrl()).into(viewHolder.img);
viewHolder.tv.setText(mList.get(i).getWho());
}
@Override
public int getItemCount() {
return mList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private ImageView img;
private TextView tv;
public ViewHolder(@NonNull View itemView) {
super(itemView);
img=itemView.findViewById(R.id.Image1);
tv=itemView.findViewById(R.id.Text_Tv1);
}
}
}
介面卡1佈局
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/Image1"
android:layout_gravity="center_horizontal"
android:src="@mipmap/ic_launcher_round"
android:layout_width="150dp"
android:layout_height="150dp" />
<TextView
android:id="@+id/Text_Tv1"
android:layout_gravity="center_horizontal"
android:text="hahah"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
/LinearLayout>
6.XrecycleView 介面卡 封裝類的時候有一個屬性改成布林型別
public class XRecyAdapter extends RecyclerView.Adapter<XRecyAdapter.ViewHolde> {
private ArrayList<MyData2.DataBean> mList;
private Context mContext;
public XRecyAdapter(ArrayList<MyData2.DataBean> mList, Context mContext) {
this.mList = mList;
this.mContext = mContext;
}
@NonNull
@Override
public ViewHolde onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = View.inflate(mContext, R.layout.item2, null);
ViewHolde holde=new ViewHolde(view);
return holde;
}
@Override
public void onBindViewHolder(@NonNull ViewHolde viewHolde, int i) {
viewHolde.tv.setText(mList.get(i).getName());
Glide.with(mContext).load(mList.get(i).getPic_url()).into(viewHolde.img);
}
@Override
public int getItemCount() {
return mList.size();
}
public class ViewHolde extends RecyclerView.ViewHolder {
private ImageView img;
private TextView tv;
public ViewHolde(@NonNull View itemView) {
super(itemView);
img=itemView.findViewById(R.id.image2);
tv=itemView.findViewById(R.id.Text2);
}
}
}
佈局
<LinearLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
android:orientation="horizontal" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="@mipmap/ic_launcher_round"
android:id="@+id/image2"
android:layout_width="150dp"
android:layout_height="150dp" />
<TextView
android:id="@+id/Text2"
android:text="哈哈哈"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
/LinearLayout>
7.BFragment 購物車邏輯
public class BFragment extends Fragment implements IVIew {
private String murl = "http://www.wanandroid.com/tools/mockapi/6523/restaurant-list";
private ArrayList<MyData3.DataBean> mList = new ArrayList<>();
private ExpandableListView mExpandView;
private GWCAdapter mAdapter;
private PresenterImpl presenter;
private CheckBox Check_All;
private TextView All_Price;
private TextView Go_To_JS;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_b, null);
initView(view);
pageLogic();
return view;
}
private void pageLogic() {
presenter = new PresenterImpl(this);
presenter.startExpandView(murl);
}
private void initView(View view) {
//底部按鈕
Check_All = view.findViewById(R.id.Check_All);
All_Price = view.findViewById(R.id.All_Price);
Go_To_JS = view.findViewById(R.id.Go_To_JS);
mExpandView = view.findViewById(R.id.Expand_View);
mAdapter = new GWCAdapter(mList, getContext());
mExpandView.setAdapter(mAdapter);
//去掉選中箭頭
mExpandView.setGroupIndicator(null);
mAdapter.setCallBack(new GWCAdapter.AdapterCallback() {
@Override
public void setGroupCheck(int groupPosition) {
boolean childAllCheck = mAdapter.isChildAllCheck(groupPosition);
mAdapter.childAllCheck(groupPosition, !childAllCheck);
mAdapter.notifyDataSetChanged();
flushBottom();
}
@Override
public void setChildCheck(int groupPosition, int childPosition) {
//得到你要點選的商品Child是否是選中狀態
boolean childChecked = mAdapter.isChildChecked(groupPosition, childPosition);
mAdapter.setChildChecked(groupPosition, childPosition, !childChecked);
mAdapter.notifyDataSetChanged();
flushBottom();
}
@Override
public void setNumber(int groupPosition, int childPosition, int number) {
mAdapter.notifyDataSetChanged();
flushBottom();
}
});
Check_All.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean allGoods = mAdapter.isAllGoods();
mAdapter.setAllGoodsIsChecked(!allGoods);
mAdapter.notifyDataSetChanged();
flushBottom();
}
});
}
@Override
public void success(Object data, int type) {
switch (type) {
case 2:
MyData3 myData3 = (MyData3) data;
mList.addAll(myData3.getData());
mAdapter.notifyDataSetChanged();
//首次進入載入資料
for (int i = 0; i < mList.size(); i++) {
mExpandView.expandGroup(i);
}
//點選組條目不能收縮
mExpandView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
return true;
}
});
break;
}
}
@Override
public void error(Object error) {
}
private void flushBottom() {
boolean allGoods = mAdapter.isAllGoods();
Check_All.setChecked(allGoods);
float allGoodPrice = mAdapter.getAllGoodPrice();
int allGoodNumber = mAdapter.getAllGoodNumber();
All_Price.setText("價格:" + allGoodPrice);
Go_To_JS.setText("去結算(" + allGoodNumber + ")");
}
@Override
public void onStop() {
super.onStop();
mList.clear();
}
}
佈局 fragment_b
<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment.BFragment">
<RelativeLayout
android:id="@+id/bottom_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<CheckBox
android:id="@+id/Check_All"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全選" />
<TextView
android:id="@+id/All_Price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="價格" />
<TextView
android:id="@+id/Go_To_JS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="去結算(0)" />
</RelativeLayout>
<ExpandableListView
android:layout_above="@+id/bottom_layout"
android:id="@+id/Expand_View"
android:layout_width="match_parent"
android:layout_height="match_parent"></ExpandableListView>
/RelativeLayout>
8.購物車 介面卡
public class GWCAdapter extends BaseExpandableListAdapter {
private ArrayList<MyData3.DataBean> mList;
private Context mContext;
public GWCAdapter(ArrayList<MyData3.DataBean> mList, Context mContext) {
this.mList = mList;
this.mContext = mContext;
}
@Override
public int getGroupCount() {
return mList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return mList.get(groupPosition).getSpus().size();
}
@Override
public Object getGroup(int groupPosition) {
return null;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
GroupHolder holder = null;
if (convertView == null) {
convertView = View.inflate(mContext, R.layout.group_item, null);
holder = new GroupHolder();
holder.mGroupCheck = convertView.findViewById(R.id.Group_Check);
holder.mGroupTv = convertView.findViewById(R.id.Group_Name);
convertView.setTag(holder);
} else {
holder = (GroupHolder) convertView.getTag();
}
holder.mGroupTv.setText(mList.get(groupPosition).getName());
//自動選中 當子條目全部選中 小組的checkbox自動會被選中
boolean childAllCheck = isChildAllCheck(groupPosition);
holder.mGroupCheck.setChecked(childAllCheck);
//點選事件
holder.mGroupCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (adapterCallback != null) {
adapterCallback.setGroupCheck(groupPosition);
}
}
});
return convertView;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildHolder cholder = null;
if (convertView == null) {
convertView = View.inflate(mContext, R.layout.child_item, null);
cholder = new ChildHolder();
cholder.mChildCheck = convertView.findViewById(R.id.Child_Check);
cholder.mChildTitle = convertView.findViewById(R.id.Chile_Name);
cholder.mChildPrice = convertView.findViewById(R.id.Chile_Price);
cholder.mImage = convertView.findViewById(R.id.Child_Img);
cholder.jiaJianView=convertView.findViewById(R.id.jiajian_text);
convertView.setTag(cholder);
} else {
cholder = (ChildHolder) convertView.getTag();
}
MyData3.DataBean.SpusBean spusBean = mList.get(groupPosition).getSpus().get(childPosition);
Glide.with(mContext).load(spusBean.getPic_url()).into(cholder.mImage);
cholder.mChildPrice.setText(spusBean.getSkus().get(0).getPrice());
cholder.mChildTitle.setText(spusBean.getName() + "");
//商品數量賦值 後臺 介面回撥
cholder.jiaJianView.setNum(spusBean.getPraise_num());
//子checkxbox賦值
cholder.mChildCheck.setChecked(spusBean.isChildChecked());
//子 點選事件
cholder.mChildCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (adapterCallback != null) {
adapterCallback.setChildCheck(groupPosition, childPosition);
}
}
});
//商品數量介面回撥賦值 別忘了查詢自定義元件
cholder.jiaJianView.setOnChange(new JiaJianView.OnCountChange() {
@Override
public void setCount(int count) {
MyData3.DataBean.SpusBean spusBean = mList.get(groupPosition).getSpus().get(childPosition);
spusBean.setPraise_num(count);
if (adapterCallback != null) {
adapterCallback.setNumber(groupPosition,childPosition,count);
}
}
});
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
class GroupHolder {
private CheckBox mGroupCheck;
private TextView mGroupTv;
}
class ChildHolder {
private CheckBox mChildCheck;
private TextView mChildTitle;
private ImageView mImage;
private TextView mChildPrice;
private JiaJianView jiaJianView;
}
//1.點選group的checkbox讓child的全選中 只有全選 全不選功能
public void childAllCheck(int groupPosition, boolean isCheck) {
MyData3.DataBean dataBean = mList.get(groupPosition);
List<MyData3.DataBean.SpusBean> spus = dataBean.getSpus();
for (int i = 0; i < spus.size(); i++) {
MyData3.DataBean.SpusBean spusBean = spus.get(i);
spusBean.setChildChecked(isCheck);
}
}
//2.判斷小組是否全部選中
public boolean isChildAllCheck(int groupPosition) {
boolean boo = true;
MyData3.DataBean dataBean = mList.get(groupPosition);
List<MyData3.DataBean.SpusBean> spus = dataBean.getSpus();
for (int i = 0; i < spus.size(); i++) {
MyData3.DataBean.SpusBean spusBean = spus.get(i);
//只要有一個沒選中 點選checkbox就是全選功能
if (!spusBean.isChildChecked()) {
return false;
}
}
return boo;
}
//3.點選child給他賦值
public void setChildChecked(int groupPosition, int childPosition, boolean isCheckBox) {
MyData3.DataBean.SpusBean spusBean = mList.get(groupPosition).getSpus().get(childPosition);
spusBean.setChildChecked(isCheckBox);
}
//4.檢視當前商品是否被選中
public boolean isChildChecked(int groupPosition, int childPosition) {
MyData3.DataBean.SpusBean spusBean = mList.get(groupPosition).getSpus().get(childPosition);
if (spusBean.isChildChecked()) {
return true;
}
return false;
}
//5.底部檢視有一個全選按鈕 得到所有條目的狀態
public boolean isAllGoods() {
boolean boo = true;
for (int i = 0; i < mList.size(); i++) {
MyData3.DataBean dataBean = mList.get(i);
for (int j = 0; j < dataBean.getSpus().size(); j++) {
MyData3.DataBean.SpusBean spusBean = dataBean.getSpus().get(j);
if (!spusBean.isChildChecked()) {
boo = false;
}
}
}
return boo;
}
//6.全選反選功能
public void setAllGoodsIsChecked(boolean isAllCheck) {
for (int i = 0; i < mList.size(); i++) {
MyData3.DataBean dataBean = mList.get(i);
for (int j = 0; j < dataBean.getSpus().size(); j++) {
MyData3.DataBean.SpusBean spusBean = dataBean.getSpus().get(j);
spusBean.setChildChecked(isAllCheck);
}
}
}
//7.計算所有商品的價格
public float getAllGoodPrice() {
float allPrice = 0;
for (int i = 0; i < mList.size(); i++) {
MyData3.DataBean dataBean = mList.get(i);
for (int j = 0; j < dataBean.getSpus().size(); j++) {
MyData3.DataBean.SpusBean spusBean = dataBean.getSpus().get(j);
if (spusBean.isChildChecked()) {
allPrice += spusBean.getPraise_num() * Float.parseFloat(spusBean.getSkus().get(0).getPrice());
}
}
}
return allPrice;
}
//8.計算所有的商品數量
public int getAllGoodNumber() {
int allNumber = 0;
for (int i = 0; i < mList.size(); i++) {
MyData3.DataBean dataBean = mList.get(i);
for (int j = 0; j < dataBean.getSpus().size(); j++) {
MyData3.DataBean.SpusBean spusBean = dataBean.getSpus().get(j);
if (spusBean.isChildChecked()) {
allNumber += spusBean.getPraise_num();
}
}
}
return allNumber;
}
//介面回撥 宣告一個介面
public interface AdapterCallback {
//小組
void setGroupCheck(int groupPosition);
//子條目
void setChildCheck(int groupPosition, int childPosition);
//點選加減按鈕重新整理
void setNumber(int groupPosition, int childPosition, int number);
}
private AdapterCallback adapterCallback;
public void setCallBack(AdapterCallback madapterCallback) {
this.adapterCallback = madapterCallback;
}
}
介面卡 佈局
1.group_item
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<CheckBox
android:id="@+id/Group_Check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"/>
<TextView
android:id="@+id/Group_Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hahah "/>
/LinearLayout>
2.child_item
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="120dp"
android:gravity="center_vertical">
<CheckBox
android:layout_marginLeft="20dp"
android:id="@+id/Child_Check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:layout_marginLeft="20dp"
android:id="@+id/Child_Img"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@mipmap/ic_launcher_round"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:text="hahah"
android:id="@+id/Chile_Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="hahah"
android:layout_marginTop="10dp"
android:id="@+id/Chile_Price"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<com.bawei.wangyaxiao.yuekaomoni.weight.JiaJianView
android:id="@+id/jiajian_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></com.bawei.wangyaxiao.yuekaomoni.weight.JiaJianView>
/LinearLayout>
9.自定義view 加減器
public class JiaJianView extends LinearLayout implements View.OnClickListener {
private TextView mDelete;
private TextView mAdd;
private TextView mShow;
private int mConut;
public JiaJianView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.jiajianview, this);
initView();
}
private void initView() {
mDelete = findViewById(R.id.Delete_Tv);
mAdd = findViewById(R.id.Add_Tv);
mShow = findViewById(R.id.Show_Tv);
mDelete.setOnClickListener(this);
mAdd.setOnClickListener(this);
}
public void setNum(int number) {
this.mConut = number;
mShow.setText(number + "");
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.Delete_Tv:
if (mConut > 0) {
mConut--;
mShow.setText(mConut + "");
if(mOnCountChange!=null){
mOnCountChange.setCount(mConut);
}
}else{
Toast.makeText(getContext(), "商品已售空", Toast.LENGTH_SHORT).show();
}
break;
case R.id.Add_Tv:
mConut++;
mShow.setText(mConut + "");
if (mOnCountChange != null) {
mOnCountChange.setCount(mConut);
}
break;
}
}
//介面回撥
public interface OnCountChange {
void setCount(int count);
}
private OnCountChange mOnCountChange;
public void setOnChange(OnCountChange onCountChange) {
this.mOnCountChange = onCountChange;
}
}
佈局
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/Delete_Tv"
android:layout_width="10dp"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:background="#ffffff"
android:gravity="center"
android:text="-"
android:textSize="16sp" />
<TextView
android:id="@+id/Show_Tv"
android:layout_width="18dp"
android:layout_height="match_parent"
android:background="#ffffff"
android:gravity="center"
android:layout_weight="1"
android:text="1" />
<TextView
android:id="@+id/Add_Tv"
android:layout_width="10dp"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:background="#ffffff"
android:gravity="center"
android:text="+"
android:textSize="16sp" />
/LinearLayout>
10.工具類
public class OkUtils {
private OkHttpClient okHttpClient;
private OkUtils() {
//攔截器依賴: implementation 'com.squareup.okhttp3:logging-interceptor:3.12.0'
HttpLoggingInterceptor loggingInterceptor=new HttpLoggingInterceptor();
okHttpClient = new OkHttpClient.Builder()
.connectTimeout(20,TimeUnit.SECONDS)
.callTimeout(20,TimeUnit.SECONDS)
.readTimeout(20,TimeUnit.SECONDS)
.addInterceptor(loggingInterceptor)
.build();
}
public static OkUtils getInstance() {
return OkHolder.okutils;
}
//私有靜態方法
private static class OkHolder {
//例項化
private static final OkUtils okutils = new OkUtils();
}
public void getAsync(String url, Callback callback){
Request request=new Request.Builder().url(url).build();
okHttpClient.newCall(request).enqueue(callback);
}
}
11.model層
public class ModelImpl implements Model {
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
String str = (String) msg.obj;
/* 、 JSONObject jsonObject = new JSONObject();
// String string = jsonObject.optString("status");
int status = jsonObject.optInt("status");
if (status==200){
}*/
Gson gson = new Gson();
MyData1 myData1 = gson.fromJson(str, MyData1.class);
callBack.getData(myData1, 0);
break;
case 1:
String str1 = (String) msg.obj;
Gson gson1 = new Gson();
MyData2 myData2 = gson1.fromJson(str1, MyData2.class);
callBack.getData(myData2, 1);
break;
case 2:
String str2 = (String) msg.obj;
Gson gson2=new Gson();
MyData3 myData3 = gson2.fromJson(str2, MyData3.class);
callBack.getData(myData3,2);
break;
}
}
};
private MyCallBack callBack;
@Override
public void setData(String url, String params, MyCallBack callBack) {
this.callBack = callBack;
OkUtils.getInstance().getAsync(url, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
mHandler.sendMessage(mHandler.obtainMessage(0, string));
}
});
}
@Override
public void setXrecydata(String url, MyCallBack callBack) {
this.callBack =callBack;
OkUtils.getInstance().getAsync(url, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
mHandler.sendMessage(mHandler.obtainMessage(1, string));
}
});
}
@Override
public void ExpandView(String url, MyCallBack callBack) {
this.callBack =callBack;
OkUtils.getInstance().getAsync(url, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
mHandler.sendMessage(mHandler.obtainMessage(2, string));
}
});
}
}
12.presenter 層
public class PresenterImpl implements IPresenter{
private IVIew ivIew;
private ModelImpl model;
public PresenterImpl(IVIew ivIew) {
this.ivIew = ivIew;
model=new ModelImpl();
}
@Override
public void startRequest(String url, String params) {
model.setData(url, params, new MyCallBack() {
@Override
public void getData(Object data, int type) {
ivIew.success(data,type);
}
@Override
public void error(Object error) {
ivIew.error(error);
}
});
}
@Override
public void startXrecy(String url) {
model.setXrecydata(url, new MyCallBack() {
@Override
public void getData(Object data, int type) {
ivIew.success(data,type);
}
@Override
public void error(Object error) {
ivIew.error(error);
}
});
}
@Override
public void startExpandView(String url) {
model.ExpandView(url, new MyCallBack() {
@Override
public void getData(Object data, int type) {
ivIew.success(data,type);
}
@Override
public void error(Object error) {
ivIew.error(error);
}
});
}
public void onDetach(){
if(model!=null){
model=null;
}
if(ivIew!=null){
ivIew=null;
}
}
}
13.baseactivity
public abstract class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
protected abstract int getLayout();
protected abstract void initView();
protected abstract void setOnClick();
protected abstract void pageLogic();
void init(){
if(getLayout()!=0){
setContentView(getLayout());
initView();
setOnClick();
pageLogic();
}else{
throw new IllegalStateException("請載入佈局");
}
}
}