当前位置: 首页 > news >正文

.xml 下拉列表_RecyclerView嵌套recyclerview实现二级下拉列表,包含自定义IOS对话框...

无图无真相,一言不合就上图:

9db53ff4848b5484cf78777b219a72f1.png

​​

f7e6a5b378d73743a577cf0759729059.png
  • 功能简介:下拉显示时间列表,点击时间弹出自定义的IOS对话框,然后选择对应的车牌号,跳转到第二个界面。

本以为是个很简单的东西,写了之后才发现坑不断,特此纪念下。(里面用的都是假数据,如需请求网络数据不止如何修改的可以联系我qq:2872960735) 代码有点长,麻烦看官慢慢看,接下来让我们开心的撸码吧!

becd6958390e708553d2d38a53e1463e.png

1、好了,因为这个东西还是比较大的,所以这边就不贴出全部布局,需要的可以去下载我的源码,首先我们先编写IOSDialog.class:

package 

2、编写IosDialog的布局文件dialog_check_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/lLayout_bg"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:background="@drawable/shape_editfee"
              android:orientation="vertical">

    <TextView
        android:id="@+id/txt_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="6dp"
        android:text="请选择车牌号"
        android:textSize="20sp"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rlv_car_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_marginTop="17dp"
        android:background="@color/gray"/>

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

        <TextView
            android:id="@+id/btn_neg"
            android:layout_width="wrap_content"
            android:layout_height="70dp"
            android:layout_weight="1"
            android:gravity="center"
            android:text="取消"
            android:textColor="@color/black"
            android:textSize="16sp"
            />

        <View
            android:id="@+id/img_line"
            android:layout_width="2dp"
            android:layout_height="match_parent"
            android:background="@color/gray"/>

        <TextView
            android:id="@+id/btn_pos"
            android:layout_width="wrap_content"
            android:layout_height="70dp"
            android:layout_weight="1"
            android:gravity="center"
            android:text="确定"
            android:textColor="@color/black"
            android:textSize="16sp"

            />
    </LinearLayout>

</LinearLayout>

3、编写IosDialog适配器DialogAdapter.class:

package com.lenz.arouter.recyclerviewtesttwo.adapter;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.lenz.arouter.recyclerviewtesttwo.R;
import com.lenz.arouter.recyclerviewtesttwo.SpUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: LJW
 * Date: 2019-07-17 14:24
 * Description: 自定义dialog里面的列表适配器
 */
public class DialogAdapter extends RecyclerView.Adapter<DialogAdapter.ViewHolder> {
    private List<String> cards;
    private Context mContext;
    private List<Boolean> isClicks;//控件是否被点击,默认为false,如果被点击,改变值,控件根据值改变自身颜色

    public DialogAdapter(List<String> beans, Context context) {
        cards = beans;
        mContext = context;
        isClicks =new ArrayList<>();
        for (int i = 0; i <cards.size() ; i++) {
            isClicks.add(false);
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup group, int i) {
        return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.in_item, null));
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        holder.tv.setText(cards.get(position));
        //当前位置为被点中时就改变颜色
        if (isClicks.get(position)){
            holder.tv.setBackgroundResource(R.drawable.shape_radius10_orange);
            holder.tv.setTextColor(Color.WHITE);
        }else {
            holder.tv.setBackgroundResource(R.drawable.shape_radius5_light_gray);
            holder.tv.setTextColor(Color.BLACK);
        }
        holder.tv.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onClick(View v) {
                SpUtils.putString(mContext,"carNumber",cards.get(position));
                //点击的时候,把所有的按钮都变回原色
                for (int i = 0; i < cards.size(); i++) {
                    isClicks.set(i,false);
                }
                //对点击的按钮,设置成true,以便后面变颜色
                isClicks.set(position,true);
                //刷新数据源改变item颜色
                notifyDataSetChanged();
            }
        });
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tv;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.tv);
        }
    }

4、编写styles.xml

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
     <!--自定义ios对话框样式-->
    <style name="AlertDialogStyle" parent="@android:style/Theme.Dialog">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowFrame">@null</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsTranslucent">true</item>
    </style>
</resources>

到这里我们自定义的IOS对话框就好了,让我们继续往下写:

427bee6d5099d7cf3d4058f18ac94bad.png

5、编写MainActivity.class类:

