1. 程式人生 > >Android 仿京東訂單頁面

Android 仿京東訂單頁面

不要作弊哦,不然直降49

訂單佈局檔案(activity_myorder)

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:background="#7878"

    <RelativeLayout
        android:id="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="50dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="訂單列表"
            android:textColor="#000"
            android:textSize="22sp" />

    </RelativeLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_below="@id/top"
        android:background="#787878" />

    <android.support.design.widget.TabLayout
        android:id="@+id/myTab"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@id/top"
        android:layout_marginTop="10dp"
        app:tabGravity="fill"
        app:tabIndicatorColor="@color/colorAccent"
        app:tabMode="fixed"
        app:tabSelectedTextColor="@color/colorPrimaryDark"
        app:tabTextColor="@color/colorPrimary"></android.support.design.widget.TabLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_below="@id/myTab"
        android:background="#787878" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/myTab"
        android:layout_marginTop="10dp"></android.support.v4.view.ViewPager>

</RelativeLayout>
訂單java程式碼

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

public class MyOrderActivity extends AppCompatActivity {

    private TabLayout myTab;
    private ViewPager viewPage;
    private String[] title = {"全部", "待支付", "已支付", "已取消"};
    private String[] urlTitle = {"9", "0", "1", "2"};
    private MyAdapter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myorder);
        initView();//初始化檢視
        myAdapter = new MyAdapter(getSupportFragmentManager());
        viewPage.setAdapter(myAdapter);
        myTab.setupWithViewPager(viewPage);
    }

    private void initView() {
        myTab = (TabLayout) findViewById(R.id.myTab);
        viewPage = (ViewPager) findViewById(R.id.viewPage);
    }

    //內部介面卡
    class MyAdapter extends FragmentPagerAdapter {
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return title[position];
        }

        @Override
        public Fragment getItem(int position) {

            //建立fragment物件並返回
            Bundle bundle = new Bundle();
            bundle.putString("url", urlTitle[position]);
            //例項化Fragment
            PayFragment payFragment = new PayFragment();
            payFragment.setArguments(bundle);
            return payFragment;
        }

        @Override
        public int getCount() {
            return title.length;
        }
    }

}
訂單頁面建立的動態Fragment佈局程式碼(activity_pay)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <android.support.v7.widget.RecyclerView
        android:background="#EBEBEB"
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>

</RelativeLayout>

訂單頁面建立的動態Fragmentjava程式碼(PayFragment)

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * author:Created by ZhangPengFei on 2017/12/20.
 */

public class PayFragment extends Fragment implements IPayPresenter {

    private View view;
    private String uid = "3384";
    private PayPresenter payPresenter;
    private RecyclerView recyclerView;
    private MyRecyclerViewAdapter myAdapter;
    private String urlTitle;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = View.inflate(getActivity(), R.layout.activity_pay, null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();//初始化資料
        Bundle bundle = getArguments();
        urlTitle = bundle.getString("url").toString();
//        Log.i("qqqq",urlTitle);
        payPresenter = new PayPresenter(this);//例項化
        payPresenter.receive(uid, urlTitle);
    }

    //初始化資料
    private void initView() {
        recyclerView = view.findViewById(R.id.recyclerView);
    }


    @Override
    public void onSuccess(List<JsonPayBean.DataBean> data) {
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        myAdapter = new MyRecyclerViewAdapter(getActivity(), data);
        recyclerView.setAdapter(myAdapter);
    }

    @Override
    public void onFailed() {

    }
}
訂單的P層介面(IPayPresenter)

import java.util.List;

/**
 * author:Created by ZhangPengFei on 2017/12/20.
 */

public interface IPayPresenter {

    void onSuccess(List<JsonPayBean.DataBean> data);

    void onFailed();

}

訂單的M層介面(IPayModel)


import java.util.List;

/**
 * author:Created by ZhangPengFei on 2017/12/20.
 */

public interface IPayModel {

    void onSuccess(List<JsonPayBean.DataBean> data);

    void onFailed();

}
訂單的M層(PayModel)

import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;

/**
 * author:Created by ZhangPengFei on 2017/12/20.
 */

public class PayModel {

    public void receive(String uid, String urlTitle, final IPayModel iPayModel) {
        OKHttpUtils instance = OKHttpUtils.getInstance();
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("uid", uid);
        if (urlTitle.trim().equals("9")) {

        } else {
            hashMap.put("status", urlTitle);
        }
        instance.doGet("http://120.27.23.105/product/getOrders", hashMap, new IOkHttpUtils() {
            @Override
            public void onSuccess(String str) {
                if (str != null) {
                    Gson gson = new Gson();
                    JsonPayBean jsonPayBean = gson.fromJson(str, JsonPayBean.class);
                    if (jsonPayBean != null) {
                        List<JsonPayBean.DataBean> data = jsonPayBean.getData();
                        if (data != null) {
                            iPayModel.onSuccess(data);
                        }
                    }
                }
            }

            @Override
            public void onFailed(String message) {
                iPayModel.onFailed();
            }
        });
    }

}

訂單的P層(PayPresenter)


import java.util.List;

/**
 * author:Created by ZhangPengFei on 2017/12/20.
 */

public class PayPresenter implements IPayModel {

    private PayModel payModel;
    private IPayPresenter iPayPresenter;

    public PayPresenter(IPayPresenter iPayPresenter) {
        this.iPayPresenter = iPayPresenter;
        payModel = new PayModel();
    }

    public void receive(String uid,String urlTitle) {
        payModel.receive(uid,urlTitle, this);
    }

    @Override
    public void onSuccess(List<JsonPayBean.DataBean> data) {
        iPayPresenter.onSuccess(data);
    }

    @Override
    public void onFailed() {
        iPayPresenter.onFailed();
    }
}