Android 实现仿淘宝地址多级选择器

这篇具有很好参考价值的文章主要介绍了Android 实现仿淘宝地址多级选择器。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

先看下效果图

Android 实现仿淘宝地址多级选择器,android,java,开发语言

 仿淘宝的选择完城市出来的选择省市区之类的,这个支持自定义层级,多少层都可以使用,接下来是代码:

BottomPopUtils.class,我写了一个工具类,方便全局调用
public class BottomPopUtils {

    public static ProjectTitleAdapter titleAdapter;
    public static ProjectAdapter adapter1;
    public static PopupWindow popupWindow;
    public static View contentView;
    public static StringBuilder result;

    public static void showPopWindow(List<ProjectTitleItemBean> listTop, List<ProjectItemBean> listContent, Activity context, Finish finish) {
        result = new StringBuilder();
        Map<Integer, List<ProjectItemBean>> nowMap = new HashMap<>();

        //加载弹出框的布局
        contentView = LayoutInflater.from(context).inflate(
                R.layout.popupwindow, null);
        // 设置按钮的点击事件
        ImageView ivFinish = contentView.findViewById(R.id.iv_finish);

        RecyclerView rl = contentView.findViewById(R.id.rl);
        RecyclerView rlTitle = contentView.findViewById(R.id.rl_title);

        //这个为了取出第一级的数据
        List<ProjectItemBean> item = new ArrayList<>();
        for (int i = 0; i < listContent.size(); i++) {
            if (listContent.get(i).getLevel() == 0) {
                item.add(listContent.get(i));
            }
        }
        List<ProjectItemBean> firstMap = new ArrayList<>();

        for (int i = 0; i < item.size(); i++) {
            ProjectItemBean itemBean1 = item.get(i);
            firstMap.add(BeanCloneUtil.cloneTo(itemBean1));
        }

        nowMap.put(0, firstMap);

        titleAdapter = new ProjectTitleAdapter(context, listTop, (itemBean, position) -> {
            //每次用户点击顶部集合的时候,移除掉点击下标后的所有数据
            int size = listTop.size();
            for (int i = position + 1; i < size; i++) {
                listTop.remove(position + 1);
            }

            titleAdapter.notifyDataSetChanged();


            Iterator<Map.Entry<Integer, List<ProjectItemBean>>> iterator = nowMap.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<Integer, List<ProjectItemBean>> entry = iterator.next();
                Integer key = entry.getKey();
                if (key > position) {
                    iterator.remove();
                }
            }

            List<ProjectItemBean> items = new ArrayList<>();
            List<ProjectItemBean> listNow = nowMap.get(position);
            if (listNow != null) {
                for (int i = 0; i < listNow.size(); i++) {
                    if (listNow.get(i).getName().equals(itemBean.getName())) {
                        listNow.get(i).setChoose(true);
                    } else {
                        listNow.get(i).setChoose(false);
                    }
                    items.add(listNow.get(i));
                }
            }

            item.clear();
            item.addAll(items);
            adapter1.notifyDataSetChanged();

        });


        rlTitle.setLayoutManager(new LinearLayoutManager(context));
        rlTitle.setAdapter(titleAdapter);


