当前位置:网站首页>Usage of swipemenurecyclerview
Usage of swipemenurecyclerview
2022-07-24 12:51:00 【Yaya pear】
In the recent project, we need a nearly omnipotent recyclerview, You can either pull-down refresh or pull-up load , With special effects , And every item Slide left and right , Delete or edit . So I searched a tool written by a cow on the Internet . Intended to be used in the project . The key category he gave was SwipeMenuRecyclerView. But there are also many interfaces . Time is short , It's impossible to understand all at once . Now the most important thing is to figure out how to use ! First pick some functions to be used next week !
1 Use of pull-up refresh pull-down loading
Pull up to refresh the dynamic effect of pull-down loading
Pull up refresh pull-down load trigger callback
2 The interface setting mode of sliding left and right
The action of sliding left and right triggers the callback code
3 item Up and down position change function
4 Add headerView How is it added ?
Consider these points first . I see the author's description , It seems that this custom view It can realize these functions . Please check the website for details :RecyclerView Sideslip menu , Slide delete , Long press drag , Pull down refresh pull up load .
So let's start a personal test , Explore its function . Start with the simplest . First use him to express the simplest style :
First of all, I wrote the simplest function , It's a show :
Because I just learned ConstrainsLayout, So whether I am activity The outermost layout is still item The layout adopts this constrainsLayout, If you find yourself unable to make item The width fills the whole listview Words , Please be there. adapter In the initialization view When you use view = mLayoutInflater.inflate(R.layout.XXXX, parent, false); The third parameter is false!
Pull up load pull down refresh , And the dynamic picture settings involved .
Pull on loading :
// Set the pull-up loading function
// Use the self-contained about loading more view.
recyclerView.useDefaultLoadMore();
// Set this line , When sliding to the bottom , More loading interfaces will appear .
recyclerView.loadMoreFinish(false, true);
// Set the callback when loading more .
recyclerView.setLoadMoreListener(new SwipeMenuRecyclerView.LoadMoreListener() {
@Override
public void onLoadMore() {
// Add data
adapter.addDatalist(makeData(0));
// notice adapter
adapter.notifyDataSetChanged();
}
});
Use the default footer style , Pull down loading more implementations .
In the code above , There is a very important way to decide whether to continue loading , That's it :loadMoreFinish() This method . This method is very important , Determines that the default bottom load is more View How to show it
public final void loadMoreFinish(boolean dataEmpty, boolean hasMore).
If dataEmpty = true Words ,list Bottom footer loading Go straight away . Because there is no data, there is no need to appear .
meanwhile dataEmpty And hasMore The synthesis determines whether to call the... In the listener onLoadMore() Method . So when writing, you should pay attention to , These conditions .
Use custom footer style , Drop down to load more implementations .
The above code uses the default style to indicate that the pull-down loads more . What if we need to customize ourselves ?gitHub The example given above is actually problematic . I have to grope for it myself .
First turn on the useDefaultLoadMore() Take out this line . We don't have to default . Next, we set up our own view. First, write and implement SwipeMenuRecyclerView.LoadMoreView The footer of layout.
public class DefinLoadingmoreView extends LinearLayout implements SwipeMenuRecyclerView.LoadMoreView,
View.OnClickListener {
public DefinLoadingmoreView(Context context) {
super(context);
initView();
}
public DefinLoadingmoreView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initView();
}
public DefinLoadingmoreView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
private void initView () {
View view = inflate(getContext(), R.layout.loading_more_item, null);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 300);
setLayoutParams(params);
view.setLayoutParams(params);
addView(view);
setBackgroundColor(Color.parseColor("#ff0000"));
setOnClickListener(this);
}
@Override
public void onLoading() {
Log.d(TAG, "onLoading....");
setVisibility(VISIBLE);
}
@Override
public void onLoadFinish(boolean dataEmpty, boolean hasMore) {
Log.d(TAG, "onLoadFinish....");
if (dataEmpty || !hasMore) {
setVisibility(VISIBLE);
} else {
setVisibility(VISIBLE);
}
}
@Override
public void onWaitToLoadMore(SwipeMenuRecyclerView.LoadMoreListener loadMoreListener) {
Log.d(TAG, "onWaitToLoadMore....");
setVisibility(VISIBLE);
}
@Override
public void onLoadError(int errorCode, String errorMessage) {
Log.d(TAG, "onLoadError....");
setVisibility(GONE);
}
@Override
public void onClick(View v) {
Log.d(TAG, "onClick....");
}
}
Then there was this View, Let's use it as a footer . to recyclerView Set up the following sentences :
DefinLoadingmoreView loadingmoreView = new DefinLoadingmoreView(this);
recyclerView.setLoadMoreView(loadingmoreView); // To put it bluntly, this is just setting up monitoring . It is intended to set the callback interface
recyclerView.addFooterView(loadingmoreView);//
In this case , The footer can show what we have written view 了 .but, It's not over yet ! There are many other things that need our attention. First , The interface implemented by the footer class , That is to say :SwipeMenuRecyclerView.LoadMoreView. There are several methods in it :
public interface LoadMoreView {
void onLoading();
void onLoadFinish(boolean dataEmpty, boolean hasMore);
void onWaitToLoadMore(LoadMoreListener loadMoreListener);
void onLoadError(int errorCode, String errorMessage);
}
Pay special attention to , When were they triggered . Because when we implement these methods , All we have to do is View Changes on . For example, change a copy ,show One loading ah , Let him show or direct gone. There can be no other logic mixed in .
By looking at the code , Analyze it :
onLoading(), We really give swipeEmunRecyclerView The one stuffed setLoadMoreListener() Of the monitor onLoadMore() Method called before callback . It is intended to represent the style being loaded . You can at this time , Set the copy to loading !
onLoadFinish() We call SwipeEnumRecyclerView Of loadMoreFinish() Method called indirectly . You can decide the copy according to the parameters in it , There is no data , Or maybe there will be data next , direct GONE fall .
onWaitToLoadMore(), This depends on whether the automatic loading mode , To decide whether to use . Anyway, I don't use this method . Don't explain !
onLoadError() , This is also through external calls loadMoreError() Method is called indirectly . We can change the copy in this case , namely Tips like loading exceptions .
The drop-down refresh
Item Click event
Ah , Speechless, even if you set the click event, you should pay attention to !
recyclerView.setSwipeItemClickListener(new SwipeItemClickListener() {
@Override
public void onItemClick(View itemView, int position) {
Log.d(TAG, " Click on an item ");
}
});
recyclerView.setAdapter(adapter);
Be careful , This click event must be set at setAdapter Go ahead of . Otherwise, report errors . Ah !
Left slide optimization settings
Ah , When I finish this today , Sliding left and sliding right is , But I couldn't bear the old collapse and was speechless . After thousands of difficulties, I finally found the imported package , Want to use support v4 What's inside , But the current project does not . but , Just follow the online saying , stay main java A new folder named android.support.v4.animation The package name , Then I found SDK The package under the directory is underground , hold animation Only when the class of is copied in can it be solved . Ah . It's almost a little . To get it done . It's annoying .
The key code of sliding left and right : Must be written in setAdapter Before
recyclerView.setItemViewSwipeEnabled(true);
边栏推荐
- 让一套代码完美适配各种屏幕
- Snowflake algorithm (PHP)
- Compatibility problems of call, apply, bind and bind
- Nacos deployment
- Unity rotation test
- Industry insight | how to better build a data center? It and business should "go together"
- Is it safe to open an account on Oriental Fortune online? Is there a threshold for opening an account?
- Say no to blackmail virus, it's time to reshape data protection strategy
- [stm32] internal independent watchdog iwdg
- The setting float cannot float above the previous Div
猜你喜欢

