美文网首页各种view
RecycleView的多布局以及滑动到某一距离来做显示隐藏某些

RecycleView的多布局以及滑动到某一距离来做显示隐藏某些

作者: 鹅鹅鹅曲项向天歌呀 | 来源:发表于2018-07-09 15:51 被阅读65次

    公司最近有一新需求,效果如下图.

    演示图.gif
    思前想后,想了两种种办法去做.
    第一种是recycleView的多布局
    第二种用scrollview嵌套tablayout+viewpager+fragment.
    显然,第二种要显得麻烦一些,我也懒得去试验,直接采用第一种.
    这是一个有帮助的文章链接
    分析一下布局
    第一分部
    1.jpg
    第二部分 2.png
    第三部分
    3.png
    分析数据
    一共有两个接口
    第一部分是一个接口,返回各种字段需要展示的字段
    第二部分第三部分是一个接口,返回一个字段+一个集合
    我们要把两个接口的数据塞到一个集合里面,所以,我们定义一个泛型为Serializable的集合
    var mList = arrayListOf<Serializable>()
    这样就可以吧各种数据塞进去了,注意,bean要实现Serializable.
    我使用kotlin写的,Object泛型报错,我不晓得为啥,为了完成工作 任务,还米有研究.
    好啦,接下来就请求数据,去adapter里面去展示了呗
    各小部分布局文件就不展示了哦,我把最外层布局写出来~~~

    布局:
    封装了一下recycleView,自带刷新和加载更多的功能,普通的也可以哈
    我是将悬浮导航栏隐藏了,当滑动与recycleView中的导航栏重合的时候展示出来,否则则隐藏.

        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <include layout="@layout/common_title_layout" />
    
        <FrameLayout
            android:id="@+id/fl_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
    
            <com.yangsu.jinqian.library.pulltorefresh.library.PullToRefreshRecyclerView
                android:id="@+id/ptrrv_refreshable"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
    
            <LinearLayout
                android:id="@+id/ll_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/white"
                android:orientation="horizontal"
                android:visibility="gone">
    
                <RadioGroup
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/dpValue42"
                    android:layout_marginLeft="@dimen/dpValue5"
                    android:layout_marginRight="@dimen/dpValue5"
                    android:layout_marginTop="@dimen/dpValue5"
                    android:orientation="horizontal">
    
                    <RadioButton
                        android:id="@+id/rb_one2"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:background="@drawable/agent_home_center"
                        android:button="@null"
                        android:checked="true"
                        android:gravity="center"
                        android:orientation="vertical"
                        android:padding="@dimen/dpValue5"
                        android:text="待确认"
                        android:textColor="@color/agent_text_color"
                        android:textSize="@dimen/textSizeValue15">
    
    
                    </RadioButton>
    
                    <RadioButton
                        android:id="@+id/rb_two2"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:background="@drawable/agent_home_center"
                        android:button="@null"
                        android:gravity="center"
                        android:orientation="vertical"
                        android:padding="@dimen/dpValue5"
                        android:text="已完成"
                        android:textColor="@color/agent_text_color"
                        android:textSize="@dimen/textSizeValue15" />
    
                    <RadioButton
                        android:id="@+id/rb_three2"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:background="@drawable/agent_home_center"
                        android:button="@null"
                        android:gravity="center"
                        android:orientation="vertical"
                        android:padding="@dimen/dpValue5"
                        android:text="待取消"
                        android:textColor="@color/agent_text_color"
                        android:textSize="@dimen/textSizeValue15" />
    
                    <RadioButton
                        android:id="@+id/rb_four2"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:background="@drawable/agent_home_center"
                        android:button="@null"
                        android:gravity="center"
                        android:orientation="vertical"
                        android:padding="@dimen/dpValue5"
                        android:text="已取消"
                        android:textColor="@color/agent_text_color"
                        android:textSize="@dimen/textSizeValue15" />
                </RadioGroup>
    
            </LinearLayout>
    
    
        </FrameLayout>
    </LinearLayout>
    

    最重要的是adapter,要展示不同的布局,加载不同的数据
    上代码

    class AgentHomeAdapter(context: Context, list: ArrayList<Serializable>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
        var context: Context
        var list: List<Serializable>
        var inflater: LayoutInflater
       var headerHolder: HeaderHolder? = null
    
        init {
            this.context = context
            this.list = list
            this.inflater = LayoutInflater.from(context)
    
        }
        /**
         *我是根据位置来加载不同的布局
         *第0个位置,ItemViewType为1
         *第1个位置,ItemViewType为2
         *其他位置均为3,因为都是普通的列表啦
        */
        override fun getItemViewType(position: Int): Int {
           return if (position == 0) {
                1
            } else if (position == 1) {
                2
            } else {
                3
            }
        }
    
        override fun getItemCount(): Int {
            LogUtils.e("长度:" + list.size)
            return list.size
        }
     /**
         *根据不同ItemViewType来加载不同的布局
         *1为第一部分
         *2为第二部分
         *3为第三部分,也就是普通的列表
        */
        override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder {
            return if (viewType == 1) {
                headerHolder = HeaderHolder(inflater.inflate(R.layout.agent_top_layout, parent, false))
                headerHolder!!
            } else if (viewType == 2) {
                MiddleHolder(inflater.inflate(R.layout.agent_middle_layout, parent, false))
            } else {
                AgentHolder(inflater.inflate(R.layout.activity_my_order_info_item, parent, false))
            }
        }
     /**
         *根据不同ItemViewType,讲bean强转成不同的数据,然后直接设置到布局里就完事了
        */
        override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
            var bean = list.get(position)
            var itemViewType = getItemViewType(position)
            if (itemViewType == 1) {
                if (bean is AgentBean.DataBean.ContentBean) {
                 
                }
            }
            if (itemViewType == 2) {
                if (holder is MiddleHolder) {
                    (context as AgencyActivity).rb_1 = holder.rb_one
                    (context as AgencyActivity).rb_2 = holder.rb_two
                    (context as AgencyActivity).rb_3 = holder.rb_three
                    (context as AgencyActivity).rb_4 = holder.rb_four
                }
            }
            if (itemViewType == 3) {
                if (bean is MyOrderInfoBean.DataBean.ContentBean.ProxyOrdersDataBean) {
                    if (holder is AgentHolder) {
    
                }
            }
        }
        }
    
        class HeaderHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            var agentMoney: TextView
            .......
            init {
                agentMoney = itemView.findViewById(R.id.agentMoney)
               .......
    
    
            }
    
        }
    
        class MiddleHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            var tv_account: TextView
             ......
    
            init {
                tv_account = itemView.findViewById(R.id.tv_account)
                 ......
    
    
            }
    
        }
    
        class AgentHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            var tv_status: TextView
            .......
    
            init {
                tv_status = itemView.findViewById(R.id.tv_status)
                ......
    
            }
    
        }
    }
    
    

    下面是界面请求数据操作;

    class MainActivity : BaseActivity(), View.OnClickListener {
    
    
        var ruleUrl: String? = null
        var protocolUrl: String? = null
        lateinit var recyclerView: RecyclerView
        var mList = arrayListOf<Serializable>()
        lateinit var adapter: AgentHomeAdapter
        var pageNum: Int = 1
        var orderStatus: Int = 1
        var buttonIndex: Int = 1
    
        public var rb_1: RadioButton? = null
        public var rb_2: RadioButton? = null
        public var rb_3: RadioButton? = null
        public var rb_4: RadioButton? = null
        public var tv_animal: TextView? = null
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_agency)
            initView()
        }
    
        private fun initView() {
            //标题+右上角弹框
            setTitleWithBack("代理账户")
             rb_one2.setOnClickListener(this)
            rb_two2.setOnClickListener(this)
            rb_three2.setOnClickListener(this)
            rb_four2.setOnClickListener(this)
            ptrrv_refreshable.mode = PullToRefreshBase.Mode.BOTH
            recyclerView = ptrrv_refreshable.refreshableView
            var layoutManager = LinearLayoutManager(this)
            recyclerView.layoutManager = layoutManager
            adapter = AgentHomeAdapter(this@AgencyActivity, mList)
            recyclerView.adapter = adapter
    
        //这里是判断隐藏的导航栏显示和隐藏的,这个layoutManager.findFirstVisibleItemPosition()得到的position的屏幕中可见的第一条item的postion,就是0,1,2,3,4,5,6.......我的第一部分就是第0个positoin,>0则显示,<0则隐藏.
            recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
    
                override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
                    super.onScrolled(recyclerView, dx, dy)
                    if (layoutManager != null) {
                        //当前条目索引
                        val position = layoutManager.findFirstVisibleItemPosition()
                        if (position > 0) {
                            //做显示布局操作
                            ll_title.setVisibility(View.VISIBLE);
                        } else {
                            //做隐藏布局操作
                            ll_title.setVisibility(View.GONE);
                        }
                    }
                }
    
            })
    
    
            ptrrv_refreshable.setOnRefreshListener(object : PullToRefreshBase.OnRefreshListener2<RecyclerView> {
                override fun onPullDownToRefresh(refreshView: PullToRefreshBase<RecyclerView>?) {
                    pageNum = 1
                    getServiceDate()
                }
    
                override fun onPullUpToRefresh(refreshView: PullToRefreshBase<RecyclerView>?) {
    
                    pageNum++
                    getOrderInfoList()
                }
    
            })
    
    
            adapter.setItemViewClickListener(object : AgentHomeAdapter.ItemViewClickListener {
                override fun onClick(view: View) {
                    val intent = Intent()
                    when (view.id) {
                        R.id.agentMoneyPutIn -> {//代理额度转入
                        }
                        R.id.rechargeToFriend -> {//给好友充值余额
                         }
                        R.id.image_red -> {//红包
                         }
                        R.id.agent_tv_money -> {//红包
                       }
                        R.id.ll_award -> {//累计奖励
                          }
                        R.id.rb_one -> {//待确认
                            buttonIndex = 1
                            updateButtonIndex()//去改变隐藏的导航栏的状态
                            orderStatus = 1
                            pageNum = 1
                            getOrderInfoList()
                        }
                        R.id.rb_two -> {//已完成
                            buttonIndex = 2
                            updateButtonIndex()
                            orderStatus = 2
                            pageNum = 1
                            getOrderInfoList()
    
                        }
                        R.id.rb_three -> {//待取消
                            buttonIndex = 3
                            updateButtonIndex()
                            orderStatus = 3
                            pageNum = 1
                            getOrderInfoList()
    
                        }
                        R.id.rb_four -> {//已取消
                            buttonIndex = 4
                            updateButtonIndex()
                            orderStatus = 4
                            pageNum = 1
                            getOrderInfoList()
    
                        }
                    }
                }
    
                override fun onItemClick(view: View, position: Int) {
                    when (view.id) {
                        R.id.tv_right_btn_desc -> {//确认放款
                         }
                        R.id.tv_left_btn_desc -> {//取消订单
                      
                        }
                        R.id.tv_see_record -> {//查看记录
                      
                        }
                        R.id.tv_agree_cancel -> {//同意取消
                        }
                    }
                }
    
    
            })
            getServiceDate()
    
        }
    
        fun updateButtonIndex() {
            if (buttonIndex === 1) {
                rb_one2.isChecked = true
            }
            if (buttonIndex === 2) {
                rb_two2.isChecked = true
            }
            if (buttonIndex === 3) {
                rb_three2.isChecked = true
            }
            if (buttonIndex === 4) {
                rb_four2.isChecked = true
            }
        }
    //隐藏的导航栏的点击事件
        override fun onClick(p0: View?) {
            when (p0?.id) {
                R.id.rb_one2 -> {
                    rb_1?.isChecked = true//去改变adapter里面按钮的状态
                    orderStatus = 1
                    pageNum = 1
                    getOrderInfoList()
                }
                R.id.rb_two2 -> {
                    rb_2?.isChecked = true
                    orderStatus = 2
                    pageNum = 1
                    getOrderInfoList()
                }
                R.id.rb_three2 -> {
                    rb_3?.isChecked = true
                    orderStatus = 3
                    pageNum = 1
                    getOrderInfoList()
                }
                R.id.rb_four2 -> {
                    rb_4?.isChecked = true
                    orderStatus = 4
                    pageNum = 1
                    getOrderInfoList()
                }
            }
        }
    
        //首页头部数据
        fun getServiceDate() {
            val request = object : BaseStringRequest(Request.Method.POST, Constants.SERVER_URL,
                    object : BaseResponseListener() {
                        override fun onResponse(response: String) {
                            super.onResponse(response)
                            LogUtils.e("mcy---" + response)
                            dismissProgressDialog()
                            try {
                                val bean = Gson().fromJson(response, AgentBean::class.java)
                                if (bean.ret == ResponseCode.REQUEST_SUCESS) {//200
                                   //这里非常重要,先清空一下集合,讲第一部分数据,返回的bean塞到集合的第0个位置里面去,然后再去请求第二个接口
                                    mList.clear()
                                    mList.add(0, bean.data.content)
                                    getOrderInfoList()
                                } else {//400
                                    ToastUtil.showToast(bean.msg)
                                }
                            } catch (e: Exception) {
                                e.printStackTrace()
                                ToastUtil.showToast(this@AgencyActivity, getString(R.string.data_error))
                            }
    
                        }
                    }, object : BaseErrorListener() {
                override fun onErrorResponse(volleyError: VolleyError) {
                    super.onErrorResponse(volleyError)
                    dismissProgressDialog()
    
                }
            }, this) {
                @Throws(AuthFailureError::class)
                override fun getParams(): Map<String, String>? {
                    val map = super.getParams()
                    map.put("service", Constants.SERVICE_AGENCY_HOME)
                    map.put("token", UtilFunction.getInstance().calParamsMD5(map))
                    return map
                }
            }
            VolleyUtil.getQueue(this@AgencyActivity).add(request)
            showProgressDialog()
        }
    
        //首页列表数据
        private fun getOrderInfoList() {
            val stringRequest = object : BaseStringRequest(Request.Method.POST, Constants.SERVER_URL,
                    object : BaseResponseListener() {
                        override fun onResponse(response: String) {
                            super.onResponse(response)
                            dismissProgressDialog()
                            try {
                                LogUtils.e("mcy---list-" + response)
                                ptrrv_refreshable.onRefreshComplete()
                                val beans = Gson().fromJson(response, MyOrderInfoBean::class.java)
                                if (beans.ret == ResponseCode.REQUEST_SUCESS) {
                                    var proxy_orders_data = beans.data.content.proxy_orders_data
                                    if (pageNum == 1) {
                                    //这里也非常重要,这是第二部分展示的数据,塞到集合的第1个位置里面                      
                                        mList.add(1, beans.data.content.account)
                                        if (proxy_orders_data != null && proxy_orders_data.size > 0) {
                                  //这里是列表的数据,直接塞到集合里面就是
                                            mList.addAll(proxy_orders_data)
                                        }
                                    } else {
                                        mList.addAll(proxy_orders_data)
                                    }
                                    adapter.notifyDataSetChanged()
    
                                } else {
                                    if (beans.ret == ResponseCode.NO_RESULT_FOUND && pageNum == 1) {
                                        var serializable = mList.get(0)
                                        mList.clear()
                                        mList.add(0, serializable)
                                        mList.add(1, beans.data.content.account)
                                        mList.addAll(beans.data.content.proxy_orders_data)
                                        adapter.notifyDataSetChanged()
                                        ToastUtil.showToast(getString(R.string.no_result_found))
                                    } else {
                                        ToastUtil.showToast(if (beans.msg == null) "" else beans.msg)
                                    }
                                }
                            } catch (e: Exception) {
                                e.printStackTrace()
                            }
    
                        }
                    }, object : BaseErrorListener() {
                override fun onErrorResponse(error: VolleyError) {
                    dismissProgressDialog()
                    pageNum = if (pageNum - 1 < 1) 1 else pageNum - 1
                    ptrrv_refreshable.onRefreshComplete()
                    super.onErrorResponse(error)
                }
            }, this) {
                @Throws(AuthFailureError::class)
                override fun getParams(): Map<String, String>? {
                    //在这里设置需要post的参数
                    val map = super.getParams()
                    map.put("service", Constants.SERVICE_RECHARGE_ORDER_LIST)
                    map.put("page_now", "" + pageNum)
                    map.put("page_size", "10")
                    map.put("status", "" + orderStatus)//订单状态:1待确认 2已完成 3待取消 4:已取消
                    map.put("token", UtilFunction.getInstance().calParamsMD5(map))
                    return map
                }
            }
    
            VolleyUtil.getQueue(this).add(stringRequest)
        }
    

    我的语言组织能力有限,目前只能做成这样了.不明白的可以联系我 QQ1032528317

    相关文章

      网友评论

      本文标题:RecycleView的多布局以及滑动到某一距离来做显示隐藏某些

      本文链接:https://www.haomeiwen.com/subject/mdzzuftx.html