Android smart refresh framework smartrefreshlayout

Time:2021-8-28

Android smart refresh framework smartrefreshlayout

Smartrefreshlayout is a “smart” or “smart” drop-down refresh layout. Because of its “intelligence”, it supports not only all views, but also multi-layer nested view structures. It inherits from ViewGroup instead of FrameLayout or LinearLayout, which improves performance.
Add dependency
Implementation ‘com. Scwang. Smart: refresh layout kernel: 2.0.3’ / / the core must depend on
Implementation ‘com. Scwang. Smart: refresh header Classics: 2.0.3’ / / classic refresh header
Implementation ‘com. Scwang. Smart: refresh footer Classics: 2.0.3’ / / classic loading
1: Basic use
1. Layout documents

    <com.scwang.smart.refresh.layout.SmartRefreshLayout
        android:id="@+id/refreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#444444"
        app:srlAccentColor="@android:color/white"
        app:srlEnablePreviewInEditMode="true"
        app:srlPrimaryColor="#444444">
        <com.scwang.smart.refresh.header.ClassicsHeader
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycler"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#fff"
            android:overScrollMode="never"/>
        <com.scwang.smart.refresh.footer.ClassicsFooter
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </com.scwang.smart.refresh.layout.SmartRefreshLayout>

code:

public class ThirdActivity extends AppCompatActivity {
    private SmartRefreshLayout smartRefreshLayout;
    private RecyclerView recyclerView;
    private MyAdatpter myAdatpter;
    private List<String> datas=new ArrayList<>();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);
        initData();
        smartRefreshLayout= findViewById(R.id.refreshLayout);
        //smartRefreshLayout.autoLoadMore();// Automatic loading
        //smartRefreshLayout.autoRefresh();// auto refresh
        smartRefreshLayout.setEnableAutoLoadMore(true);// Whether to enable the list to automatically load more when sliding to the bottom
        smartRefreshLayout.setEnableLoadMore(true);// Enable load more features
        smartRefreshLayout.setEnableRefresh(true);// Whether to enable the pull-down refresh function
        recyclerView=findViewById(R.id.recycler);
        LinearLayoutManager layoutManager=new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(layoutManager);
        myAdatpter=new MyAdatpter(this,datas);
        recyclerView.setAdapter(myAdatpter);
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                Toast. Maketext (thirdactivity. This, "I request the drop-down to refresh data", toast. Length_ LONG).show();

                refreshLayout.finishRefresh();// Pull down refresh end
                //refreshLayout.finishRefresh(2000);// 2S end

            }
        });
        smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                Toast. Maketext (thirdactivity. This, "I request to load more data", toast. Length_ SHORT).show();
                refreshLayout.finishLoadMore();
            }
        });
    }

    private void initData() {
        for (int i=0;i<40;i++){
            Data. Add ("I am test data" + I);
        }
    }
}

The myadapter inside is the data adapter of recyclerview to adapt the test data

public class MyAdatpter extends RecyclerView.Adapter<MyAdatpter.MyViewHolder> {
    private Context context;
    private List<String> mDatas;
    private View view;
    //Construction method, incoming data
    public MyAdatpter(Context context,List<String> mDatas) {
        this.context=context;
        this.mDatas=mDatas;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //Create a viewholder, return the layout of each item, and convert the view layout into a viewholder
        view=LayoutInflater.from(context).inflate(R.layout.item_first,parent,false);

        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        //Binding layout controls
        holder.item.setText(mDatas.get(position));

    }

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

    static class MyViewHolder extends RecyclerView.ViewHolder{
        TextView item;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            item=itemView.findViewById(R.id.item_tv);
        }
    }
}

Basic use can realize pull-down refresh and pull-down load more functions

2: Some settings for pull-down refresh and pull-up load

//The value in the following example is equal to the default value