        adapter1 = new ProjectAdapter(context, item, itemBean -> {
            List<ProjectItemBean> mapItem = new ArrayList<>();
            if (!nowMap.containsKey(itemBean.getLevel())) {
                for (int i = 0; i < item.size(); i++) {
                    ProjectItemBean itemBean1 = item.get(i);
                    mapItem.add(BeanCloneUtil.cloneTo(itemBean1));
                }
                nowMap.put(itemBean.getLevel(), mapItem);
            }

            for (int i = 0; i < item.size(); i++) {
                item.get(i).setChoose(false);
            }
            itemBean.setChoose(true);



            listTop.get(itemBean.getLevel()).setCode(itemBean.getCode());
            listTop.get(itemBean.getLevel()).setId(itemBean.getId());
            listTop.get(itemBean.getLevel()).setName(itemBean.getName());
            listTop.get(itemBean.getLevel()).setChoose(true);
            titleAdapter.notifyDataSetChanged();


            //如果当前选择的数据是否有子集合
            if (itemBean.getChildList() != null && itemBean.getChildList().size() > 0) {
                item.clear();
                if (itemBean.getChildList() == null) {
                    item.addAll(new ArrayList<>());
                } else {
                    item.addAll(itemBean.getChildList());
                }

                ProjectTitleItemBean bean = new ProjectTitleItemBean();
                bean.setName("请选择");
                bean.setChoose(false);
                listTop.add(bean);

                titleAdapter.notifyDataSetChanged();
                adapter1.notifyDataSetChanged();
            } else {
                adapter1.notifyDataSetChanged();
            }
        });
        rl.setLayoutManager(new LinearLayoutManager(context));
        rl.setAdapter(adapter1);

        ivFinish.setOnClickListener(v -> {
            if (popupWindow != null && popupWindow.isShowing()) {
                for (int i = 0; i < listTop.size(); i++) {
                    String name = listTop.get(i).getName();
                    if (i == listTop.size() - 1) {
                        result.append(name);
                    } else {
                        result.append(name + "--");
                    }
                    Log.i("已选中的值", name);
                }
                finish.finish(result.toString());
                popupWindow.dismiss();
                backgroundAlpha(1.0f, context);
            }
        });

        popupWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setFocusable(false);// 取得焦点
        //注意  要是点击外部空白处弹框消息  那么必须给弹框设置一个背景色  不然是不起作用的
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        //点击外部消失
        popupWindow.setOutsideTouchable(false);
        //设置可以点击
        popupWindow.setTouchable(true);
        //进入退出的动画,指定刚才定义的style
        popupWindow.setAnimationStyle(R.style.ipopwindow_anim_style);
        openPopWindow();
        backgroundAlpha(0.4f, context);

    }

    /**
     * 按钮的监听
     */
    private static void openPopWindow() {
        //从底部显示
        popupWindow.showAtLocation(contentView, Gravity.BOTTOM, 0, 0);
    }

    private static void backgroundAlpha(float f, Activity context) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = f;
        context.getWindow().setAttributes(lp);
    }

    public interface Finish {
        void finish(String result);
    }
}
BeanCloneUtil 用来复制bean类,保证数据的修改不会影响其他map
public abstract class BeanCloneUtil {
    @SuppressWarnings("unchecked")
    public static <T> T cloneTo(T src) throws RuntimeException {
        ByteArrayOutputStream memoryBuffer = new ByteArrayOutputStream();
        ObjectOutputStream out = null;
        ObjectInputStream in = null;
        T dist = null;
        try {
            out = new ObjectOutputStream(memoryBuffer);
            out.writeObject(src);
            out.flush();
            in = new ObjectInputStream(new ByteArrayInputStream(memoryBuffer.toByteArray()));
            dist = (T) in.readObject();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (out != null)
                try {
                    out.close();
                    out = null;
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            if (in != null)
                try {
                    in.close();
                    in = null;
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
        }
        return dist;
    }
}
ProjectTitleAdapter  顶部的列表适配器
public class ProjectTitleAdapter extends RecyclerView.Adapter<ProjectTitleAdapter.ViewHolder> {
    private Context context;
    private List<ProjectTitleItemBean> list;
    private ProjectTitleItemClick itemClick;

    public ProjectTitleAdapter(Context context, List<ProjectTitleItemBean> list,
                               ProjectTitleItemClick itemClick) {
        this.context = context;
        this.list = list;
        this.itemClick = itemClick;
    }

    public ProjectTitleAdapter(Context context, List<ProjectTitleItemBean> list) {
        this.context = context;
        this.list = list;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ProjectTitleItemBean item = list.get(position);

        if (item.isChoose()) {
            holder.viewCircleNo.setVisibility(View.GONE);
            holder.viewCircleYes.setVisibility(View.VISIBLE);
        } else {
            holder.viewCircleNo.setVisibility(View.VISIBLE);
            holder.viewCircleYes.setVisibility(View.GONE);
        }

        holder.tvName.setText(item.getName());

        holder.tvName.setOnClickListener(view -> {
            if (item.isChoose()) {
                itemClick.itemListener(item, position);
            }
        });

    }

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

    class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView tvName;
        private final View viewCircleNo;
        private final View viewCircleYes;

        public ViewHolder(View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_name);
            viewCircleNo = itemView.findViewById(R.id.view_circle_no);
            viewCircleYes = itemView.findViewById(R.id.view_circle_yes);
        }
    }
}
ProjectAdapter 底部的列表适配器
public class ProjectAdapter extends RecyclerView.Adapter<ProjectAdapter.ViewHolder> {
    private Context context;
    private List<ProjectItemBean> list;
    private ProjectItemClick itemClick;