package com.lenz.arouter.recyclerviewtesttwo;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lenz.arouter.recyclerviewtesttwo.adapter.OutAdapter;
import com.lenz.arouter.recyclerviewtesttwo.bean.InBean;
import com.lenz.arouter.recyclerviewtesttwo.bean.OutBean;
import com.lenz.arouter.recyclerviewtesttwo.dialog.IosDialog;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private List<OutBean> mList;
    private OutAdapter mAdapter;
    private Context mContext;
    OutBean outBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
        mRecyclerView = findViewById(R.id.rlv_outside);
        //外部recyclerview假数据
        mList = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            outBean = new OutBean("厦门——广东线路:" + i, R.drawable.down, true);
            mList.add(outBean);
        }
        //dialog假数据
        final List<String> dialogList = new ArrayList<>(); //用来测试车牌号只能单选的数据
        dialogList.add("闽D1537");
        dialogList.add("闽D3453");
        dialogList.add("闽D6785");
        dialogList.add("闽D2345");
        dialogList.add("闽D7898");
        mAdapter = new OutAdapter(mList, mContext);
        mAdapter.setOnItemClickListener(new OutAdapter.OnInItemClickListener() {
            @Override
            public void onExcuteCheck(final InBean inBean, final TextView tv) {
                new IosDialog(mContext).builder().setTitle("请选择车牌号").
                        setRecyclerView(dialogList).setPositiveButton("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //跳转到第二界面的同时,把原来的时间背景和字体颜色变回来
                        tv.setBackground(getResources().getDrawable(R.drawable.shape_radius5_light_gray));
                        tv.setTextColor(Color.BLACK);
                        String time =inBean.getRouteTime();
                        String carNumber =SpUtils.getString(mContext,"carNumber","");
                        Intent  intent =new Intent(mContext,Main2Activity.class);
                        intent.putExtra("time",time);
                        intent.putExtra("carNumber",carNumber);
                        startActivity(intent);
                    }
                }).setNegativeButton("取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //点击取消时,把原先选择的时间变回原色
                        tv.setBackground(getResources().getDrawable(R.drawable.shape_radius5_light_gray));
                        tv.setTextColor(Color.BLACK);
                    }
                }).setCancelable(false).show();//setCancelable属性:点击对话框以为区域无效
            }
        });
        //使用这个属性让布局填满
        LinearLayoutManager layoutManager = new LinearLayoutManager(this) {
            @Override
            public RecyclerView.LayoutParams generateDefaultLayoutParams() {

                return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            }
        };
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(mAdapter);
    }
}

115a214866d3b4036af75acf57eb0fbe.png

6、activity的布局文件activity_main.xml:

<?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=".MainActivity"
    android:background="@color/colorPrimary">

  <android.support.v7.widget.RecyclerView
      android:layout_margin="5dp"
      android:id="@+id/rlv_outside"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>

</LinearLayout>

到这里我们已经把Activity写好了,离胜利 又近了一步,接下来我们要憋足了气,把内部和外部的recyclerview适配器写了。

39309d7a7b4dbd0daab5e1ac57036caf.png

98188c71360f8e9acc26b83f06bd69e2.png

7、编写外层recyclerview的布局out_item.xml:

<?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:orientation="vertical"
              android:padding="5dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/shape_white_corner_10dp"
        android:orientation="vertical">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="60dp">

            <TextView
                android:id="@+id/tv_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="10dp"
                android:text="123e423"
                android:textSize="20sp" />
<!-- 默认是让这个图标向下的-->
            <ImageView
                android:id="@+id/iv_show"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right|center_vertical"
                android:layout_marginRight="10dp"
                android:background="@drawable/down" />
        </FrameLayout>

        <View
            android:id="@+id/view_divider"
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:background="@color/inside_showtime_carnum"
            android:visibility="gone" />
        <android.support.v7.widget.RecyclerView
            android:id="@+id/rlv_inside"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/shape_bottom_corner_10dp" />
    </LinearLayout>
</LinearLayout>

8、编写外层recyclerview的适配器OutAdapter.class:

package com.lenz.arouter.recyclerviewtesttwo.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lenz.arouter.recyclerviewtesttwo.R;
import com.lenz.arouter.recyclerviewtesttwo.bean.InBean;
import com.lenz.arouter.recyclerviewtesttwo.bean.OutBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: LJW
 * Date: 2019-07-17 10:12
 * Description: 外部recyclerview适配器
 */
public class OutAdapter extends RecyclerView.Adapter<OutAdapter.MyHolder> implements InAdapter.OnItemClickListener{
    private List<OutBean> mList;
    private Context mContext;
    private List<Integer> positions = new ArrayList<>();

