Because it is project practice, so it is not a comprehensive study RecyclerView. The writing logic of this article is: first talk about the simplest use of a RecyclerView, and then to achieve the function I want. The original text on CSDN, directly copied from the format seems to be a bit wrong. CSDN address: blog.csdn.net/Wby_Nju/art…
Start a simple RecyclerView
Declare a RecyclerView where you want to RecyclerView
<? The XML version = "1.0" encoding = "utf-8"? > <android.support.v7.widget.CardView <RelativeLayout android:layout_width="match_parent" Android: layout_height = "wrap_content" figure + word < ImageView / > > < TextView / > <. Android support. V7. Widget. RecyclerView android:id="@+id/card_item_folder_RecyclerView" android:layout_marginTop="15dp" android:layout_marginBottom="10dp" android:layout_width="match_parent" android:layout_height="wrap_content" Android: Layout_below ="@+id/card_item_folder_img"/> <LinearLayout> </LinearLayout> </RelativeLayout> </android.support.v7.widget.CardView>Copy the code
Set RecyclerView subitem layout file
<?xml version="1.0" encoding="utf-8"? ><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="wrap_content"><TextViewandroid:id="@+id/card_item_text"android:text="hello"android:textSize="18dp"android:layout_marginLeft="30dp"android:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout>Copy the code
/** * The item in this list has only one title */publicclassCard_Recycler_adapterextendsRecyclerView.Adapter<Card_Recycler_adapter.ViewHolder> {
ArrayList<Card_Recycler_item> items = new ArrayList<>();
publicCard_Recycler_adapter(ArrayList<Card_Recycler_item> items){
this.items = items;
}
@NonNull@Overridepublic ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i){
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_item, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@OverridepublicvoidonBindViewHolder(@NonNull ViewHolder viewHolder, int i){
Card_Recycler_item item = items.get(i);
viewHolder.textView.setText(item.getTitle());
}
@OverridepublicintgetItemCount(a){
return items.size() >= 5 ? 5 : items.size();
}
publicclassViewHolderextendsRecyclerView.ViewHolder{
TextView textView;
publicViewHolder(@NonNull View itemView){
super(itemView); textView = itemView.findViewById(R.id.card_item_text); }}}Copy the code
Use RecyclerView in Fragment/Acytiviy where you want to use RecyclerView
RecyclerView recyclerView = view.findViewById(R.id.card_item_folder_RecyclerView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(),
LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
initItems();
adapter = new Card_Recycler_adapter(items);
recyclerView.setAdapter(adapter);
Copy the code
Long press and drag function
2.1 Style Design
Add a dividing line
recyclerView.setAdapter(adapter);
recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
Copy the code
2.2 Respond to drag and sideslip events
Refer to the article
2.2.1 Simple ItemTouchHelper use
The reference article above, as well as a series of other articles, talk about using ItemTouchHelper. I have no idea what it is. Official API documentation. ☆ A basic ItemTouchHelper example
What is it? It’s a tool class that Google created to help you swipe and drag RecyclerView. That’s what it does! Before I thought he was handling Touch response class, just powerful, by the way here to help. Well, not at all. It’s just a big name.
This is a utility class to add swipe to dismiss and drag & drop support to RecyclerView.
How does it work? withCallbackClass to configure which interactions are allowed, you need a classgetMovementFlagsAccordingly, sliding and dragging require two classes respectivelyonMoveandonSwiped
It works with a RecyclerView and a Callback class, which configures what type of interactions are enabled and also receives events when user performs these actions. . It needs to work with a RecyclerView and Callback class. Callback is used to configure which interactions are allowed, Depending on which functionality you support, you should override onMove(RecyclerView, ViewHolder, ViewHolder) and / or onSwiped(ViewHolder, int). . This class is designed to work with any LayoutManager but for certain situations, it can be optimized for your custom LayoutManager by extending methods in the ItemTouchHelper.Callback class or implementing ItemTouchHelper.ViewDropHandler interface in your LayoutManager.
publicclassCard_Recycler_CallbackextendsItemTouchHelper.Callback{
// Decouple design: Because sliding and removing involve data exchange, abstract out the method and then let RecyclerAdapter implement the abstraction.// I don't really understand how I came up with it, but it works. Where should such ideas be systematically learned?privatefinal ItemTouchHelperAdapter mAdapter;
publicCard_Recycler_Callback(ItemTouchHelperAdapter mAdapter){
this.mAdapter = mAdapter;
}
@OverridepublicintgetMovementFlags(a){... }@OverridepublicbooleanonMove(a){
mAdapter.onItemMove(viewHolder.getAdapterPosition(),
viewHolder1.getAdapterPosition());
}
@OverridepublicvoidonSwiped(a){... }@OverridepublicbooleanisLongPressDragEnabled(a){
returntrue;
}
@OverridepublicbooleanisItemViewSwipeEnabled(a){
returntrue; }}Copy the code
publicclassCard_Recycler_adapterextendsRecyclerView.Adapter<Card_Recycler_adapter.ViewHolder>
implementsItemTouchHelperAdapter{
Copy the code
publicinterfaceItemTouchHelperAdapter{
voidonItemMove(int fromPosition, int toPosition);
// This is the corresponding slide method, you want to slide open write open method, you want to slide delete write delete method.voidonItemDismiss(int position);
}
Copy the code
Finally, use it in activities/fragments
// This adapter is RecyclerAdapter
ItemTouchHelper.Callback callback = new Card_Recycler_Callback(adapter);
ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
touchHelper.attachToRecyclerView(recyclerView);
Copy the code
2.2.2 Practice Feedback – Effect of long press
It would be nice if the long press had raised behavior to indicate that the user is ready to drag.
It’s good to be able to cover other areas when you drag them, without overlap.
Reference. – Look at his renderings
In the callback
// Set the slide item background@OverridepublicvoidonSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState){
// Check the selected statusif(actionState ! = ItemTouchHelper.ACTION_STATE_IDLE) { viewHolder.itemView.setBackgroundColor (viewHolder.itemView.getContext().getResources().getColor(R.color.grey_300)); }super.onSelectedChanged(viewHolder, actionState);
}
// Clears the slide item background@OverridepublicvoidclearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder){
/ / recovery
viewHolder.itemView.setBackgroundColor(Color.WHITE);
super.clearView(recyclerView, viewHolder);
}
Copy the code