1. 程式人生 > >仿京東我的頁面

仿京東我的頁面

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:background="@drawable/b5a"
                android:gravity="center"
                android:orientation="horizontal">

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="3"
                    android:gravity="center">

                    <com.facebook.drawee.view.SimpleDraweeView
                        android:id="@+id/five_sdv"
                        android:layout_width="80dp"
                        android:layout_height="80dp"
                        android:layout_marginLeft="20dp"
                        android:background="@drawable/user" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="10dp"
                    android:layout_weight="7"
                    android:gravity="center_vertical"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/me_login"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="登入"
                        android:textColor="#fff"
                        android:textSize="16dp" />

                    <TextView
                        android:id="@+id/text_iv"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text=""
                        android:textColor="#cccccc"
                        android:textSize="16dp" />

                    <TextView
                        android:id="@+id/me_zuce"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="註冊"
                        android:textColor="#fff"
                        android:textSize="16dp" />

                </LinearLayout>

            </LinearLayout>


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:orientation="horizontal">

                <LinearLayout
                    android:id="@+id/mine_yzf"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center_horizontal"
                    android:orientation="vertical">

                    <ImageView
                        android:layout_width="35dp"
                        android:layout_height="25dp"
                        android:layout_marginBottom="4dp"
                        android:layout_marginTop="6dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/dfk" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:gravity="center"
                        android:text="已支付" />

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/mine_dzf"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center_horizontal"
                    android:orientation="vertical">

                    <ImageView
                        android:layout_width="35dp"
                        android:layout_height="35dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/dsh" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:gravity="center"
                        android:text="待支付" />

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/mine_yqx"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center_horizontal"
                    android:orientation="vertical">

                    <ImageView
                        android:layout_width="35dp"
                        android:layout_height="35dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/dpj" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:gravity="center"
                        android:text="已取消" />

                </LinearLayout>


                <LinearLayout
                    android:id="@+id/mine_sh_th"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center_horizontal"
                    android:orientation="vertical">

                    <ImageView
                        android:layout_width="35dp"
                        android:layout_height="35dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/th_sh" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:gravity="center"
                        android:text="退換/售後" />

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/mine_myOrder"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center_horizontal"
                    android:orientation="vertical">

                    <ImageView
                        android:layout_width="40dp"
                        android:layout_height="40dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/wd2" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:gravity="center"
                        android:text="我的訂單" />

                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="1dp"
                android:orientation="horizontal">

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center_horizontal"
                    android:orientation="vertical">

                    <ImageView
                        android:layout_width="40dp"
                        android:layout_height="40dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/jdou" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:gravity="center"
                        android:text="京豆" />


                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center_horizontal"
                    android:orientation="vertical">

                    <ImageView
                        android:layout_width="40dp"
                        android:layout_height="40dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/yhq" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:gravity="center"
                        android:text="優惠券" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center_horizontal"
                    android:orientation="vertical">

                    <ImageView
                        android:layout_width="40dp"
                        android:layout_height="40dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/bt" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:gravity="center"
                        android:text="白條" />

                </LinearLayout>


                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center_horizontal"
                    android:orientation="vertical">

                    <ImageView
                        android:layout_width="40dp"
                        android:layout_height="40dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/foodcard" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:gravity="center"
                        android:text="禮品卡" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center_horizontal"
                    android:orientation="vertical">

                    <ImageView
                        android:layout_width="40dp"
                        android:layout_height="40dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/wdqb" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:gravity="center"
                        android:text="我的錢包" />

                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:orientation="horizontal">

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center_horizontal"
                    android:orientation="vertical">

                    <ImageView
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/spgz" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:gravity="center"
                        android:text="商品關注" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center_horizontal"
                    android:orientation="vertical">

                    <ImageView
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/dp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:gravity="center"
                        android:text="店鋪關注" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center_horizontal"
                    android:orientation="vertical">

                    <ImageView
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/gz" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:gravity="center"
                        android:text="內容收藏" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center_horizontal"
                    android:orientation="vertical">

                    <ImageView
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/lljl" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:gravity="center"
                        android:text="瀏覽記錄" />

                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="6dp"
                android:orientation="horizontal">

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center_horizontal"
                    android:orientation="vertical">

                    <ImageView
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/hd" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:gravity="center"
                        android:text="我的活動" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center_horizontal"
                    android:orientation="vertical">

                    <ImageView
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/shqz" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:gravity="center"
                        android:text="社群" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center_horizontal"
                    android:orientation="vertical">

                    <ImageView
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/kh" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:gravity="center"
                        android:text="客戶服務" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center_horizontal"
                    android:orientation="vertical">

                    <ImageView
                        android:layout_width="40dp"
                        android:layout_height="40dp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:gravity="center" />
                </LinearLayout>
            </LinearLayout>

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="110dp"
                android:layout_marginTop="5dp"
                android:scaleType="centerCrop"
                android:src="@drawable/a_3q" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/me_Rev"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/mine_title_liner"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <View
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="1" />

            <ImageView
                android:id="@+id/mine_message"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="15dp"
                android:scaleType="fitXY"
                android:src="@drawable/xiaoxi" />

            <ImageView
                android:id="@+id/mine_setting"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:layout_marginRight="15dp"
                android:layout_marginTop="15dp"
                android:scaleType="fitXY"
                android:src="@drawable/shezhi" />
        </LinearLayout>
    </RelativeLayout>
