订单列表---tab---viewpage--实现activity
package com.example.mr.yuekaomoni; import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TableLayout; public class DingdanActivity extends AppCompatActivity { String [] aa={"全部","代付款","已付款","已取消"}; private TabLayout tab; private ViewPager vp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_dingdan); tab = findViewById(R.id.tab); vp = findViewById(R.id.vp); vp.setOffscreenPageLimit(aa.length); //动态添加数据 vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { @Override public CharSequence getPageTitle(int position) { return aa[position]; } //关联的Fragment @Override public Fragment getItem(int position) { Myfrag myfrag = new Myfrag(); Bundle bundle = new Bundle(); bundle.putString("name",aa[position]); myfrag.setArguments(bundle); return myfrag; } //数组长度 @Override public int getCount() { return aa.length; } }); //tab关联page tab.setupWithViewPager(vp); } }
page界面显示列表
package com.example.mr.yuekaomoni; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ListAdapter; import android.widget.ListView; import com.example.mr.yuekaomoni.bean.Dingbean; import com.example.mr.yuekaomoni.utils.OkUrl; import com.google.gson.Gson; import com.handmark.pulltorefresh.library.ILoadingLayout; import com.handmark.pulltorefresh.library.PullToRefreshBase; import com.handmark.pulltorefresh.library.PullToRefreshListView; import java.io.IOException; import java.util.ArrayList; import java.util.List; import okhttp3.Call; import okhttp3.Callback; import okhttp3.Response; /** * Created by Mr赵 on 2017/12/20. */ public class Myfrag extends Fragment { private PullToRefreshListView lv; private List<Dingbean.DataBean> data; List<Dingbean.DataBean> list=new ArrayList<>(); int type=1; private DingAdapter dingAdapter; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.quanbuju, container, false); lv = view.findViewById(R.id.lv); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); lv.setMode(PullToRefreshBase.Mode.BOTH); ILoadingLayout startLabels = lv.getLoadingLayoutProxy(true, false); startLabels.setPullLabel("下拉刷新"); startLabels.setRefreshingLabel("正在拉"); startLabels.setReleaseLabel("放开刷新"); ILoadingLayout endLabels = lv.getLoadingLayoutProxy(false, true); endLabels.setPullLabel("上拉刷新"); endLabels.setRefreshingLabel("正在载入..."); endLabels.setReleaseLabel("放开刷新..."); //监听事件 lv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() { @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) { OkUrl.doget("https://www.zhaoapi.cn/product/getOrders?uid=2834&page=1", new Callback() { @Override public void onFailure(Call call, IOException e) { e.printStackTrace(); } @Override public void onResponse(Call call, Response response) throws IOException { if(response.isSuccessful()){ final String string = response.body().string(); getActivity().runOnUiThread(new Runnable() { @Override public void run() { Gson gson = new Gson(); Dingbean js = gson.fromJson(string, Dingbean.class); data = js.getData(); list.clear(); list.addAll(data); ingo(); lv.onRefreshComplete(); } }); } } }); } @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) { type++; OkUrl.doget("https://www.zhaoapi.cn/product/getOrders?uid=2834&page="+type, new Callback() { @Override public void onFailure(Call call, IOException e) { e.printStackTrace(); } @Override public void onResponse(Call call, Response response) throws IOException { if(response.isSuccessful()){ final String string = response.body().string(); getActivity().runOnUiThread(new Runnable() { @Override public void run() { Gson gson = new Gson(); Dingbean js = gson.fromJson(string, Dingbean.class); data = js.getData(); list.addAll(data); ingo(); lv.onRefreshComplete(); } }); } } }); } }); String name = getArguments().getString("name"); OkUrl.doget("https://www.zhaoapi.cn/product/getOrders?uid=2834&page=1", new Callback() { @Override public void onFailure(Call call, IOException e) { e.printStackTrace(); } @Override public void onResponse(Call call, Response response) throws IOException { if(response.isSuccessful()){ final String string = response.body().string(); getActivity().runOnUiThread(new Runnable() { @Override public void run() { Gson gson = new Gson(); Dingbean js = gson.fromJson(string, Dingbean.class); data = js.getData(); list.addAll(data); ingo(); lv.onRefreshComplete(); } }); } } }); } public void ingo(){ if(dingAdapter==null){ dingAdapter = new DingAdapter(list, getActivity()); lv.setAdapter(dingAdapter); }else{ dingAdapter.notifyDataSetChanged(); } } }
适配器中赋值--事件
package com.example.mr.yuekaomoni; import android.content.Context; import android.os.Handler; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.TextView; import com.example.mr.yuekaomoni.bean.Dingbean; import com.example.mr.yuekaomoni.utils.OkUrl; import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; import java.util.List; import okhttp3.Call; import okhttp3.Callback; import okhttp3.Response; /** * Created by Mr赵 on 2017/12/20. */ public class DingAdapter extends BaseAdapter { List<Dingbean.DataBean> data;; Context ctx; Handler handler=new Handler(); public DingAdapter(List<Dingbean.DataBean> data, Context ctx) { this.data = data; this.ctx = ctx; } @Override public int getCount() { return data.size(); } @Override public Object getItem(int i) { return data.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(final int i, View view, ViewGroup viewGroup) { final hihh hh; if(view==null){ hh=new hihh(); view=View.inflate(ctx, R.layout.dingbuju,null); hh.name=view.findViewById(R.id.name); hh.daifukuan=view.findViewById(R.id.zhuangtai); hh.qian=view.findViewById(R.id.jiage); hh.time=view.findViewById(R.id.time); hh.quxiao=view.findViewById(R.id.quxiao); view.setTag(hh); }else{ hh= (hihh) view.getTag(); } hh.qian.setText("价格:"+data.get(i).getPrice()); hh.name.setText(data.get(i).getTitle()); int status = data.get(i).getStatus(); if(status==0){ hh.daifukuan.setText("代付款"); }else if(status==1){ hh.daifukuan.setText("已付款"); }else if(status==2){ hh.daifukuan.setText("已取消"); } hh.time.setText(data.get(i).getCreatetime()); if(status==0){ hh.quxiao.setText("取消订单"); }else{ hh.quxiao.setText("查看订单"); } /* * 点击取消订单 * */ hh.quxiao.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(hh.quxiao.getText().toString().equals("取消订单")){ OkUrl.doget("https://www.zhaoapi.cn/product/updateOrder?uid=2834&status=2&orderId=" + data.get(i).getOrderid() + "", new Callback() { @Override public void onFailure(Call call, IOException e) { e.printStackTrace(); } @Override public void onResponse(Call call, Response response) throws IOException { if(response.isSuccessful()){ final String string = response.body().string(); handler.post(new Runnable() { @Override public void run() { try { JSONObject jsonObject = new JSONObject(string); String msg = jsonObject.getString("msg"); if(msg.equals("订单状态修改成功")){ hh.daifukuan.setText("已取消"); //修改本地状态 data.get(i).setStatus(2); //刷新适配器 notifyDataSetChanged(); } } catch (Exception e) { e.printStackTrace(); } } }); } } }); } } }); /* * 修改订单 * */ hh.daifukuan.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(hh.daifukuan.getText().toString().equals("代付款")){ OkUrl.doget("https://www.zhaoapi.cn/product/updateOrder?uid=2834&status=1&orderId=" + data.get(i).getOrderid() + "", new Callback() { @Override public void onFailure(Call call, IOException e) { e.printStackTrace(); } @Override public void onResponse(Call call, Response response) throws IOException { if(response.isSuccessful()){ final String string = response.body().string(); handler.post(new Runnable() { @Override public void run() { try { JSONObject jsonObject = new JSONObject(string); String msg = jsonObject.getString("msg"); if(msg.equals("订单状态修改成功")){ hh.daifukuan.setText("已付款"); data.get(i).setStatus(1); notifyDataSetChanged(); } } catch (Exception e) { e.printStackTrace(); } } }); } } }); } } }); return view; } class hihh{ TextView name; TextView daifukuan; TextView qian; TextView time; Button quxiao; } }
订单的条目布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_alignParentLeft="true" android:id="@+id/name" android:text="adsadasdasdasdasda" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_alignParentRight="true" android:id="@+id/zhuangtai" android:text="代付款" android:textColor="#f00" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> <TextView android:layout_marginTop="10dp" android:id="@+id/jiage" android:text="价格:99.99" android:textColor="#f00" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RelativeLayout android:layout_marginTop="10dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_marginTop="15dp" android:layout_alignParentLeft="true" android:id="@+id/time" android:text="创建时间:2017-11-09" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:layout_alignParentRight="true" android:id="@+id/quxiao" android:text="取消订单" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> </LinearLayout>订单布局<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="com.example.mr.yuekaomoni.DingdanActivity"> <LinearLayout android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:text="订单列表" android:textSize="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <android.support.design.widget.TabLayout android:id="@+id/tab" android:layout_width="match_parent" android:layout_height="55dp" app:tabGravity="center" app:tabIndicatorColor="@color/colorAccent" app:tabMode="scrollable" app:tabSelectedTextColor="@color/colorPrimaryDark" app:tabTextColor="@color/colorPrimary" ></android.support.design.widget.TabLayout> <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>