    public OutAdapter(List<OutBean> list, Context context) {
        mList = list;
        mContext = context;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup group, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.out_item, null);
        MyHolder myHolder = new MyHolder(view);
        return myHolder;
    }

    @Override
    public void onBindViewHolder(final MyHolder holder, final int i) {
        final OutBean outBean = mList.get(i);
        holder.mView.setTag(outBean);
        holder.title.setText(outBean.getTitle());
        holder.img.setBackgroundResource(outBean.getImg());
        List<InBean> inBeans = new ArrayList<>();
        for (int j = 0; j < 10; j++) {
            InBean inBean = new InBean("时间" + j);
            inBeans.add(inBean);
        }
        InAdapter inAdapter = new InAdapter(inBeans, mContext);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 3);
        holder.mRecyclerView.setLayoutManager(gridLayoutManager);
        inAdapter.setOnItemClickListener(OutAdapter.this);//记得在这里监听内部adapter
        holder.mRecyclerView.setAdapter(inAdapter);
        holder.mRecyclerView.setVisibility(View.GONE);//里面的recyclerview是先隐藏的
        holder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (holder.mRecyclerView.getVisibility() == View.VISIBLE) {
                    holder.img.setBackgroundResource(R.drawable.down);
                    holder.mRecyclerView.setVisibility(View.GONE);
                    holder.mDivider.setVisibility(View.GONE);
                }else {
                    //第一次点击的时候,因为最开始里面的recyclerview是隐藏,所以会走这里
                    holder.img.setBackgroundResource(R.drawable.up);
                    holder.mDivider.setVisibility(View.VISIBLE);
                    holder.mRecyclerView.setVisibility(View.VISIBLE);
                }

            }
        });


    }
    //实现内部Recyclerview适配器的接口
    @Override
    public void onClickItem(InBean inBean,TextView tv) {
        if (mOnItemClickListener!=null){
            mOnItemClickListener.onExcuteCheck(inBean,tv);//通过接口回调返回到Activity处理逻辑
        }
    }
    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class MyHolder extends RecyclerView.ViewHolder {
        private TextView title;
        private ImageView img;
        private RecyclerView mRecyclerView;
        private View mDivider;
        private View mView;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            mView = itemView;
            title = itemView.findViewById(R.id.tv_title);
            img = itemView.findViewById(R.id.iv_show);
            mDivider = itemView.findViewById(R.id.view_divider);
            mRecyclerView = itemView.findViewById(R.id.rlv_inside);
        }
    }
    private OnInItemClickListener mOnItemClickListener;
    public interface OnInItemClickListener {
        void onExcuteCheck(InBean inBean,TextView tv);
    }
    public void setOnItemClickListener(OnInItemClickListener onItemClickListener){
        this.mOnItemClickListener =onItemClickListener;
    }

}

​​9、编写内层recyclerview的布局文件in_item.xml:

<?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="match_parent"
    >

    <TextView
        android:layout_margin="8dp"
        android:padding="10dp"
        android:gravity="center"
        android:background="@drawable/shape_radius5_light_gray"
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:text="nihaoya"
        android:textColor="@color/black"
        android:textSize="16sp"/>
</LinearLayout>

10、编写内层recyclerview的适配器InAdapter.class:

package com.lenz.arouter.recyclerviewtesttwo.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lenz.arouter.recyclerviewtesttwo.R;
import com.lenz.arouter.recyclerviewtesttwo.bean.InBean;

import java.util.List;

/**
 * Author: LJW
 * Date: 2019-07-17 10:56
 * Description: 内部recyclerview适配器
 */
public class InAdapter extends RecyclerView.Adapter<InAdapter.ViewHolder>{
    private List<InBean> mBeans;
    private Context mContext;

    public InAdapter(List<InBean> beans, Context context) {
        mBeans = beans;
        mContext = context;
    }

    private OnItemClickListener mOnItemClickListener;

    public interface OnItemClickListener {
        void onClickItem(InBean inBean,TextView tv);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup group, int i) {
        return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.in_item, null));
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        holder.tv.setText(mBeans.get(position).getRouteTime());
        holder.tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    //当内部recyclerview的item被点击,就接口回调出去到OutAdapter,再到activity处理业务逻辑
                    mOnItemClickListener.onClickItem(mBeans.get(position),holder.tv);
                    holder.tv.setBackgroundResource(R.drawable.shape_radius10_orange);
                    holder.tv.setTextColor(Color.WHITE);
                }
                //简单的recyclerview点击事件
