@[toc]
preface
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"? >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android: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
- Create RecyclerView item Class
public class Card_Recycler_item {
String title;
public Card_Recycler_item(String title) {
this.title = title;
}
public String getTitle(a) {returntitle; }public void setTitle(String title) { this.title = title;}
}
Copy the code
- Create an adapter for RecyclerView
/** * The item in this list has only one title */
public class Card_Recycler_adapter extends RecyclerView.Adapter<Card_Recycler_adapter.ViewHolder> {
ArrayList<Card_Recycler_item> items = new ArrayList<>();
public Card_Recycler_adapter(ArrayList<Card_Recycler_item> items) {
this.items = items;
}
@NonNull
@Override
public 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;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
Card_Recycler_item item = items.get(i);
viewHolder.textView.setText(item.getTitle());
}
@Override
public int getItemCount(a) {
return items.size() >= 5 ? 5 : items.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView textView;
public ViewHolder(@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? with
Callback
Class to configure which interactions are allowed, you need a classgetMovementFlags
Accordingly, sliding and dragging require two classes respectivelyonMove
andonSwiped
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.
public class Card_Recycler_Callback extends ItemTouchHelper.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?
private final ItemTouchHelperAdapter mAdapter;
public Card_Recycler_Callback(ItemTouchHelperAdapter mAdapter) {
this.mAdapter = mAdapter;
}
@Override
public int getMovementFlags(a){... }@Override
public boolean onMove(a){
mAdapter.onItemMove(viewHolder.getAdapterPosition(),
viewHolder1.getAdapterPosition());
}
@Override
public void onSwiped(a){... }@Override
public boolean isLongPressDragEnabled(a) {
return true;
}
@Override
public boolean isItemViewSwipeEnabled(a) {
return true; }}Copy the code
public class Card_Recycler_adapter extends RecyclerView.Adapter<Card_Recycler_adapter.ViewHolder>
implements ItemTouchHelperAdapter {
Copy the code
public interface ItemTouchHelperAdapter {
void onItemMove(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.
void onItemDismiss(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
@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
// Check the selected status
if(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
@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
/ / recovery
viewHolder.itemView.setBackgroundColor(Color.WHITE);
super.clearView(recyclerView, viewHolder);
}
Copy the code