Nacos deployment

Vscode solves the problem of terminal Chinese garbled code

Cluster construction based on kubernetes v1.24.0 (II)

Nearly 65billion pieces of personal information were illegally handled in seven years, and the investigation of didi network security review case was announced
Efficientformer: lightweight vit backbone
This is how the permission system is designed, yyds

29. Right view of binary tree

cookie

Okaleido tiger NFT即将登录Binance NFT平台

Getting started with SQL join use examples to learn left connection, inner connection and self connection
随机推荐
It is difficult for Chinese consumers and industrial chains to leave apple, and iPhone has too much influence
高速成长的背后,华为云乌兰察布数据中心的绿色之道
权限系统就该这么设计,yyds
Custom scroll bar
Use abp Zero builds a third-party login module (4): wechat applet development
深圳地铁12号线第二批工程验收通过 预计7月28日试运行
The basis of point graph in the map of life information and knowledge
Taishan Office Technology Lecture: layout difficulties of paragraph borders
元宇宙更多的功能和作用在于对于传统生活方式和生产方式的深度改造
SSM online rental and sales platform multi city version
Snowflake algorithm (PHP)
thinkphp 实现数据库备份
QWaitCondition 的正确使用方法
Shell script case ---2
Slow motion animation, window related data and operations, BOM operations [DOM (V)]
Node takes effect after using NVM to install under Windows system, but NPM does not take effect
26. Reverse linked list II
31. Climb stairs
Prototype inheritance
基于matlab的语音处理