Model
public class ShowDingDanModel {
private IPresenterShowDingDan iPresenterShowDingDan;
public ShowDingDanModel(IPresenterShowDingDan iPresenterShowDingDan) {
this.iPresenterShowDingDan = iPresenterShowDingDan;
}
public void getData(String showdinngdanUrl, int uid, int page) {
Map<String, String> map = OkHttp3Util.CommonParamsInterceptor.map;
map.put("page",page+"");
map.put("uid",uid+"");
OkHttp3Util.doPost(showdinngdanUrl, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if(response.isSuccessful()){
String json = response.body().string();
if(json != null){
Gson gson = new Gson();
ShowDingDanBean showDingDanBean = gson.fromJson(json, ShowDingDanBean.class);
//使用接口回调
iPresenterShowDingDan.onShowSuccess(showDingDanBean);
}
}
}
});
}
}
presenter
public class ShowDingDanPresenter implements IPresenterShowDingDan{
private IViewShowDingDan iViewShowDingDan;
private final ShowDingDanModel showDingDanModel;
public ShowDingDanPresenter(IViewShowDingDan iViewShowDingDan) {
this.iViewShowDingDan = iViewShowDingDan;
showDingDanModel = new ShowDingDanModel(this);
}
public void getData(String showdinngdanUrl, int uid, int page) {
showDingDanModel.getData(showdinngdanUrl,uid,page);
}
@Override
public void onShowSuccess(ShowDingDanBean showDingDanBean) {
iViewShowDingDan.onShowSuccess(showDingDanBean);
}
}
接口
public interface IPresenterShowDingDan {
void onShowSuccess(ShowDingDanBean showDingDanBean);
}
IView
public interface IViewShowDingDan {
void onShowSuccess(ShowDingDanBean showDingDanBean);
}
Activity
public class ShowDingDanActivity extends AppCompatActivity {
private ViewPager vp;
private TabLayout tab_layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_ding_dan);
tab_layout = (TabLayout) findViewById(R.id.tab_layout);
vp = (ViewPager) findViewById(R.id.vp);
//tablayout设置数据
final List<String> tablist = new ArrayList<>();
tablist.add("全部");
tablist.add("待支付");
tablist.add("已支付");
tablist.add("取消");
//设置默认显示
//设置适配器
vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public CharSequence getPageTitle(int position) {
return tablist.get(position);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position){
case 0:
fragment = new AllDingDanFragment();
break;
case 1:
fragment = new DaiZhiFuFragment();
break;
default:
break;
}
return fragment;
}
@Override
public int getCount() {
return tablist.size();
}
});
//设置关联
tab_layout.setupWithViewPager(vp);
}
}
fragment
public class AllDingDanFragment extends Fragment implements IViewShowDingDan {
private XRecyclerView all_xrv;
private ShowDingDanPresenter showDingDanPresenter;
private int page = 1;
List<ShowDingDanBean.DataBean> data = new ArrayList<>();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.alldingdan_layout, container, false);
all_xrv = (XRecyclerView) view.findViewById(R.id.all_xrv);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
showDingDanPresenter = new ShowDingDanPresenter(this);
showDingDanPresenter.getData(APIUtil.SHOWDINNGDAN_URL,2753,1);
}
@Override
public void onShowSuccess(final ShowDingDanBean showDingDanBean) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
//Toast.makeText(getActivity(),showDingDanBean.getData().size()+"",Toast.LENGTH_SHORT).show();
//设置适配器
data.addAll(showDingDanBean.getData());
XRVAdapter adapter = new XRVAdapter(getActivity(),data,showDingDanPresenter);
//设置适配器
all_xrv.setAdapter(adapter);
//设置管理器
all_xrv.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayout.VERTICAL,false));
all_xrv.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
all_xrv.setLoadingMoreProgressStyle(ProgressStyle.BallSpinFadeLoader);
all_xrv.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {
showDingDanPresenter.getData(APIUtil.SHOWDINNGDAN_URL,2753,1);
all_xrv.refreshComplete();
}
@Override
public void onLoadMore() {
page++;
showDingDanPresenter.getData(APIUtil.SHOWDINNGDAN_URL,2753,page);
all_xrv.loadMoreComplete();
}
});
}
});
}
}
布局
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:text="订单列表"
android:textSize="25dp"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<TextView
android:background="#f1ebeb"
android:layout_width="match_parent"
android:layout_height="2dp" />
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
app:tabIndicatorColor="@color/red"
app:tabSelectedTextColor="@color/red"
app:tabTextColor="@color/black"
></android.support.design.widget.TabLayout>
<TextView
android:background="#f1ebeb"
android:layout_width="match_parent"
android:layout_height="2dp" />
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v4.view.ViewPager>
子布局
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/xrv_title"
android:text="标题标题标题标题"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/xrv_state"
android:text="待支付"
android:textColor="#f00"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<TextView
android:id="@+id/xrv_price"
android:textColor="#f00"
android:text="999999"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_marginTop="5dp">
<TextView
android:id="@+id/xrv_time"
android:text="20171221"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/xrv_click"
android:text="取消订单"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="35dp" />
</RelativeLayout>
<TextView
android:background="#f1ebeb"
android:layout_width="match_parent"
android:layout_height="2dp" />