实现支付宝支付从底部弹窗效果

前言

我们再用支付宝支付的时候,会从底部弹上来一个对话框,让我们选择支付方式等等,今天我们就来慢慢实现这个功能

效果图

实现支付宝支付从底部弹窗效果_第1张图片

实现

主界面很简单,就是一个按钮,点击后跳到支付详情的Fragment中

package com.example.hfs.alipayuidemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {
        mButton= (Button) this.findViewById(R.id.btn_pay);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                PayDetailFragment payDetailFragment=new PayDetailFragment();
                payDetailFragment.show(getSupportFragmentManager(),"payDetailFragment");
            }
        });
    }
}

接着是支付详情的Fragment代码

"1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
    android:id="@+id/re_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff">
    
    "@+id/re_pay_detail"
        android:layout_width="match_parent"
        android:layout_height="fill_parent">

        "match_parent"
            android:layout_height="wrap_content">

            "match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                "match_parent"
                    android:layout_height="wrap_content"
                    android:paddingBottom="15dp"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:paddingTop="15dp">

                    "10dp"
                        android:layout_height="10dp"
                        android:src="@mipmap/ic_close" />

                    "wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerHorizontal="true"
                        android:text="订单详情"
                        android:textSize="15sp" />

                    "10dp"
                        android:layout_height="10dp"
                        android:layout_alignParentRight="true"
                        android:src="@mipmap/ic_doubt" />
                

                "match_parent"
                    android:layout_height="1dp"
                    android:background="@color/line"/>

                "match_parent"
                    android:layout_height="wrap_content"
                    android:paddingBottom="15dp"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:paddingTop="15dp">

                    "wrap_content"
                        android:layout_height="wrap_content"
                        android:text="信用卡还款"
                        android:textColor="@color/text_color_grey" />

                    "wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:text="农业银行"
                        android:textColor="@color/text_color_grey" />

                

                "match_parent"
                    android:layout_height="1dp"
                    android:background="@color/line"/>

                "@+id/re_pay_way"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingBottom="15dp"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:paddingTop="15dp">

                    "wrap_content"
                        android:layout_height="wrap_content"
                        android:text="支付方式"
                        android:textColor="@color/text_color_grey" />

                    "wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:drawablePadding="10dp"
                        android:drawableRight="@mipmap/ic_open"
                        android:text="招商银行(尾号2345)"
                        android:textColor="@color/text_color_grey" />

                

                "match_parent"
                    android:layout_height="1dp"
                    android:background="@color/line"/>

                "match_parent"
                    android:layout_height="wrap_content"
                    android:paddingBottom="15dp"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:paddingTop="15dp">

                    "wrap_content"
                        android:layout_height="wrap_content"
                        android:text="订单金额"
                        android:textColor="@color/text_color_grey" />

                    "wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:text="10.00"
                        android:textColor="@color/text_color_grey" />
                

                "match_parent"
                    android:layout_height="1dp"
                    android:background="@color/line"/>

                "match_parent"
                    android:layout_height="wrap_content"
                    android:paddingBottom="15dp"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:paddingTop="15dp">

                    "wrap_content"
                        android:layout_height="wrap_content"
                        android:text="需付款" />

                    "wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:text="10.00" />
                

            
        

        
package com.example.hfs.alipayuidemo;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;


/**
 * 底部弹窗Fragment
 */
public class PayDetailFragment extends DialogFragment {
    private RelativeLayout rePayWay, rePayDetail, reBalance;
    private LinearLayout LinPayWay,linPass;
    private ListView lv;
    private Button btnPay;
    private EditText gridPasswordView;
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // 使用不带Theme的构造器, 获得的dialog边框距离屏幕仍有几毫米的缝隙。
        Dialog dialog = new Dialog(getActivity(), R.style.BottomDialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置Content前设定
        dialog.setContentView(R.layout.fragment_pay_detail);
        dialog.setCanceledOnTouchOutside(true); // 外部点击取消
        // 设置宽度为屏宽, 靠近屏幕底部。
        final Window window = dialog.getWindow();
        window.setWindowAnimations(R.style.AnimBottom);
        final WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.BOTTOM; // 紧贴底部
        lp.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度持平
        lp.height = getActivity().getWindowManager().getDefaultDisplay().getHeight() * 3 / 5;
        window.setAttributes(lp);
        rePayWay = (RelativeLayout) dialog.findViewById(R.id.re_pay_way);
        rePayDetail = (RelativeLayout) dialog.findViewById(R.id.re_pay_detail);//付款详情
        LinPayWay = (LinearLayout) dialog.findViewById(R.id.lin_pay_way);//付款方式
        lv = (ListView) dialog.findViewById(R.id.lv_bank);//付款方式(银行卡列表)
        reBalance = (RelativeLayout) dialog.findViewById(R.id.re_balance);//付款方式(余额)
        btnPay = (Button) dialog.findViewById(R.id.btn_confirm_pay);
        gridPasswordView = (EditText) dialog.findViewById(R.id.pass_view);
        linPass = (LinearLayout)dialog.findViewById(R.id.lin_pass);
        rePayWay.setOnClickListener(listener);
        reBalance.setOnClickListener(listener);
        btnPay.setOnClickListener(listener);
        return dialog;
    }

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Animation slide_left_to_left = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_left_to_left);
            Animation slide_right_to_left = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_right_to_left);
            Animation slide_left_to_right = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_left_to_right);
            Animation slide_left_to_left_in = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_left_to_left_in);
            switch (view.getId()) {
                case R.id.re_pay_way:
                    rePayDetail.startAnimation(slide_left_to_left);
                    rePayDetail.setVisibility(View.GONE);
                    LinPayWay.startAnimation(slide_right_to_left);
                    LinPayWay.setVisibility(View.VISIBLE);
                    break;
                case R.id.re_balance:
                    rePayDetail.startAnimation(slide_left_to_left_in);
                    rePayDetail.setVisibility(View.VISIBLE);
                    LinPayWay.startAnimation(slide_left_to_right);
                    LinPayWay.setVisibility(View.GONE);
                    break;
                case R.id.btn_confirm_pay:
                    rePayDetail.startAnimation(slide_left_to_left);
                    rePayDetail.setVisibility(View.GONE);
                    linPass.startAnimation(slide_right_to_left);
                    linPass.setVisibility(View.VISIBLE);
                    break;
                default:
                    break;
            }
        }
    };
}

还有一个ScrollView嵌套ListView的问题,主要是重写ListView的计算高度

package com.example.hfs.alipayuidemo;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;

/**
 * ScrollView中嵌套ListView,重写ListView计算高度
 */
public class ScrollviewListView extends ListView {
    public ScrollviewListView(Context context) {
        super(context);
    }

    public ScrollviewListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ScrollviewListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,

                MeasureSpec.AT_MOST);

        super.onMeasure(widthMeasureSpec, expandSpec);

    }
}

好了,其实还是挺好理解的,附上GitHub项目地址:
https://github.com/Greathfs/AliPayUiDemo

你可能感兴趣的:(AndroidUI设计)