【Android】折叠式布局+RecyclerView上拉加载和下拉刷新

博客先展示了实现效果的图片,接着给出界面代码main_activity.xml,提到界面代码网上有很多,随后表示将展示activity代码,最后告知整个实现的代码查看处。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先上图片看一下效果

     20190618145148.jpg                        20190618145148.jpg 

 

界面代码main_activity.xml的代码如下:

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

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:background="@color/white"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_height="match_parent">

        <!-- android:fitsSystemWindow = "true" 表示整个布局展示是整个屏幕出去状态栏,标题栏和导航栏剩下的区域-->
        <android.support.design.widget.AppBarLayout
            android:id="@+id/layout_appbar"
            android:layout_width="match_parent"
            android:layout_height = "wrap_content"
            android:background="@color/white"
            >
            <!--
             app:expandedTitleMarginStart="10dp"
            设置扩张时候(还没有收缩时)title离屏幕左边的距离

             app:contentScrim="?attr/colorPrimary"
            设置当完全CollapsingToolbarLayout折叠(收缩)后的背景颜色
            -->
            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/ctb"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true"
                app:contentScrim="@color/white"
                app:expandedTitleMarginStart="10dp"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

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

                        <TextView
                            android:id="@+id/tv_Name_and_quantity"
                            android:layout_width="match_parent"
                            android:layout_height="32dp"
                            android:text="------------"
                            android:textColor="#ff131314"
                            android:textSize="16dp"
                            android:gravity="center|left"
                            android:layout_marginTop="12dp"
                            android:layout_marginLeft="12dp"
                            android:layout_marginRight="12dp"
                            />

                        <LinearLayout
                            android:id="@+id/ll_date"
                            android:layout_width="match_parent"
                            android:layout_height="32dp"
                            android:orientation="horizontal"
                            android:layout_marginTop="5dp"
                            android:layout_marginLeft="12dp"
                            android:layout_marginRight="12dp"
                            android:gravity="center"
                            >

                            <LinearLayout
                                android:id="@+id/ll_start_date"
                                android:layout_width="0dp"
                                android:layout_height="26dp"
                                android:layout_weight="0.4"
                                android:background="@drawable/shape_editext_white"

                                android:orientation="horizontal"
                                android:gravity="center"
                                >
                                <TextView
                                    android:id="@+id/tv_start_date"
                                    android:layout_width="0dp"
                                    android:layout_height="wrap_content"
                                    android:layout_weight="0.5"
                                    android:text=""
                                    android:hint="开始日期"
                                    android:textSize="12sp"
                                    android:paddingLeft="5dp"

                                    />
                                <TextView
                                    android:layout_width="20dp"
                                    android:layout_height="20dp"
                                    android:background="@drawable/rili"
                                    android:layout_marginRight="5dp"
                                    />

                            </LinearLayout>
                            <View
                                android:layout_width="15dp"
                                android:layout_height="1dp"
                                android:background="#cdcdcd"
                                />

                            <LinearLayout
                                android:id="@+id/ll_end_date"
                                android:layout_width="0dp"
                                android:layout_height="26dp"
                                android:layout_weight="0.4"
                                android:gravity="center"
                                android:background="@drawable/shape_editext_white"
                                android:orientation="horizontal"
                                >
                                <TextView
                                    android:id="@+id/tv_end_date"
                                    android:layout_width="0dp"
                                    android:layout_weight="0.8"
                                    android:layout_height="wrap_content"
                                    android:text=""
                                    android:hint="结束日期"
                                    android:textSize="12sp"
                                    android:paddingLeft="5dp"
                                    />

                                <TextView
                                    android:layout_alignRight="@id/tv_start_date"
                                    android:layout_width="20dp"
                                    android:layout_height="20dp"
                                    android:layout_marginRight="5dp"
                                    android:background="@drawable/rili"
                                    />
                            </LinearLayout>

                        </LinearLayout>

                        <LinearLayout
                            android:layout_marginTop="10dp"
                            android:layout_marginLeft="12dp"
                            android:layout_marginRight="12dp"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:gravity="center"
                            android:orientation="horizontal">

                            <RelativeLayout
                                android:id="@+id/ll_paymentNumber"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                >
                                <EditText
                                    android:id="@+id/et_searchContent"
                                    android:layout_width="match_parent"
                                    android:layout_height="26dp"
                                    android:layout_marginRight="60dp"
                                    android:textSize="12sp"
                                    android:textColorHint="#b7b7b7"
                                    android:paddingLeft="4dp"
                                    android:paddingRight="4dp"
                                    android:hint="------------"
                                    android:maxLines="1"
                                    android:background="@drawable/shape_editext_white"
                                    />
                                <TextView
                                    android:id="@+id/tv_query"
                                    android:layout_width="48dp"
                                    android:layout_height="27dp"
                                    android:textSize="12sp"
                                    android:padding="5dp"
                                    android:background="@drawable/bluebutton_click_style"
                                    android:text="查询"
                                    android:layout_marginLeft="10dp"
                                    android:textColor="@color/white"
                                    android:gravity="center"
                                    android:layout_alignParentRight="true"
                                    />
                            </RelativeLayout>
                        </LinearLayout>


                    </LinearLayout>


                    <View
                        android:id="@+id/view_a"
                        android:layout_below="@+id/ll_LinearLayout_a"
                        android:layout_marginTop="12dp"
                        android:layout_width="match_parent"
                        android:layout_height="10dp"
                        android:background="#E0DFDF"/>

                    <LinearLayout
                        android:id="@+id/ll_linearlayout_b"
                        android:layout_below="@+id/view_a"
                        android:paddingLeft="12dp"
                        android:paddingRight="12dp"
                        android:paddingTop="10dp"
                        android:paddingBottom="12dp"
                        android:orientation="vertical"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content">

                            <TextView
                                android:id="@+id/tv_Record"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="共----条记录"
                                android:textColor="#ff131314"
                                android:textSize="15dp"
                                android:layout_centerVertical="true"
                                />

                            <TextView
                                android:id="@+id/tv_Import"
                                android:layout_width="48dp"
                                android:layout_height="27dp"
                                android:textSize="12sp"
                                android:padding="5dp"
                                android:background="@drawable/bluebutton_click_style"
                                android:text="导出"
                                android:layout_marginLeft="10dp"
                                android:textColor="@color/white"
                                android:gravity="center"
                                android:layout_alignParentRight="true"
                                />


                        </RelativeLayout>

                        <TextView
                            android:id="@+id/tv_Total_sales_volume"
                            android:layout_marginTop="5dp"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="------:------"
                            android:textColor="#ff4d90e7"
                            android:textSize="14sp"
                            />

                        <TextView
                            android:id="@+id/tv_Total_sales"
                            android:layout_marginTop="9dp"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="-------:------"
                            android:textColor="#ff4d90e7"
                            android:textSize="14sp"
                            />

                        <TextView
                            android:id="@+id/tv_Total_Payment"
                            android:layout_marginTop="9dp"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="-------:------"
                            android:textColor="#ff4d90e7"
                            android:textSize="14sp"
                            />

                    </LinearLayout>

                    <View
                        android:id="@+id/view_b"
                        android:layout_below="@+id/ll_linearlayout_b"
                        android:layout_width="match_parent"
                        android:layout_height="10dp"
                        android:background="#E0DFDF"/>

                </RelativeLayout>
            </android.support.design.widget.CollapsingToolbarLayout>
            <!--选项卡-->
            <LinearLayout
                android:id="@+id/ll_LinearLayout_c"
                android:layout_below="@+id/view_b"
                android:orientation="horizontal"
                android:gravity="center"
                android:layout_marginLeft="12dp"
                android:layout_marginRight="12dp"
                android:layout_width="match_parent"
                android:layout_height="39dp">

                <LinearLayout
                    android:id="@+id/ll_Sales_volumes"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:layout_weight="1"
                    android:orientation="horizontal">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="按销售数量"
                        android:textSize="15dp"
                        android:textColor="#6E6E6E"/>

                    <ImageView
                        android:layout_marginLeft="5dp"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/jiantou"
                        />

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/ll_sales_amount"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:layout_weight="1"
                    android:orientation="horizontal">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="按销售金额"
                        android:textSize="15dp"
                        android:textColor="#6E6E6E"/>

                    <ImageView
                        android:layout_marginLeft="5dp"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/jiantou"
                        />

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/ll_Unpaid_amount"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:layout_weight="1"
                    android:orientation="horizontal">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="按未付金额"
                        android:textSize="15dp"
                        android:textColor="#6E6E6E"/>

                    <ImageView
                        android:layout_marginLeft="5dp"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/jiantou"
                        />

                </LinearLayout>

            </LinearLayout>

        </android.support.design.widget.AppBarLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_height="match_parent">
            <View
                android:layout_width="match_parent"
                android:layout_height="0.5dp"
                android:layout_marginLeft="12dp"
                android:layout_marginRight="12dp"
                android:background="#A5A4A4"/>
            <com.huoniao.cq.mymode.widget.SwipeRecyclerView
                android:id="@+id/swipeRecyclerView"
                android:layout_width="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                android:layout_height="match_parent"/>
        </LinearLayout>

    </android.support.design.widget.CoordinatorLayout>