    public ProjectAdapter(Context context, List<ProjectItemBean> list,
                          ProjectItemClick itemClick) {
        this.context = context;
        this.list = list;
        this.itemClick = itemClick;
    }

    public ProjectAdapter(Context context, List<ProjectItemBean> list) {
        this.context = context;
        this.list = list;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ProjectItemBean item = list.get(position);

        if (item.isChoose()) {
            holder.tvName.setTypeface(null, Typeface.BOLD);
            holder.ivSelect.setVisibility(View.VISIBLE);
        } else {
            holder.tvName.setTypeface(null, Typeface.NORMAL);
            holder.ivSelect.setVisibility(View.INVISIBLE);
        }

        holder.tvName.setText(item.getName());

        holder.tvName.setOnClickListener(view -> {
            itemClick.itemListener(item);
        });

    }

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

    class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView tvName;
        private final ImageView ivSelect;

        public ViewHolder(View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_name);
            ivSelect = itemView.findViewById(R.id.iv_select);
        }
    }
}
ProjectTitleItemBean 顶部的实体类
public class ProjectTitleItemBean {
    private String name;
    private String id;
    private String code;
    private boolean isChoose;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public boolean isChoose() {
        return isChoose;
    }

    public void setChoose(boolean choose) {
        isChoose = choose;
    }
}
ProjectItemBean 底部实体类
public class ProjectItemBean implements Serializable {
    private String id;
    private String name;
    private String code;
    private boolean isChoose;
    private int level;
    private List<ProjectItemBean> childList = new ArrayList<>();
    private ProjectItemBean parent;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public boolean isChoose() {
        return isChoose;
    }

    public void setChoose(boolean choose) {
        isChoose = choose;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public List<ProjectItemBean> getChildList() {
        return childList;
    }

    public void setChildList(List<ProjectItemBean> childList) {
        this.childList = childList;
    }

    public ProjectItemBean getParent() {
        return parent;
    }

    public void setParent(ProjectItemBean parent) {
        this.parent = parent;
    }
}
ProjectTitleItemClick 顶部条目点击
public interface ProjectTitleItemClick {
    void itemListener(ProjectTitleItemBean itemBean,int position);
}
ProjectItemClick 底部条目点击
public interface ProjectItemClick {
    void itemListener(ProjectItemBean itemBean);
}
MainActivity 主页面的代码,顶部的集合默认给一条就行,后续的顶部集合自增或自减都是在适配器里完成的
public class MainActivity extends AppCompatActivity {

    private TextView tvContent;

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

        Button bottom = findViewById(R.id.btn_bottom);
        tvContent = findViewById(R.id.tvContent);
        bottom.setOnClickListener(v -> initPop());
    }

