仿京东的购物车和订单
//从购物车跳转到了确认订单界面
/**
* 确认下单界面*/
public class Main2Activity extends AppCompatActivity {
private TextView text_order;
private TextView text_kuan;
private ImageView tu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Intent intent = getIntent();
final CountPriceBean countPriceBean = (CountPriceBean) intent.getSerializableExtra("order");
text_kuan = (TextView) findViewById(R.id.text_kuan);
text_order = (TextView) findViewById(R.id.text_order);
tu = (ImageView) findViewById(R.id.maidongxidetupian);
text_kuan.setText("实付款: ¥"+countPriceBean.getPrice());
text_order.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
OkHttp3Util.doGet("https://www.zhaoapi.cn/product/createOrder?uid=2845&price=" + countPriceBean.getPrice(), new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String string = response.body().string();
CommonUtils.runOnUIThread(new Runnable() {
@Override
public void run() {
Toast.makeText(Main2Activity.this, string,Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Main2Activity.this, Main3Activity.class);
// intent.putExtra("toux",chachebean.getData().get(0).getList().g)
// intent.putExtra("order",countPriceBean);
startActivity(intent);
}
});
}
});
}
});
}
public void huiqu2(View view) {
AlertDialog.Builder ab=new AlertDialog.Builder(Main2Activity.this);
ab.setTitle("你在三思三思~~~~");
ab.setPositiveButton("去意已决", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
ab.setNegativeButton("再看看",null);
ab.show();
}
public void dingdanzhongxin(View view) {
Intent intent = new Intent(Main2Activity.this, Main3Activity.class);
startActivity(intent);
}
}
//布局示例
<?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" tools:context=".Main2Activity"> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal" android:layout_alignParentTop="true" android:id="@+id/ding" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="<" android:textSize="26dp" android:onClick="huiqu2" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="确认订单" android:textSize="24dp" android:layout_marginLeft="220dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="订单中心" android:layout_marginLeft="180dp" android:onClick="dingdanzhongxin" /> </LinearLayout> <TextView android:layout_below="@+id/ding" android:layout_width="match_parent" android:layout_height="1dp" android:background="#000" android:id="@+id/xian" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:id="@+id/tupian" > <ImageView android:layout_width="100dp" android:layout_height="100dp" android:id="@+id/maidongxidetupian" /> </LinearLayout> <LinearLayout android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <TextView android:id="@+id/text_kuan" android:layout_width="0dp" android:layout_weight="2" android:text="实付款:" android:layout_marginLeft="120dp" android:layout_marginTop="13dp" android:textColor="#ff0000" android:layout_height="match_parent" /> <TextView android:id="@+id/text_order" android:layout_width="0dp" android:layout_weight="2" android:text="立即下单" android:gravity="center" android:textColor="#fff" android:background="#ff0000" android:layout_height="match_parent" /> </LinearLayout> </RelativeLayout>
//到我的订单界面
* 我的订单页面
*/
public class Main3Activity extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager viewPager;
private List<String> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
tabLayout = (TabLayout) findViewById(R.id.tab2);
viewPager = (ViewPager) findViewById(R.id.view_pager2);
tabLayout.setTabMode(TabLayout.MODE_FIXED);
tabLayout.setTabGravity(GRAVITY_FILL);
list = new ArrayList<>();
list.add("全部");
list.add("待付款");
list.add("待收货");
list.add("已完成");
list.add("已取消");
viewPager.setOffscreenPageLimit(list.size());
//1.给viewPager设置适配器
/**
* 管理者对象有几种????
* getSupportFragmentManager()...activity管理他身上的fragment的时候使用
* getChildFragmentManager()...fragment嵌套的时候,,,管理孩子需要使用这个管理者
* getFragmentManager()....孩子里面还有fragment的话就使用这个
*/
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
//2.重写这个方法getPageTitle,,,得到当前页面的标题
@Override
public CharSequence getPageTitle(int position) {
return list.get(position);
}
@Override
public Fragment getItem(int position) {
NewsFragment1 newsFragment = new NewsFragment1();
//应该要做的是传值,,,,去fragment里面获取,,,获取到值之后,,,在进行url路径的拼接
Bundle bundle = new Bundle();
bundle.putString("name",list.get(position));
newsFragment.setArguments(bundle);
return newsFragment;
}
@Override
public int getCount() {
return list.size();
}
});
//3.将tabLayout和viewPager关联起来
tabLayout.setupWithViewPager(viewPager);
}
}
//布局示例
<?xml version="1.0" encoding="utf-8"?> <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=".Main3Activity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="24dp" android:textColor="#000" android:text="我的订单" android:layout_gravity="center" ></TextView> <TextView android:layout_width="match_parent" android:layout_height="1dp" android:background="#000" /> <android.support.design.widget.TabLayout android:id="@+id/tab2" app:tabGravity="center" app:tabIndicatorColor="@color/colorhong" app:tabSelectedTextColor="@color/colorAccent" app:tabTextColor="@color/colorhei" android:layout_width="match_parent" android:layout_height="40dp" /> <android.support.v4.view.ViewPager android:id="@+id/view_pager2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/tab"> </android.support.v4.view.ViewPager> </LinearLayout>
//我的订单中的数据
public class NewsFragment1 extends Fragment{
private List<Dingbean.DataBean> list=new ArrayList<>();
List<Dingbean.DataBean> list1=new ArrayList<>();
private Base base;
private ListView lv;
int page=3;
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what==1) {
Log.d("cccc", "进来了");
if ("全部".equals(name)) {
//Log.d("---",name);
list.clear();
list.addAll(dingbean.getData());
if (list.size() > 0) {
setaba();
} else {
Toast.makeText(getActivity(), "没有此类型的商品订单哦", Toast.LENGTH_SHORT).show();
}
} else if ("待付款".equals(name)) {
Log.d("lllll", name);
if (list1.size() > 0) {
list.clear();
for (int i = 0; i < list1.size(); i++) {
if (list1.get(i).getStatus() == 0) {
list.add(list1.get(i));
}
}
if (list.size() > 0) {
setaba();
} else {
Toast.makeText(getActivity(), "没有此类型的商品订单哦", Toast.LENGTH_SHORT).show();
}
} else {
getshuju();
}
} else if ("待收货".equals(name)) {
Log.d("lllll", name);
if (list1.size() > 0) {
list.clear();
for (int i = 0; i < list1.size(); i++) {
if (list1.get(i).getStatus() == 0) {
list.add(list1.get(i));
}
}
if (list.size() > 0) {
setaba();
} else {
Toast.makeText(getActivity(), "没有此类型的商品订单哦", Toast.LENGTH_SHORT).show();
}
} else {
getshuju();
}
} else if ("已完成".equals(name)) {
Log.d("----", name);
if (list1.size() > 0) {
list.clear();
for (int i = 0; i < list1.size(); i++) {
if (list1.get(i).getStatus() == 2) {
list.add(list1.get(i));
}
}
if (list.size() > 0) {
setaba();
} else {
Toast.makeText(getActivity(), "没有此类型的商品订单哦", Toast.LENGTH_SHORT).show();
}
} else {
getshuju();
}
} else if ("已取消".equals(name)) {
Log.d("----", name);
if (list1.size() > 0) {
list.clear();
for (int i = 0; i < list1.size(); i++) {
if (list1.get(i).getStatus() == 2) {
list.add(list1.get(i));
}
}
if (list.size() > 0) {
setaba();
} else {
Toast.makeText(getActivity(), "没有此类型的商品订单哦", Toast.LENGTH_SHORT).show();
}
} else {
getshuju();
}
}
}else if (msg.what==2){
getshuju();
/*List<Dingbean.DataBean> date= (List<Dingbean.DataBean>) msg.obj;
Log.d("yyyyy", date.toString());
list.addAll(date);*/
setaba();
}
}
};
private String name;
private Dingbean dingbean;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.ding, container, false);
lv = view.findViewById(R.id.lvvv);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
name = getArguments().getString("name", "");
Log.d("nnnn", name);
if (name.equals("全部")){
getshuju();
}else if (name.equals("待付款")){
Log.d("vvvv","进来了");
handler.sendEmptyMessage(1);
}else if (name.equals("待收货")){
handler.sendEmptyMessage(1);
}else if (name.equals("已完成")){
handler.sendEmptyMessage(1);
}else if (name.equals("已取消")){
handler.sendEmptyMessage(1);
}
}
private void getshuju() {
page++;
OkHttp3Util.doGet("https://www.zhaoapi.cn/product/getOrders?uid=71&page=6", new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String string = response.body().string();
if (response.isSuccessful()){
CommonUtils.runOnUIThread(new Runnable() {
@Override
public void run() {
Gson gson=new Gson();
dingbean = gson.fromJson(string, Dingbean.class);
list1.clear();
list1.addAll(dingbean.getData());
handler.sendEmptyMessage(1);
}
});
}
}
});
}
private void setaba() {
if (base==null){
base = new Base(getActivity(),list,handler);
lv.setAdapter(base);
}else
{
base.notifyDataSetChanged();
}
}
///////给一个listview就可以