</LinearLayout>

其实界面代码网上都有很多,接下来看一下activity代码:

package com.huoniao.cq.mymode.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;

import com.huoniao.cq.mymode.R;
import com.huoniao.cq.mymode.adapter.DetailsRecyclerViewAdapter;
import com.huoniao.cq.mymode.bean.StationDetailsBean;
import com.huoniao.cq.mymode.widget.SwipeRecyclerView;

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

import butterknife.ButterKnife;
import butterknife.InjectView;

public class MainActivity extends AppCompatActivity {

    @InjectView(R.id.swipeRecyclerView)
    SwipeRecyclerView recyclerView;

    private List<StationDetailsBean> dateList = new ArrayList<>();
    private List<StationDetailsBean> dateList1 = new ArrayList<>();
    private DetailsRecyclerViewAdapter adapter;

    private int pagerSize = 10;
    int totlePage = 5;
    int page = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);
        getTrainPaymentSumDetailadapter(initData() , page);
    }

    private void getTrainPaymentSumDetailadapter(List<StationDetailsBean> dataBeanList , int pageNumber){

        if (pageNumber == 1) {
            settimeAdapter(dataBeanList);
        }else {
            //上拉加载刷新adapter,重新赋值,用set方法去刷新adapter,不然每次上拉加载都会回到第一条数据,设置recyclerView.setFocusable(false);没作用
            dateList.addAll(dataBeanList);
            adapter.notifyDataSetChanged();
        }
    }
    //第一次加载数据走这边
    private void settimeAdapter(List<StationDetailsBean> dataBeanList){
        recyclerView.complete();
        recyclerView.onNoMore(null);
        recyclerView.onLoadingMore();
        recyclerView.getSwipeRefreshLayout().setColorSchemeColors(getResources().getColor(R.color.colorPrimary));
        recyclerView.getRecyclerView().setLayoutManager(new LinearLayoutManager(this));
        dateList.addAll(dataBeanList);
        adapter = new DetailsRecyclerViewAdapter(this , dateList);
        recyclerView.setAdapter(adapter);
        recyclerView.setOnLoadListener(new SwipeRecyclerView.OnLoadListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        dateList.clear();
                        dateList1.clear();
                        page = 1;
                        getTrainPaymentSumDetailadapter(initData() , page);
                        recyclerView.complete();
                        adapter.notifyDataSetChanged();
                    }
                }, 1000);
            }
            @Override
            public void onLoadMore() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (page == totlePage){
                            recyclerView.onNoMore("已经全部加载完毕");
                        }else {
                            dateList1.clear();
                            getTrainPaymentSumDetailadapter(initData() , page);
                            recyclerView.stopLoadingMore();
                            adapter.notifyDataSetChanged();
                            page++;
                        }
                    }
                }, 1000);
            }
        });
        //设置自动下拉刷新,切记要在recyclerView.setOnLoadListener()之后调用
        //因为在没有设置监听接口的情况下,setRefreshing(true),调用不到OnLoadListener
        recyclerView.setRefreshing(false);

    }

    private List<StationDetailsBean> initData(){
        for (int i = 0; i < pagerSize; i++) {
            StationDetailsBean dateBean = new StationDetailsBean();
            dateBean.setId((i+1)+"");
            dateBean.setName("这是第" + (i+1) +"条数据");
            dateList1.add(dateBean);
        }
        return dateList1;
    }

}

整个实现的代码大家可以看这里

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值