    private void initPop() {
        List<ProjectTitleItemBean> listTop = new ArrayList<>();
        ProjectTitleItemBean bean = new ProjectTitleItemBean();
        bean.setName("请选择");
        bean.setChoose(false);
        listTop.add(bean);

        List<ProjectItemBean> listChild = new ArrayList<>();

        ProjectItemBean bean1 = new ProjectItemBean();
        bean1.setName("第一页");
        bean1.setChoose(false);
        bean1.setParent(null);
        bean1.setLevel(0);

        List<ProjectItemBean> listChild1 = new ArrayList<>();
        ProjectItemBean bean11 = new ProjectItemBean();
        bean11.setName("第1-1页");
        bean11.setChoose(false);
        bean11.setParent(bean1);


        List<ProjectItemBean> listChild11 = new ArrayList<>();
        ProjectItemBean bean111 = new ProjectItemBean();
        bean111.setName("第1-1-1页");
        bean111.setChoose(false);
        bean111.setParent(bean1);
        bean111.setChildList(null);
        bean111.setLevel(2);
        listChild11.add(bean111);

        ProjectItemBean bean112 = new ProjectItemBean();
        bean112.setName("第1-1-2页");
        bean112.setChoose(false);
        bean112.setParent(bean1);
        bean112.setChildList(null);
        bean112.setLevel(2);
        listChild11.add(bean112);

        bean11.setChildList(listChild11);
        bean11.setLevel(1);
        listChild1.add(bean11);


        ProjectItemBean bean12 = new ProjectItemBean();
        bean12.setName("第1-2页");
        bean12.setChoose(false);
        bean12.setParent(bean1);
        bean12.setChildList(null);
        bean12.setLevel(1);
        listChild1.add(bean12);
        bean1.setChildList(listChild1);
        listChild.add(bean1);

        ProjectItemBean bean2 = new ProjectItemBean();
        bean2.setName("第二页");
        bean2.setChoose(false);
        bean2.setParent(null);
        bean2.setLevel(0);

        List<ProjectItemBean> listChild2 = new ArrayList<>();
        ProjectItemBean bean21 = new ProjectItemBean();
        bean21.setName("第2-1页");
        bean21.setChoose(false);
        bean21.setParent(bean1);
        bean21.setChildList(null);
        bean21.setLevel(1);
        listChild2.add(bean21);
        ProjectItemBean bean22 = new ProjectItemBean();
        bean22.setName("第2-2页");
        bean22.setChoose(false);
        bean22.setParent(bean1);
        bean22.setChildList(null);
        bean22.setLevel(1);
        listChild2.add(bean22);
        bean2.setChildList(listChild2);
        listChild.add(bean2);


        BottomPopUtils.showPopWindow(listTop, listChild, MainActivity.this,result -> {
            tvContent.setText(result);
        });
    }
}

接下来是布局:

activity_main

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">

    <Button
        android:id="@+id/btn_bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击弹框"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tvContent"
        android:layout_marginTop="20dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/btn_bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</androidx.constraintlayout.widget.ConstraintLayout>

adapter_project

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:background="#FFFFFF">

    <ImageView
        android:id="@+id/iv_select"
        android:visibility="invisible"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/pop_select"
        app:layout_constraintBottom_toBottomOf="@id/tv_name"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@id/tv_name" />

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_marginEnd="16dp"
        android:gravity="center_vertical"
        android:ellipsize="end"
        android:lines="1"
        android:textSize="18dp"
        app:layout_constraintEnd_toStartOf="@id/iv_select"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

pop_time_line

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="7dp"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"
        android:textSize="18dp"
        android:text="请选择"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/barrier"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/view_circle_no"
        android:layout_width="5dp"
        android:layout_height="5dp"
        android:background="@drawable/circle_no"
        app:layout_constraintBottom_toBottomOf="@id/tv_name"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@id/tv_name" />

    <View
        android:visibility="gone"
        android:id="@+id/view_circle_yes"
        android:layout_width="5dp"
        android:layout_height="5dp"
        android:background="@drawable/circle_yes"
        app:layout_constraintBottom_toBottomOf="@id/tv_name"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@id/tv_name" />

    <androidx.constraintlayout.widget.Barrier
        android:id="@+id/barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="end"
        app:constraint_referenced_ids="view_circle_no,view_circle_yes" />