//                int position =holder.getAdapterPosition();
//                InBean inBean =mBeans.get(position);
//                Toast.makeText(mContext, "这个是"+inBean.getTv(), Toast.LENGTH_SHORT).show();
                //当点击item时就让他背景和字体颜色改变

            }
        });
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tv;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.tv);
        }
    }
}

主要的代码都在这了,如果还有不懂劳烦下载源码,注释写的很清楚,再不懂的也可以来骚扰我。

a87baf9243770737904075eeb6f09275.png

源码地址:
https://download.csdn.net/download/qq_38363506/11445305​

相关文章:

  • java怎么实现自动退出功能_教你用python操作Excel 轻松实现自动读写功能
  • python 语义网络_Python好书从入门到进阶整理好送你
  • cloud压缩怎么彻底删除_财务软件T+Cloud操作指南
  • python字符串反码_python中的进制转换和原码,反码,补码
  • python假设检验和区间估计_推断统计分析(三):python实现假设检验
  • c++ char 转int_程序员每日一题-int和char还可以一起玩耍
  • java double转int_Java中的换形师-数据类型转换
  • matplotlib 标签_[Matplotlib官方教程]使用指南 0.7
  • python微信群发消息_Python3 itchat实现微信定时发送群消息的实例代码
  • easyconnect无法在mac上使用_如何在Mac上使用pyenv运行Python的多个版本
  • isdebugenabled_日志框架中为什么有isDebugEnabled方法?
  • python hashlib_python import hashlib出现问题
  • opencvpython中文文档_OpenCV中文官方文档
  • dataframe添加一列并迭代赋值_Java 泛型和迭代器详解
  • c# 多线程单例模式_设计模式的征途—1.单例(Singleton)模式
  • Google 是如何开发 Web 框架的
  • #Java异常处理
  • 《Java8实战》-第四章读书笔记(引入流Stream)
  • 8年软件测试工程师感悟——写给还在迷茫中的朋友
  • Android开发 - 掌握ConstraintLayout(四)创建基本约束
  • - C#编程大幅提高OUTLOOK的邮件搜索能力!
  • Docker 笔记(1):介绍、镜像、容器及其基本操作
  • JavaScript 一些 DOM 的知识点
  • JavaScript服务器推送技术之 WebSocket
  • magento2项目上线注意事项
  • Material Design
  • node学习系列之简单文件上传
  • vue.js框架原理浅析
  • 回顾2016
  • 如何使用 JavaScript 解析 URL
  • 腾讯优测优分享 | Android碎片化问题小结——关于闪光灯的那些事儿
  • SAP CRM里Lead通过工作流自动创建Opportunity的原理讲解 ...
  • #include<初见C语言之指针(5)>
  • #Linux杂记--将Python3的源码编译为.so文件方法与Linux环境下的交叉编译方法
  • #NOIP 2014#Day.2 T3 解方程
  • %3cscript放入php,跟bWAPP学WEB安全(PHP代码)--XSS跨站脚本攻击
  • (附源码)php投票系统 毕业设计 121500
  • (附源码)springboot宠物管理系统 毕业设计 121654
  • (切换多语言)vantUI+vue-i18n进行国际化配置及新增没有的语言包
  • (实战)静默dbca安装创建数据库 --参数说明+举例
  • (一)搭建springboot+vue前后端分离项目--前端vue搭建
  • (转)C语言家族扩展收藏 (转)C语言家族扩展
  • (转)IOS中获取各种文件的目录路径的方法
  • (转)socket Aio demo
  • (转)真正的中国天气api接口xml,json(求加精) ...
  • (轉貼) UML中文FAQ (OO) (UML)
  • (轉貼) 寄發紅帖基本原則(教育部禮儀司頒布) (雜項)
  • ..thread“main“ com.fasterxml.jackson.databind.JsonMappingException: Jackson version is too old 2.3.1
  • .【机器学习】隐马尔可夫模型(Hidden Markov Model,HMM)
  • .gitignore文件—git忽略文件
  • .net 7 上传文件踩坑
  • .NET Standard、.NET Framework 、.NET Core三者的关系与区别?
  • .Net 中的反射(动态创建类型实例) - Part.4(转自http://www.tracefact.net/CLR-and-Framework/Reflection-Part4.aspx)...
  • /proc/interrupts 和 /proc/stat 查看中断的情况
  • ?