Introduction to the
Some time ago, a recyclerView-related library was packaged, called SXRecyclerView. The library includes many common features, including touch feedback effects, click events, long press events, multiple selection, HeaderAndFooter, PinnedHeader, and more. Using such a component directly during application development can quickly realize related functions and improve development efficiency. The realization of SXRecyclerView is not directly introduce the original RecyclerView, but extract the source code of the whole RecyclerView library and package it on the basis of the original source code. One of the advantages of this is to break the limit of authority, facilitate the encapsulation and customization of functions; Second, it can completely control the entire library to avoid compatibility problems caused by importing different versions. SXRecyclerView needs to rely on support-V4 package. If V4 package is also introduced in the project of importing SXRecyclerView, it is better to ensure that v4 package 25.3.1 or later is introduced, otherwise compilation errors may occur. I am now sharing this component library with you as an open source address: github.com/zhimaochen/…
Touch feedback effect
SXRecyclerView has achieved the touch feedback effect, the default click, long press operation will have water ripple animation. If you want to customize the touch feedback effect, you can set it in the following way:
public void setSelector(Drawable sel)
public void setSelector(int resID)
Copy the code
If you want to remove the touch feedback, through mRecyclerView. SetSelector (null); Set it like this.
Click events and long press events
The native RecyclerView doesn’t provide ItemView click-and-hold functionality, It is common practice to set OnClickListener and OnLongClickListener for each ViewHolder view during data binding in Adapter. There is no need for such trouble now. SXRecyclerView this class in RecyclerView based on the function, and provides the event callback, call code is as follows:
/ / click event mRecyclerView. SetOnItemClickListener (new SXRecyclerView.OnItemClickListener() {
@Override
public void onItemClick(RecyclerView parent, View view, int position, long id) {
//dosomething } }); / / long press event mRecyclerView. SetOnItemLongClickListener (new SXRecyclerView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(RecyclerView parent, View view, int position, long id){
//do something
return true; }});Copy the code
By default, after setting these two listeners, SXRecyclerView responds to each ItemView. You can override the isEnable(int Position) method of the Adapter to make an ItemView unresponsive to clicking and holding events:
/** * TYPE_TITLE Item does not need to respond to click and hold events, so returnsfalse
* @param position
* @return
*/
@Override
public boolean isEnable(int position) {
if (getItemViewType(position) == TYPE_TITLE){
return false;
}
return true;
}
Copy the code
Native RecyclerView does not have the method, this method is SXRecyclerView added. This method returns false to indicate that the corresponding location of the ItemView does not enable SXRecyclerView related functions, that is, does not respond to click events, long press events, touch feedback, multi-select functions, etc. This method returns true by default. Schematic diagram:
click
Multiple functions
To use the multi-select function, there are two necessary steps: 1. Set the multi-select mode, which is as follows:
- CHOICE_MODE_NONE: disables the multiple selection function. The default value is this.
- CHOICE_MODE_MULTIPLE: Actively triggers the multiple selection function. Trigger mode has long press and active call
startMultiChoice()
Method There are two trigger modes.
2. Set the selection mode listener, and implement relevant methods to interact with ActionMode according to requirements. The code is as follows:
// Set the selection mode to multi-selection mode
mRecyclerView.setChoiceMode(SXRecyclerView.CHOICE_MODE_MULTIPLE);
// Set the multi-choice mode listener
mRecyclerView.setMultiChoiceModeListener(new SXRecyclerView.MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {}@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return true;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {}});Copy the code
The multi-choice mode here is binding and interacting with ActionMode. For specific implementation, please refer to the demo. By default, all ItemViews can be selected. If you want to make items unselectable, override the isEnable(int Position) method in Adaper to invalidate the ItemView at the corresponding position. This can also be set through another Adapter method:
/ / returnfalse@Override public Boolean isSelectable(int position) {if (position % 5 == 0) {
return false;
}
return true;
}
Copy the code
Unlike the isEnable(int Position) method, the isSelectable(int Position) method only applies to multi-select functions. Itemviews that return false cannot be selected. Other functions, including click and hold events, can respond. When you select an ItemView, you mark it with a check mark. To make the Checkable operation easier and more straightforward, SXRecyclerView provides a way to implement the Checkable interface to the ViewHolder so that the setChecked(Boolean Checked) method is called whenever the Checkable state changes. We can do whatever we want with the ItemView in this method.
// Implement the Checkable interface, Class MyViewHolder extends RecyclerView.ViewHolder implements Checkable {TextView mTextView; ImageView mImageView; public MyViewHolder(View itemView) { super(itemView); mTextView = (TextView) itemView.findViewById(R.id.item_tv); mImageView = (ImageView) itemView.findViewById(R.id.img_check); } // This method calls @override public void when the ItemView checked state changessetChecked(boolean checked) {
if (checked) {
mImageView.setVisibility(View.VISIBLE);
} else {
mImageView.setVisibility(View.GONE);
}
}
@Override
public boolean isChecked() {
return false;
}
@Override
public void toggle() {}}Copy the code
** Tip: ** Multiple selection function related methods are as follows:
public boolean startMultiChoice()
: Enters the multiple selection statepublic void finishMultiChoice()
: Exits the multiple selection statepublic Boolean isInMutiChoiceState()
: Checks whether SXRecyclerview is in multi-select statepublic void setItemChecked(int position, boolean value)
: Sets the selected status of an itempublic void clearChoices()
: Clears all selected statespublic long[] getCheckedItemIds()
: Gets the collection of ids for the currently selected itempublic List<integer style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box ! important; word-wrap: break-word ! important; font-size: inherit; color: inherit; line-height: inherit;" >getCheckedItemPositions()</integer>
: Gets the collection of positions for the currently selected itempublic boolean isItemChecked(int position)
: Checks whether an item is selectedpublic int getCheckedItemCount()
: Gets the total number of selected itemspublic void checkedAll()
: allpublic void unCheckedAll()
: don’t choose
Schematic diagram:
multichoice
PinnedHeader
PinnedHeader refers to grouping ViewHolder, each group has a Header, and during the slide, The Header of the top group will be suspended at the top of SXRecyclerView and will not slide out of the screen until the next Header slides to the top of SXRecyclerView and the last Header will be pushed out of the screen. There are two necessary steps to implement the PinnedHeader effect: 1. The following interface needs to be implemented in its Adapter
Public interface RecyclerPinnedHeaderAdapter < VH extends MzRecyclerView. ViewHolder > {/ / return PinnedHeader number id / / Group viewholders by ID. Viewholders with the same ID are grouped into the same group, corresponding to the same PinnedHeader. The PinnedHeader does not display long getHeaderId(int Position). VH onCreateHeaderViewHolder(ViewGroup parent); Void onBindHeaderViewHolder(VH holder, int position); int getItemCount(); }Copy the code
2, in order to realize the RecyclerPinnedHeaderAdapter Adapter object as a parameter, create RecyclerPinnedHeaderDecoration object, Add it to SXRecyclerView by addItemDecoration, and now you can display the PinnedHeader.
/ / create RecyclerPinnedHeaderDecoration RecyclerPinnedHeaderDecoration headersDecor = new RecyclerPinnedHeaderDecoration(adapter); / / will create RecyclerPinnedHeaderDecoration as a ItemDecoration added to Recyclerview, So as to realize the PinnedHeader drawn into SXRecyclerview mRecyclerView. AddItemDecoration (headersDecor); < / pre > RecyclerPinnedHeaderDecoration provides PinnedHeader listener, suspended at the top of the PinnedHeader change (the top out of the screen or the screen has a new PinnedHeader in) is triggered when the listener callback, This listener can be used to add some identification or animation to the floating head. <pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box ! important; word-wrap: break-word ! important; color: inherit; font-size: inherit; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); line-height: inherit;"> / / can be set by RecyclerPinnedHeaderDecoration listener, To implement the switching PinnedHeader listening headersDecor. SetPinnedHeaderListener (new RecyclerPinnedHeaderDecoration.OnPinnedHeaderChangeListener() {
@Override
public void OnPinnedHeaderChange(RecyclerView recyclerView, View currentHeader, int currentPosition, long currentHeaderId, View lastHeader, int lastPosition, long lastHeaderId) {
//do something
}
});
Copy the code
In addition, SXRecyclerview supports setting click events to PinnedHeaders. Example code is as follows:
/ / can be set by RecyclerPinnedHeaderTouchListener click PinnedHeader a callback RecyclerPinnedHeaderTouchListener touchListener = new RecyclerPinnedHeaderTouchListener(mRecyclerView, headersDecor); touchListener.setOnHeaderClickListener(new RecyclerPinnedHeaderTouchListener.OnHeaderClickListener() {
@Override
public void onHeaderClick(View header, int position, long headerId, MotionEvent e) {
// dosomething } }); / / remember to add RecyclerPinnedHeaderTouchListener SXRecyclerview will only take effect in mRecyclerView. AddOnItemTouchListener (touchListener);Copy the code
Schematic diagram:
pinnedheader
The Header and Footer
SXRecyclerView provides the function of adding headers and tails. Example code is as follows:
View headerView = LayOutInflater.from (this).inflate(r.layout.recyclerview_header_view, mRecyclerView,null); Viewholder final HeaderViewHolder Header = new HeaderViewHolder(headerView); header.mTextView.setText("This is Header View");
header.mTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// dosomething } }); / / this is packaged headerview is added to the SXRecyclerview mRecyclerView. AddHeaderView (header); View footerView = LayoutInflater.from(this).inflate(R.layout.recyclerview_footer_view, null); final FooterViewHolder footer = new FooterViewHolder(footerView); footer.mTextView.setText("This is Footer View");
footer.mTextView.setBackgroundColor(0xFF6495ED);
footer.mTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// dosomething } }); / / add this was packaged footerview into SXRecyclerview mRecyclerView. AddFooterView (footer);Copy the code
HeaderAndFooter
HeaderAndFooterGrid
HeaderAndFooterStag I will keep updating and maintaining this library, and gradually add more general functions in the future. After updating, I will synchronize documents to this article and GitHub. What problems can be exchanged at any time during the process of use ~ Android learning PDF+ architecture video + interview document + source notes