</androidx.constraintlayout.widget.ConstraintLayout>

popupwindow 我的时间线设计的有点问题,有点偏差对不准,欢迎各位大佬提点修改方法

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/popwindow_radius_bg"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/iv_finish"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:paddingStart="10dp"
            android:paddingEnd="20dp"
            android:src="@mipmap/pop_finish"
            app:layout_constraintBottom_toBottomOf="@id/tv_title"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@id/tv_title" />

        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginTop="20dp"
            android:text="选择项目"
            android:textColor="#000000"
            android:textSize="18dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


        <View
            android:id="@+id/view1"
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_marginTop="10dp"
            android:background="@color/cardview_dark_background"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tv_title" />

        <View
            android:id="@+id/view2"
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_marginTop="10dp"
            android:background="@color/cardview_dark_background"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/rl_title" />

        <View
            android:id="@+id/view_line"
            android:layout_width="2dp"
            android:layout_height="0dp"
            android:layout_marginStart="22dp"
            android:layout_marginTop="18dp"
            android:layout_marginBottom="18dp"
            android:background="@color/cardview_dark_background"
            app:layout_constraintBottom_toBottomOf="@id/rl_title"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@id/rl_title" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rl_title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:layout_marginEnd="20dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/view1" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rl"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:layout_marginStart="20dp"
            android:layout_marginEnd="20dp"
            app:layout_constraintTop_toBottomOf="@id/view2" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>

接下来是drawable文件:

circle_no.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="100dp"/>

    <solid android:color="#FFFFFF" />

    <stroke
        android:width="1dp"
        android:color="@color/cardview_dark_background" />
</shape>

circle_yes.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="100dp"/>
    <solid android:color="@color/cardview_dark_background"/>
</shape>

popwindow_radius_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="14dp"/>
    <solid android:color="#FFFFFF" />
</shape>

接下来是anim,需要在res下创建anim文件夹,主要用作popwindow的弹出动画

pophidden_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="300"
        android:fromYDelta="0"
        android:toYDelta="50%p" />
    <alpha
        android:duration="300"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />
</set>

popshow_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:duration="300"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />

    <translate
        android:duration="300"
        android:fromYDelta="100%p"
        android:toYDelta="0" />
</set>

styles里面的代码

<resources>
    <style name="ipopwindow_anim_style">
        <item name="android:windowEnterAnimation">@anim/popshow_anim</item>
        <!-- 指定显示的动画xml -->
        <item name="android:windowExitAnimation">@anim/pophidden_anim</item>
        <!-- 指定消失的动画xml -->
    </style>
</resources>

还有两张图片没上传,是底部列表后面的对勾,还有popwindow的关闭按钮,这个你们自己找图片替换掉就行;文章来源地址https://www.toymoban.com/news/detail-605861.html