</android.support.v4.widget.NestedScrollView>
public interface ListsView {
    void success(ListsBean listsBean);
    void failure(String msg);
}
package com.example.jingd.fragment;

import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.jingd.LoginActivity;
import com.example.jingd.R;
import com.example.jingd.SettingActivity;
import com.example.jingd.ZhuceActivity;
import com.example.jingd.adapter.MeListAdapter;
import com.example.jingd.adapter.MyLiushiListAdapter;
import com.example.jingd.bean.ListsBean;
import com.example.jingd.persenter.ListPersenter;
import com.example.jingd.view.ListsView;
import com.facebook.drawee.view.SimpleDraweeView;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static android.app.Activity.RESULT_OK;

public class MeFragment extends Fragment implements ListsView {

    @BindView(R.id.five_sdv)
    SimpleDraweeView sim_touxiang;
    Unbinder unbinder;
    @BindView(R.id.me_login)
    TextView meLogin;
    @BindView(R.id.text_iv)
    TextView textIv;
    @BindView(R.id.me_zuce)
    TextView meZuce;
    @BindView(R.id.mine_yzf)
    LinearLayout mineYzf;
    @BindView(R.id.mine_dzf)
    LinearLayout mineDzf;
    @BindView(R.id.mine_yqx)
    LinearLayout mineYqx;
    @BindView(R.id.mine_sh_th)
    LinearLayout mineShTh;
    @BindView(R.id.mine_myOrder)
    LinearLayout mineMyOrder;
    @BindView(R.id.mine_setting)
    ImageView mineSetting;
    @BindView(R.id.mine_message)
    ImageView mineMessage;
    @BindView(R.id.mine_title_liner)
    LinearLayout mineTitleLiner;
    @BindView(R.id.me_Rev)
    RecyclerView meRev;
    private Bitmap head;// 頭像Bitmap
    private static String path = "/sdcard/myHead/";// sd路徑
    private ListPersenter listPersenter;
    private String name;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.mefragment_layout, container, false);
        unbinder = ButterKnife.bind(this, inflate);
        listPersenter = new ListPersenter(this);
        listPersenter.getListsData("電腦");
        initData();
        initView();
        return inflate;
    }

    private void initView() {
        meRev.setLayoutManager(new GridLayoutManager(getActivity(), 2));
    }

    private void initData() {
//        Intent intent = getActivity().getIntent();
//        name = intent.getStringExtra("mobile");
//        Log.e("註冊", name + "");
//        meZuce.setText(name);


        mineSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), SettingActivity.class));
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.five_sdv)
    public void onViewClicked() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        final AlertDialog alertDialog = builder.create();
        View inflate = View.inflate(getActivity(), R.layout.add_image, null);
        TextView tv_select_gallery = inflate.findViewById(R.id.tv_select_gallery);
        TextView tv_select_camera = inflate.findViewById(R.id.tv_select_camera);

        // 在相簿中選取
        tv_select_gallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK, null);
                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                startActivityForResult(intent, 1);
                alertDialog.dismiss();
            }
        });
        // 呼叫照相機
        tv_select_camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "head.jpg")));
                startActivityForResult(intent, 2);
                alertDialog.dismiss();
            }
        });
        alertDialog.setView(inflate);
        alertDialog.show();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    cropPhoto(data.getData());// 裁剪圖片
                }

                break;
            case 2:
                if (resultCode == RESULT_OK) {
                    File temp = new File(Environment.getExternalStorageDirectory() + "/head.jpg");
                    cropPhoto(Uri.fromFile(temp));// 裁剪圖片
                }

                break;
            case 3:
                if (data != null) {
                    Bundle extras = data.getExtras();
                    head = extras.getParcelable("data");
                    if (head != null) {
                        /**
                         * 上傳伺服器程式碼
                         */
                        setPicToView(head);// 儲存在SD卡中
                        sim_touxiang.setImageBitmap(head);// 用ImageView顯示出來
                    }
                }
                break;
            case 4:
                String mobile = data.getStringExtra("mobile");
                Log.e("mobile", mobile + "");
                meZuce.setText(mobile);
            default:
                break;

        }
        super.onActivityResult(requestCode, resultCode, data);