ClassicsHeader header = (ClassicsHeader)findViewById(R.id.header);
    header.setAccentColor(android.R.color.white);// Set accent color
    header.setPrimaryColor(R.color.colorPrimary);// Set theme color
    header.setTextSizeTitle(16);// Set title text size (SP units)
    header.setTextSizeTitle(16, TypedValue.COMPLEX_ UNIT_ SP);// Ditto (deleted in version 1.1.0)
    header.setTextSizeTime(10);// Set time text size (SP units)
    header.setTextSizeTime(10, TypedValue.COMPLEX_ UNIT_ SP);// Ditto (deleted in version 1.1.0)
    header.setTextTimeMarginTop(10);// Sets the top margin of the time text (DP units)
    header.setTextTimeMarginTopPx(10);// Ditto - pixel units (deleted in version 1.1.0)
    header.setEnableLastTime(true);// Show time
    header.setFinishDuration(500);// Set the dwell time of the refresh completion display (set to 0 to turn off the dwell function)
    header.setDrawableSize(20);// Set the size of the arrow and the picture at the same time (DP units)
    header.setDrawableArrowSize(20);// Sets the size of the arrow in DP units
    header.setDrawableProgressSize(20);// Sets the size of the picture in DP units
    header.setDrawableMarginRight(20);// Sets the spacing (DP units) between pictures and arrows and text
    header.setDrawableSizePx(20);// Ditto - pixel units
    header.setDrawableArrowSizePx(20);// Ditto - pixel units (deleted in version 1.1.0)
    header.setDrawableProgressSizePx(20);// Ditto - pixel units (deleted in version 1.1.0)
    header.setDrawableMarginRightPx(20);// Ditto - pixel units (deleted in version 1.1.0)
    header.setArrowBitmap(bitmap);// Set arrow bitmap (deleted in version 1.1.0)
    header.setArrowDrawable(drawable);// Set arrow picture
    header.setArrowResource(R.drawable.ic_ arrow);// Set arrow resources
    header.setProgressBitmap(bitmap);// Set picture bitmap (deleted in version 1.1.0)
    header.setProgressDrawable(drawable);// Set picture
    header.setProgressResource(R.drawable.ic_ progress);// Set up picture resources
    Header.settimeformat (New dynamictimeformat ("last updated% s")// Set time format (time will be updated automatically)
    Header.setlastupdatetext ("3 seconds before the last update")// Manually update the time text setting (the time will not be updated automatically)
    header.setSpinnerStyle(SpinnerStyle.Translate);// Set move style (not supported: matchlayout)

It can be implemented in code, and you can also set attributes in XML layout

<com.scwang.smart.refresh.header.ClassicsHeader
            android:id="@+id/header"
            App: srltextsizetitle = "10dp" // set the text size of the title
            App: srltextsizetime = "10dp" // set the text size of the time
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

Load more, the same is true
3: Smart smartrefreshlayout can set more styles of smart loading headers

SmartRefreshLayout  smartRefreshLayout= findViewById(R.id.refreshLayout);// Get the layout

//Set header as balloon express style 
smartRefreshLayout.setRefreshHeader(DeliveryHeader(this))
//Set header to drop box style
smartRefreshLayout.setRefreshHeader(DropBoxHeader(this))

//Official style
smartRefreshLayout.setRefreshHeader(MaterialHeader(this))
//Apple drop style
smartRefreshLayout.setRefreshHeader(WaterDropHeader(this))

//Classic style 
smartRefreshLayout.setRefreshHeader(ClassicsHeader(this))
//Pop up text style
smartRefreshLayout.setRefreshHeader(StoreHouseHeader(this))

//Pop up circle style
smartRefreshLayout.setRefreshHeader(BezierCircleHeader(this))
//Bezier radar style, setenablehorizontaldrag setting enables horizontal drag
smartRefreshLayout.setRefreshHeader(BezierRadarHeader(this))
//Pop up the water drop style. The water drop needs half the screen height. Use it with caution
smartRefreshLayout.setRefreshHeader(WaveSwipeHeader(this))

//Taobao second floor style
smartRefreshLayout.setRefreshHeader(TwoLevelHeader(this))

//Tank game style
smartRefreshLayout.setRefreshHeader(FunGameBattleCityHeader(this))
//Brick game style
smartRefreshLayout.setRefreshHeader(FunGameHitBlockHeader(this))

//Golden campus style
smartRefreshLayout.setRefreshHeader(PhoenixHeader(this))
//Skyrocketing style
smartRefreshLayout.setRefreshHeader(TaurusHeader(this))

Other methods

RefreshLayout refreshLayout = (RefreshLayout)findViewById(R.id.refreshLayout);
        refreshLayout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);
        refreshLayout.setDragRate(0.5f);// Show drop-down height / finger real drop-down height = damping effect
        refreshLayout.setReboundDuration(300);// Rebound animation duration (MS)

        refreshLayout.setHeaderHeight(100);// Header standard height (display drop-down height > = standard height triggers refresh)
        refreshLayout.setHeaderHeightPx(100);// Ditto - in pixels (V1.1.0 deleted)
        refreshLayout.setFooterHeight(100);// Footer standard height (display pull-up height > = standard height trigger loading)
        refreshLayout.setFooterHeightPx(100);// Ditto - in pixels (V1.1.0 deleted)

        refreshLayout.setFooterHeaderInsetStart(0);// Set the header start position offset of 1.0.5
        refreshLayout.setFooterHeaderInsetStartPx(0);// Ditto - pixels in 1.0.5 (V1.1.0 deleted)
        refreshLayout.setFooterFooterInsetStart(0);// Set the offset of footer start position by 1.0.5
        refreshLayout.setFooterFooterInsetStartPx(0);// Ditto - pixels in 1.0.5 (V1.1.0 deleted)

        refreshLayout.setHeaderMaxDragRate(2);// Maximum display drop-down height / header standard height
        refreshLayout.setFooterMaxDragRate(2);// Maximum display drop-down height / footer standard height
        refreshLayout.setHeaderTriggerRate(1);// The ratio of trigger refresh distance to headerheight is 1.0.4
        refreshLayout.setFooterTriggerRate(1);// The ratio of trigger loading distance to footerheight is 1.0.4

        refreshLayout.setEnableRefresh(true);// Whether to enable the pull-down refresh function
        refreshLayout.setEnableLoadMore(false);// Enable pull-up loading function
        refreshLayout.setEnableAutoLoadMore(true);// Whether to enable the list to automatically load more when sliding to the bottom
        refreshLayout.setEnablePureScrollMode(false);// Enable scroll only mode
        refreshLayout.setEnableNestedScroll(false);// Enable nested scrolling
        refreshLayout.setEnableOverScrollBounce(true);// Enable out of bounds springback
        refreshLayout.setEnableScrollContentWhenLoaded(true);// Whether to scroll the list to display new content when loading is complete
        refreshLayout.setEnableHeaderTranslationContent(true);// Whether to pan down the list or content when pulling down the header
        refreshLayout.setEnableFooterTranslationContent(true);// Whether to move the list or content upward when pulling up the footer
        refreshLayout.setEnableLoadMoreWhenContentNotFull(true);// Whether to enable the pull-up loading function when the list is less than one page
        refreshLayout.setEnableFooterFollowWhenLoadFinished(false);// Whether footer follows content 1.0.4 after all loading is completed
        refreshLayout.setEnableOverScrollDrag(false);// Whether to enable cross-border drag (imitation Apple effect) 1.0.4

        refreshLayout.setEnableScrollContentWhenRefreshed(true);// Whether to scroll the list to display new content 1.0.5 when the refresh is completed
        refreshLayout.srlEnableClipHeaderWhenFixedBehind(true);// Whether to clip header 1.0.5 when the style is fixedbehind
        refreshLayout.srlEnableClipFooterWhenFixedBehind(true);// Whether to clip 1.0.5 when the footer style is fixedbehind

        refreshLayout.setDisableContentWhenRefresh(false);// Whether to prohibit list operation during refresh
        refreshLayout.setDisableContentWhenLoading(false);// Whether to prohibit list operation when loading

        refreshLayout.setOnMultiPurposeListener(new SimpleMultiPurposeListener());// Setting up a multi-function listener
        refreshLayout.setScrollBoundaryDecider(new ScrollBoundaryDecider());// Set rolling boundary judgment
        refreshLayout.setScrollBoundaryDecider(new ScrollBoundaryDeciderAdapter());// Custom scroll boundary

        refreshLayout.setRefreshHeader(new ClassicsHeader(context));// Set header
        refreshLayout.setRefreshFooter(new ClassicsFooter(context));// Set footer
        refreshLayout.setRefreshContent(new View(context));// Set refresh content (for non XML layout instead of addview) 1.0.4

        refreshLayout.autoRefresh();// auto refresh
        refreshLayout.autoLoadMore();// Automatic loading
        refreshLayout.autoRefreshAnimationOnly();// Auto refresh, only the animation is displayed, and no refresh is performed
        refreshLayout.autoLoadMoreAnimationOnly();// Automatic loading, only animation is displayed, and loading is not performed
        refreshLayout.autoRefresh(400);// Automatic refresh after 400 ms delay
        refreshLayout.autoLoadMore(400);// Automatic loading after 400 ms delay
        refreshLayout.finishRefresh();// End refresh
        refreshLayout.finishLoadMore();// End loading
        refreshLayout.finishRefresh(3000);// End refresh after 3000 ms delay
        refreshLayout.finishLoadMore(3000);// End loading after 3000 ms delay
        refreshLayout.finishRefresh(false);// End refresh (refresh failed)
        refreshLayout.finishLoadMore(false);// End load (load failed)
        refreshLayout.finishLoadMoreWithNoMoreData();// Complete loading and mark no more data 1.0.4
        refreshLayout.closeHeaderOrFooter();// Close the open header or footer (1.1.0)
        refreshLayout.resetNoMoreData();// Restore the original state without more data 1.0.4 (1.1.0 delete)
        refreshLayout.setNoMoreData(false);// Restore original state without more data 1.0.5

The third-party framework needs us to study its implementation logic, but it can’t meet the requirements

End: how can I climb to the top of Android knowledge