到了这里,关于Android 实现仿淘宝地址多级选择器的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Rockchip官方开发板的Android固件下载地址汇总

    这里汇总Rockchip官方开发板的Android固件地址 注:开发板丝印一般在板子的正面或者背面有印硬件版本号和板型 Android版本开发板丝印 RK_EVB1_RK3588_LP4XD200P232SD10H1_V11_20211215YWQ_final Android12 链接:https://pan.baidu.com/s/1LXwnlsRjrioUo1YERyiF3g 提取码:mn2v Android版本开发板丝印 RK_EVB1_RK3588

    2024年02月04日
    浏览(33)
  • Android 实现滑动数字选择器

    Android 滑动数字选择器是一种用户界面控件,它允许用户从一系列数字中选择一个值。用户可以通过滑动手势或点击手势来选择数字。以下是一些关于 Android 滑动数字选择器的信息和链接: Android NumberPicker:这是 Android 框架提供的原生数字选择器控件。它可以通过 XML 或代码创

    2024年02月07日
    浏览(25)
  • Flutter与Android开发:构建跨平台移动应用的新选择

    本文内容提纲如下: 介绍Flutter技术:Flutter是一种由Google推出的开源UI工具包,用于构建高性能、跨平台的移动应用。文章将介绍Flutter的基本概念、特点和优势,包括其快速的开发速度、一致的用户界面和丰富的UI组件库等。 Flutter与Android开发的对比:文章将对比Flutter与传统

    2023年04月21日
    浏览(48)
  • Android如何实现地图定位?Android studio+百度地图API+Android6.0系统实现地图显示、地址设置、点击地图定位功能(详细)

    文章说明: 本文初衷是为了记录毕设学习过程,避免忘记操作流程。该功能是毕业设计的Android软件端的功能之一,本文将从获取百度地图密钥(AK)开始,详细地对地图定位配置和相关代码进行说明, 文末将附上实现该功能的代码。后续等答辩完成会把整个Android端代码上传

    2024年02月03日
    浏览(47)
  • Android相册选择图片、相机拍照上传功能实现(上)

    先上效果图 下面就来说一下相册选择图片和相机拍照的实现 相册选择图片很简单,只需要通过 Intent 设置拉起就可以了 Intent 拉起相册 /** 打开相册 @param type 打开类型区分码(type是我用来区分回调的) / private void openGallery(int type) { Intent gallery = new Intent(Intent.ACTION_PICK); galler

    2024年04月16日
    浏览(43)
  • 安卓android日期选择器对话框 DatePickerDialog,实现日期选择,滚动式选择日期

    基本想法是:点击一个文本框,然后弹出日期选择器对话框,选择日期按下确定键后,文本框显示所选择的日期。可以有多种显示的主题,完整代码会在最后给出。 一、设置XML文件 记得给TextView取一个id,这里我取的id是:date 二、获取文本框组件 三、设置文本框的点击事件

    2023年04月15日
    浏览(38)
  • Android Studio 进行NDK开发,实现JNI,以及编写C++与Java交互(Java调用本地函数)并编译出本地so动态库

    1.首先认识一下NDK。 (1)什么是NDK? NDK全称是Native Development Kit,NDK提供了一系列的工具,帮助开发者快速开发C/C++的动态库,并能自动将so和java应用一起打包成apk。NDK集成了交叉编译器(交叉编译器需要UNIX或LINUX系统环境),并提供了相应的mk文件隔离CPU、平台、ABI等差异,

    2024年02月11日
    浏览(35)
  • android studio创建一个新的项目为什么默认是kotlin语言而选择不了java语言

    关于android studio语言选择的问题。 我在进入android studio为什么创建一个新项目之后选择不了java语言有什么办法可以解决。 解决办法:这个模式下选着一个Empty Activity模块就可以使用java语言。 这对于刚刚接触anaroid studio新手比较管用。  

    2024年02月11日
    浏览(41)
  • Android开发--实现Android引导页

    几乎每个app都有引导页,下面我们来做一个引导页的实现。 引导页,是指软件首次进入或者更新完成后,软件显示的页面,一般展示软件特色功能或者更新的内容。 先判断是否第一次启动app,如果是,则进入引导页(左右滑动切换查看,滑动到最后一页点击按钮进入首页)。

    2024年02月08日
    浏览(28)
  • Android 12(S) IPV4优先IPV6(优先使用IPv4地址)的实现

    根据RFC 6724中 规定 android 会优先选择IPv6 地址而不是 IPv4 地址,当整个网络中,同时支持IPv4和IPv6 地址时,设备中的应用请求服务器DNS时,会优先返回IPv6地址。 假如IPv6服务器支持内容不够完善,则应用显示内容会与IPv4服务器不一致,甚至有问题。 因此有需求是定制设备平

    2024年02月03日
    浏览(41)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包