//
    }

    private void cropPhoto(Uri data) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(data, "image/*");
        intent.putExtra("crop", "true");
        // aspectX aspectY 是寬高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX outputY 是裁剪圖片寬高
        intent.putExtra("outputX", 150);
        intent.putExtra("outputY", 150);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, 3);
    }

    private void setPicToView(Bitmap mBitmap) {
        String sdStatus = Environment.getExternalStorageState();
        if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 檢測sd是否可用
            return;
        }
        FileOutputStream b = null;
        File file = new File(path);
        file.mkdirs();// 建立資料夾
        String fileName = path + "head.jpg";// 圖片名字
        try {
            b = new FileOutputStream(fileName);
            mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把資料寫入檔案
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                // 關閉流
                b.flush();
                b.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @OnClick({R.id.me_login, R.id.me_zuce})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.me_login:
                startActivityForResult(new Intent(getActivity(), LoginActivity.class), 1);
                break;
            case R.id.me_zuce:
                startActivity(new Intent(getActivity(), ZhuceActivity.class));
                break;
        }
    }

    @Override
    public void success(ListsBean listsBean) {
        MeListAdapter adapter = new MeListAdapter(getActivity(), listsBean.getData());
        meRev.setAdapter(adapter);
    }


    @Override
    public void failure(String msg) {

    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/me_sim"
        android:layout_width="80dp"
        android:layout_height="80dp" />

    <TextView
        android:id="@+id/me_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:textColor="#f00"
        android:id="@+id/me_product"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>



public class MeListAdapter extends RecyclerView.Adapter<MeListAdapter.ViewHolder> {

    Context context;
    List<ListsBean.DataBean> list;

    public MeListAdapter(Context context, List<ListsBean.DataBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.me_list_item, parent, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Uri uri = Uri.parse(list.get(position).getImages().split("\\|")[0]);
        holder.simpleDraweeView.setImageURI(uri);
        holder.title.setText(list.get(position).getTitle());
        holder.product.setText("¥:"+list.get(position).getPrice()+"");
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        SimpleDraweeView simpleDraweeView;
        TextView title,product;

        public ViewHolder(View itemView) {
            super(itemView);
            simpleDraweeView = itemView.findViewById(R.id.me_sim);
            title = itemView.findViewById(R.id.me_title);
            product = itemView.findViewById(R.id.me_product);
        }
    }
}