Android custom View bottom selection pop-up window

1. Select the pop-up window at the bottom

Effect:
Insert image description here

1. Custom class

Bottom pop-up window with customizable number of items

(1) CustomBottomPop custom class
package com.custom.jfrb.ui.first.customView; //自己的包位置

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.custom.jfrb.R;
import com.custom.jfrb.base.BasePopupWindow;
import com.custom.jfrb.ui.first.base.CustomPopListener;
import com.custom.jfrb.ui.first.bean.PopBean;

import org.droidparts.annotation.inject.InjectView;

import java.util.List;

/**
 * 一个自定义item数量的底部弹出窗
 */
public class CustomBottomPop extends BasePopupWindow {

    @InjectView(id = R.id.tv_cancel, click = true)
    TextView tvCancel;

    @InjectView(id = R.id.ll_root)
    LinearLayout llRoot;

    List<PopBean> data ;
    CustomPopListener mMustomPopListener;

    public CustomBottomPop(Context context, List<PopBean> list, CustomPopListener customPopListener) {
        super(context);
        this.data = list;
        this.mMustomPopListener = customPopListener;
        refresh();
    }

    public void refresh() {
        clearItem();
        for (PopBean bean : data) {
            addItem(bean);
        }
    }

    public void refresh(List<PopBean> list) {
        this.data = list;
        refresh();
    }

    Object object;

    @Override
    public View setPopview() {
        return LayoutInflater.from(getContext()).inflate(R.layout.popup_bottom_custom, null);
    }

    public void clearItem() {
        llRoot.removeAllViews();
    }

    private void addItem(final PopBean bean) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.item_pop, null);
        TextView textView = (TextView) view.findViewById(R.id.tv_button);
        textView.setText(bean.getValue());
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMustomPopListener.onClickButton(CustomBottomPop.this,bean);
            }
        });
        llRoot.addView(view);
    }

    @Override
    public void onClick(View v) {
      if (v == tvCancel) {
            dismiss();
        }
    }

    public Object getObject() {
        return this.object;
    }

    public void setObject(Object object) {
        this.object = object;
    }
}

(2) Inherited BasePopupView class
package com.custom.jfrb.base;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.custom.jfrb.R;

import org.droidparts.Injector;
import org.droidparts.bus.EventBus;

/**
 * Created by Administrator on 2015/7/9 0009.
 */
public abstract class BasePopupWindow extends PopupWindow implements View.OnClickListener
{

    public Context context;


    public BasePopupWindow(Context context)
    {

        this.context = context;
        onInit();
    }

    public Context getContext()
    {
        return this.context;
    }

    private void onInit()
    {

        setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
        View inflate = setPopview();
        setContentView(inflate);
        setBackgroundDrawable(new BitmapDrawable());
        Injector.inject(getContentView(), this);
        EventBus.unregisterAnnotatedReceiver(this);
        EventBus.registerAnnotatedReceiver(this);


        setAnimationStyle(R.style.PopupAnimation2);
    }

    protected BasePopupWindow()
    {

    }

    public abstract View setPopview();
}
(3)R.layout.popup_bottom_custom

Cancel bar style

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/rl_popup"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/half_transparent"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_alignParentBottom="true"
        android:background="@drawable/rnd_corner"
        >
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

        <LinearLayout
            android:id="@+id/ll_root"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical">

        </LinearLayout>

    </ScrollView>

        <View
            android:layout_width="match_parent"
            android:layout_height="@dimen/dk_dp_5"
            android:background="#f2f2f2"
            />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:gravity="center">

        <TextView
            android:id="@+id/tv_cancel"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="取消"
            android:textColor="@color/black"
            android:textSize="18dp"/>

    </LinearLayout>

    </LinearLayout>

</RelativeLayout>

(4)R.layout.item_pop

Single selection style, the example in this article only displays one TextView for each option, and the whole can be slid

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

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                android:id="@+id/tv_button"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:layout_gravity="center"
                android:gravity="center"
                android:textColor="@color/black"
                android:textSize="18dp"
                tools:text="按钮1"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#eeeeee"/>


        </LinearLayout>


    </ScrollView>


</LinearLayout>

2. Call

(1) Activity class
public class PublishToShangguanActivity extends BaseActivity implements CustomPopListener {

    //是否原创选择模块
    @InjectView(id = R.id.ly_is_original,click = true)
    private LinearLayout mLyOriginal;

    //选择是否原创弹窗
    CustomBottomPop originalSelectPop;
    protected List<PopBean> originalLists = new ArrayList<>();
    String[] originals = new String[]{"是","否"};

    @Override
    public void onPreInject() {
        Utils.fitSystemWhite(this);
        setContentView(R.layout.activity_publish_to_shangguan);
    }

    @Override
    protected void onPostCreate() {
    }


    //重点关注监听这里,其余根据业务可变
    @Override
    public void onClick(View v) {
        if (v == mLyOriginal){
            //调出选择是否原创弹窗
            originalLists.clear();
            for (int i = 0;i < originals.length;i++){
                originalLists.add(new PopBean(i,originals[i]));
            }
            originalSelectPop = new CustomBottomPop(this, originalLists, this);
            originalSelectPop.setClippingEnabled(true);
            originalSelectPop.showAtLocation(getRootView(), Gravity.CENTER, 0, 0);
        }
    }

    //弹窗选择监听,可取出选择项的值
    @Override
    public void onClickButton(CustomBottomPop customBottomPop, PopBean bean) {
       if (customBottomPop == originalSelectPop){
            mTvOriginal.setText(bean.getValue());
        }
        customBottomPop.dismiss();
    }
}
(2) Custom listening class CustomPopListener.java
package com.custom.jfrb.ui.first.base;

import com.custom.jfrb.ui.first.bean.PopBean;
import com.custom.jfrb.ui.first.customView.CustomBottomPop;

/**
 * Created by Administrator on 2018/3/1 0001.
 */

public interface CustomPopListener{
    void onClickButton(CustomBottomPop customBottomPop, PopBean bean);
}

3. Done, Nice!

Guess you like

Origin blog.csdn.net/qq_46269365/article/details/133925845