bean类。
创建view层
public interface IOrderView {
void showOrder(List<OrderBean.DataBean> list);
}
创建model层
public interface IOrderModel {
void showDate(Order order);
interface Order{
void complate(List<OrderBean.DataBean> list);
}
}
public class OrderModel implements IOrderModel {
@Override
public void showDate(final Order order) {
RetrofitUtils.getInstance().order()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<OrderBean>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
Log.i("=========", "onError: "+e.toString());
}
@Override
public void onNext(OrderBean orderBean) {
List<OrderBean.DataBean> list = orderBean.getData();
order.complate(list);
}
});
}
}
创建presenter层
public interface IPreesenter<T> {
void attach(T view);
void deach();
}
public class OrderPresenter implements IPreesenter<IOrderView>{
IOrderModel model;
SoftReference<IOrderView> softReference;
public OrderPresenter(IOrderView view) {
attach(view);
model = new OrderModel();
}
public void DateOrder(){
model.showDate(new IOrderModel.Order() {
@Override
public void complate(List<OrderBean.DataBean> list) {
softReference.get().showOrder(list);
}
});
}
@Override
public void attach(IOrderView view) {
softReference = new SoftReference<IOrderView>(view);
}
@Override
public void deach() {
softReference.clear();
}
}
接口
public interface APIService {
@GET("product/getOrders?uid=71")
Observable<OrderBean> order();
@GET("product/updateOrder")
Call<ResponseBody> update(@Query("uid") int uid,@Query("status") int status,@Query("orderId") int orderId);
}
工具类
public class RetrofitUtils {
private static APIService service = null ;
public static APIService getInstance(){
if(service == null){
synchronized (RetrofitUtils.class){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://www.zhaoapi.cn/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
//.client(OkHttpUtils.getInstance())
.build();
service = retrofit.create(APIService.class);
}
}
return service;
}
}
创建adpter
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
Context context;
List<OrderBean.DataBean> list;
OrderPresenter presenter;
public MyAdapter(Context context, List<OrderBean.DataBean> list,OrderPresenter presenter) {
this.context = context;
this.list = list;
this.presenter = presenter;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.qb_item,parent, false);;
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
((MyViewHolder)holder).mTitleText.setText(list.get(position).getTitle());
((MyViewHolder)holder).mPriceText.setText("价格: "+list.get(position).getPrice());
((MyViewHolder)holder).mTameText.setText(list.get(position).getCreatetime());
if (list.get(position).getStatus()==0){
((MyViewHolder) holder).mDaizhifuText.setText("待支付");
((MyViewHolder) holder).mDaizhifuText.setTextColor(Color.RED);
((MyViewHolder) holder).mBtnText.setText("取消订单");
((MyViewHolder) holder).mBtnText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("提示");
builder.setMessage("确定要取消订单吗?");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
retrofit2.Call<ResponseBody> call = RetrofitUtils.getInstance().update(71,2,list.get(position).getOrderid());
call.enqueue(new retrofit2.Callback<ResponseBody>() {
@Override
public void onResponse(retrofit2.Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
presenter.DateOrder();
notifyDataSetChanged();
}
@Override
public void onFailure(retrofit2.Call<ResponseBody> call, Throwable t) {
}
});
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
});
}else if (list.get(position).getStatus()==1){
((MyViewHolder) holder).mDaizhifuText.setText("已支付");
((MyViewHolder) holder).mDaizhifuText.setTextColor(Color.BLACK);
((MyViewHolder) holder).mBtnText.setText("查看订单");
}else if (list.get(position).getStatus()==2){
((MyViewHolder) holder).mDaizhifuText.setText("已取消");
((MyViewHolder) holder).mDaizhifuText.setTextColor(Color.BLACK);
((MyViewHolder) holder).mBtnText.setText("查看订单");
((MyViewHolder) holder).mBtnText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("提示");
builder.setMessage("确定循环利用吗?");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
retrofit2.Call<ResponseBody> call = RetrofitUtils.getInstance().update(71,0,list.get(position).getOrderid());
call.enqueue(new retrofit2.Callback<ResponseBody>() {
@Override
public void onResponse(retrofit2.Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
presenter.DateOrder();
notifyDataSetChanged();
}
@Override
public void onFailure(retrofit2.Call<ResponseBody> call, Throwable t) {
}
});
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
});
}
}
@Override
public int getItemCount() {
if (list!=null){
return list.size();
}
return 0;
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView mTitleText;
TextView mDaizhifuText;
TextView mPriceText;
TextView mTameText;
TextView mBtnText;
public MyViewHolder(View itemView) {
super(itemView);
mTitleText = (TextView) itemView.findViewById(R.id.text_title);
mDaizhifuText = (TextView) itemView.findViewById(R.id.text_daizhifu);
mPriceText = (TextView) itemView.findViewById(R.id.text_price);
mTameText = (TextView) itemView.findViewById(R.id.text_tame);
mBtnText = (TextView) itemView.findViewById(R.id.text_btn);
}
}
}
public class MyFrag extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
Context context;
List<OrderBean.DataBean> list;
OrderPresenter presenter;
public MyFrag(Context context, List<OrderBean.DataBean> list, OrderPresenter presenter) {
this.context = context;
this.list = list;
this.presenter = presenter;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.daizhifu,parent, false);;
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
if (list.get(position).getStatus()==0){
((MyViewHolder)holder).ll.setVisibility(View.VISIBLE);
((MyViewHolder)holder).mTitleText.setText(list.get(position).getTitle());
((MyViewHolder)holder).mPriceText.setText("价格: "+list.get(position).getPrice());
((MyViewHolder)holder).mTameText.setText(list.get(position).getCreatetime());
((MyViewHolder) holder).mDaizhifuText.setText("待支付");
((MyViewHolder) holder).mDaizhifuText.setTextColor(Color.RED);
((MyViewHolder) holder).mBtnText.setText("取消订单");
((MyViewHolder) holder).mBtnText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("提示");
builder.setMessage("确定要取消订单吗?");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
retrofit2.Call<ResponseBody> call = RetrofitUtils.getInstance().update(71,2,list.get(position).getOrderid());
call.enqueue(new retrofit2.Callback<ResponseBody>() {
@Override
public void onResponse(retrofit2.Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
presenter.DateOrder();
notifyDataSetChanged();
}
@Override
public void onFailure(retrofit2.Call<ResponseBody> call, Throwable t) {
}
});
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
});
}
}
@Override
public int getItemCount() {
if (list!=null){
return list.size();
}
return 0;
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView mTitleText;
TextView mDaizhifuText;
TextView mPriceText;
TextView mTameText;
TextView mBtnText;
LinearLayout ll;
public MyViewHolder(View itemView) {
super(itemView);
mTitleText = (TextView) itemView.findViewById(R.id.text_title);
mDaizhifuText = (TextView) itemView.findViewById(R.id.text_daizhifu);
mPriceText = (TextView) itemView.findViewById(R.id.text_price);
mTameText = (TextView) itemView.findViewById(R.id.text_tame);
mBtnText = (TextView) itemView.findViewById(R.id.text_btn);
ll = itemView.findViewById(R.id.ll);
}
}
}
public class MyQx extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
Context context;
List<OrderBean.DataBean> list;
OrderPresenter presenter;
public MyQx(Context context, List<OrderBean.DataBean> list,OrderPresenter presenter) {
this.context = context;
this.list = list;
this.presenter = presenter;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.qx_item,parent, false);;
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
if (list.get(position).getStatus()==2){
((MyViewHolder)holder).ll.setVisibility(View.VISIBLE);
((MyViewHolder)holder).mTitleText.setText(list.get(position).getTitle());
((MyViewHolder)holder).mPriceText.setText("价格: "+list.get(position).getPrice());
((MyViewHolder)holder).mTameText.setText(list.get(position).getCreatetime());
((MyViewHolder) holder).mDaizhifuText.setText("已取消");
((MyViewHolder) holder).mDaizhifuText.setTextColor(Color.BLACK);
((MyViewHolder) holder).mBtnText.setText("查看订单");
((MyViewHolder) holder).mBtnText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("提示");
builder.setMessage("确定循环利用吗?");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
retrofit2.Call<ResponseBody> call = RetrofitUtils.getInstance().update(71,0,list.get(position).getOrderid());
call.enqueue(new retrofit2.Callback<ResponseBody>() {
@Override
public void onResponse(retrofit2.Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
presenter.DateOrder();
notifyDataSetChanged();
}
@Override
public void onFailure(retrofit2.Call<ResponseBody> call, Throwable t) {
}
});
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
});
}
}
@Override
public int getItemCount() {
if (list != null){
return list.size();
}
return 0;
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView mTitleText;
TextView mDaizhifuText;
TextView mPriceText;
TextView mTameText;
TextView mBtnText;
LinearLayout ll;
public MyViewHolder(View itemView) {
super(itemView);
mTitleText = (TextView) itemView.findViewById(R.id.text_title);
mDaizhifuText = (TextView) itemView.findViewById(R.id.text_daizhifu);
mPriceText = (TextView) itemView.findViewById(R.id.text_price);
mTameText = (TextView) itemView.findViewById(R.id.text_tame);
mBtnText = (TextView) itemView.findViewById(R.id.text_btn);
ll = itemView.findViewById(R.id.ll);
}
}
}
public class MyZf extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
Context context;
List<OrderBean.DataBean> list;
private List<String> stringList;
public MyZf(Context context, List<OrderBean.DataBean> list) {
this.context = context;
this.list = list;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.yizhifu,parent, false);;
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
stringList = new ArrayList<>();
if (list.get(position).getStatus()==1){
// stringList.add(list.get(position).getTitle());
//
// System.out.println("****"+stringList.size());
((MyViewHolder)holder).ll.setVisibility(View.VISIBLE);
((MyViewHolder)holder).mTitleText.setText(list.get(position).getTitle());
((MyViewHolder)holder).mPriceText.setText("价格: "+list.get(position).getPrice());
((MyViewHolder)holder).mTameText.setText(list.get(position).getCreatetime());
((MyViewHolder) holder).mDaizhifuText.setText("已支付");
((MyViewHolder) holder).mDaizhifuText.setTextColor(Color.BLACK);
((MyViewHolder) holder).mBtnText.setText("查看订单");
}
}
@Override
public int getItemCount() {
if (list != null){
return list.size();
}
return 0;
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView mTitleText;
TextView mDaizhifuText;
TextView mPriceText;
TextView mTameText;
TextView mBtnText;
LinearLayout ll;
public MyViewHolder(View itemView) {
super(itemView);
mTitleText = (TextView) itemView.findViewById(R.id.text_title);
mDaizhifuText = (TextView) itemView.findViewById(R.id.text_daizhifu);
mPriceText = (TextView) itemView.findViewById(R.id.text_price);
mTameText = (TextView) itemView.findViewById(R.id.text_tame);
mBtnText = (TextView) itemView.findViewById(R.id.text_btn);
ll = itemView.findViewById(R.id.ll);
}
}
}
主activity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private RadioGroup radioGroup;
private RadioButton button_1;
private RadioButton button_2;
private RadioButton button_3;
private RadioButton button_4;
private Fragment_1 fragment_1;
private Fragment_2 fragment_2;
private Fragment_3 fragment_3;
private Fragment_4 fragment_4;
private List<Fragment> list;
private FrameLayout frameLayout;
private ImageView mBtnImage;
private View item_popup;
private PopupWindow popupWindow;
private View view;
private TextView btn_daizhi;
private TextView btn_yizhi;
private TextView btn_yiqu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
frameLayout = (FrameLayout) findViewById(R.id.framelayout);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
//找到四个按钮
button_1 = (RadioButton) findViewById(R.id.button_1);
button_2 = (RadioButton) findViewById(R.id.button_2);
button_3 = (RadioButton) findViewById(R.id.button_3);
button_4 = (RadioButton) findViewById(R.id.button_4);
//创建Fragment对象及集合
fragment_1 = new Fragment_1();
fragment_2 = new Fragment_2();
fragment_3 = new Fragment_3();
fragment_4 = new Fragment_4();
//将Fragment对象添加到list中
list = new ArrayList<>();
list.add(fragment_1);
list.add(fragment_2);
list.add(fragment_3);
list.add(fragment_4);
//设置RadioGroup开始时设置的按钮,设置第一个按钮为默认值
radioGroup.check(R.id.button_1);
//设置按钮点击监听
button_1.setOnClickListener(this);
button_2.setOnClickListener(this);
button_3.setOnClickListener(this);
button_4.setOnClickListener(this);
//初始时向容器中添加第一个Fragment对象
addFragment(fragment_1);
mBtnImage = (ImageView) findViewById(R.id.image_btn);
mBtnImage.setOnClickListener(this);
item_popup = View.inflate(this, R.layout.item_popup, null);
view = View.inflate(this, R.layout.activity_main, null);
popupWindow = new PopupWindow(item_popup, ActionBar.LayoutParams.WRAP_CONTENT,ActionBar.LayoutParams.WRAP_CONTENT);
popupWindow.setTouchable(true);
btn_daizhi = item_popup.findViewById(R.id.btn_daizhi);
btn_yizhi = item_popup.findViewById(R.id.btn_yizhi);
btn_yiqu = item_popup.findViewById(R.id.btn_yiqu);
btn_daizhi.setOnClickListener(this);
btn_yizhi.setOnClickListener(this);
btn_yiqu.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//我们根据参数的id区别不同按钮
//不同按钮对应着不同的Fragment对象页面
switch (v.getId()) {
case R.id.button_1:
addFragment(fragment_1);
break;
case R.id.button_2:
addFragment(fragment_2);
break;
case R.id.button_3:
addFragment(fragment_3);
break;
case R.id.button_4:
addFragment(fragment_4);
break;
case R.id.image_btn:
popupWindow.showAsDropDown(mBtnImage);
break;
case R.id.btn_daizhi:
addFragment(fragment_2);
popupWindow.dismiss();
break;
case R.id.btn_yizhi:
addFragment(fragment_4);
popupWindow.dismiss();
break;
case R.id.btn_yiqu:
addFragment(fragment_3);
popupWindow.dismiss();
break;
default:
break;
}
}
//向Activity中添加Fragment的方法
public void addFragment(Fragment fragment) {
//获得Fragment管理器
FragmentManager fragmentManager = getSupportFragmentManager();
//使用管理器开启事务
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//使用事务替换Fragment容器中Fragment对象
fragmentTransaction.replace(R.id.framelayout, fragment);
//提交事务,否则事务不生效
fragmentTransaction.commit();
}
}
创建fragment
public abstract class BaseFragment<T extends IPreesenter> extends Fragment {
T presenter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
createpresenter();
return super.onCreateView(inflater, container, savedInstanceState);
}
protected abstract void createpresenter();
@Override
public void onDestroy() {
super.onDestroy();
if (presenter != null){
presenter.deach();
}
}
}
public class Fragment_1 extends BaseFragment<OrderPresenter> implements IOrderView {
private RecyclerView mRecy;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = View.inflate(getActivity(), R.layout.fragment_1, null);
mRecy = (RecyclerView)view.findViewById(R.id.recy);
presenter.DateOrder();
return view;
}
@Override
public void showOrder(List<OrderBean.DataBean> list) {
LinearLayoutManager mgs = new LinearLayoutManager(getActivity());
mRecy.setLayoutManager(mgs);
MyAdapter adapter = new MyAdapter(getActivity(),list,presenter);
mRecy.setAdapter(adapter);
}
@Override
protected void createpresenter() {
presenter = new OrderPresenter(this);
}
}
public class Fragment_2 extends BaseFragment<OrderPresenter> implements IOrderView {
RecyclerView recy;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = View.inflate(getActivity(), R.layout.fragment_2,null);
recy = view.findViewById(R.id.recy);
presenter.DateOrder();
return view;
}
@Override
protected void createpresenter() {
presenter = new OrderPresenter(this);
}
@Override
public void showOrder(List<OrderBean.DataBean> list) {
LinearLayoutManager mgs = new LinearLayoutManager(getActivity());
recy.setLayoutManager(mgs);
MyFrag adapter = new MyFrag(getActivity(),list,presenter);
recy.setAdapter(adapter);
}
}
public class Fragment_3 extends BaseFragment<OrderPresenter> implements IOrderView {
RecyclerView recyclerView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = View.inflate(getActivity(), R.layout.fragment_3,null);
recyclerView = view.findViewById(R.id.re);
presenter.DateOrder();
return view;
}
@Override
public void showOrder(List<OrderBean.DataBean> list) {
LinearLayoutManager mgs = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mgs);
MyQx adapter = new MyQx(getActivity(),list,presenter);
recyclerView.setAdapter(adapter);
}
@Override
protected void createpresenter() {
presenter = new OrderPresenter(this);
}
}
public class Fragment_4 extends BaseFragment<OrderPresenter> implements IOrderView {
RecyclerView rv;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = View.inflate(getActivity(), R.layout.fragment_4,null);
presenter.DateOrder();
rv = view.findViewById(R.id.rv);
return view;
}
@Override
protected void createpresenter() {
presenter = new OrderPresenter(this);
}
@Override
public void showOrder(List<OrderBean.DataBean> list) {
LinearLayoutManager mgs = new LinearLayoutManager(getActivity());
rv.setLayoutManager(mgs);
MyZf adapter = new MyZf(getActivity(),list);
rv.setAdapter(adapter);
}
}
网友评论