** This site links some posts in Qiita. If you find it unpleasant, please comment. We will correspond. ** **
RecyclerView
is very convenient. I'm using it instead of ListView
, but I'll forget how to implement it and how to implement swipe, so I'll make a note here. The swipe animation mimics the Gmail
email delete swipe.
This implementation is the same as "Basics of RecyclerView" by @ naoi. It is explained in a very easy-to-understand manner. The mechanism of RecyclerView
is a little complicated, so it's a good idea to remember the figure.
Here, we will produce the following four.
adapter_row.xml
DataModel.java
ViewHolder.java
Adapter.java
** This time, let's put the data as an image and a character string. ** **
Determine the layout of a row of RecyclerView
.
adapter_row
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView_adapter_show_bitmap"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textView_adapter_show_string"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
There is no structure in Java, but you can substitute Class
.
DataModel.java
public class DataModel {
private Bitmap mBitmap;
private String mString;
public Bitmap getBitmap () {
return mBitmap;
}
public String getString () {
return mString;
}
public void setBitmap (Bitmap mBitmap) {
this.mBitmap = mBitmap;
}
public void setString (String mString) {
this.mString = mString;
}
}
Isn't it common to add m
to member variables?
As an aside, "get ~" and "set ~" are said to be `getter / setter```, but in ```Android Studio
, if you hit public
, the member variable It seems to make `getter / setter
. I'm grateful that if I change the name a little, it's complete.
Create a ViewHolder
. Here, xml
body is not specified, but View
in one line is findViewByID ()
.
ViewHolder.java
public class ViewHolder extends RecyclerView.ViewHolder {
public ImageView mImageView;
public TextView mTextView;
//constructor
public ViewHolder (@NonNull View itemView) {
super(itemView);
mImageView = itemView.findViewById(R.id.imageView_adapter_show_bitmap);
mTextView = itemView.findViewById(R.id.textView_adapter_show_string);
}
}
When the constructor inherits RecyclerView.ViewHolder
, it is required to add ```itemView`` to the argument.
Adapter.java
public class Adapter extends RecyclerView.Adapter<ViewHolder> {
private List<DataModel> insertDataList;
public Adapter (List<DataModel> list) {
this.insertDataList = list;
}
@NonNull
@Override
public NoteViewHolder onCreateViewHolder (@NonNull ViewGroup viewGroup, int i) {
View inflate = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.adapter_row, viewGroup, false);
return new ViewHolder(inflate);
}
@Override
public void onBindViewHolder (@NonNull ViewHolder viewHolder, int i) {
noteViewHolder.mImageView.setImageBitmap(insertDataList.get(i).getBitmap());
noteViewHolder.mTextView.setText(insertDataList.get(i).getString());
}
@Override
public int getItemCount () {
return insertDataList.size();
}
}
The usage in `ʻActivity`` etc. is as follows. ** Adapter may conflict with others ... (I actually give it a different name) **
MainActivity.java
public class RegisterNoteActivity extends AppCompatActivity {
RecyclerView mRecyclerViewList;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerViewList = findViewById(R.id.recyclerView_main);
LinearLayoutManager manager = new LinearLayoutManager(this);
mRecyclerViewList.setHasFixedSize(false);
mRecyclerViewList.setLayoutManager(manager);
mRecyclerViewList.setAdapter(new Adapter(createData()));
}
private List<NoteDataModel> createData() {
List<NoteDataModel> list = new ArrayList<>();
for (int index = 0; index < 100; index++) {
//One line of data
NoteDataModel rowData = new NoteDataModel();
rowData.setBitmap(/*Some kind of image*/);
rowData.setmString("This line is" + (index + 1) + "This is the second repetition.");
list.add(rowData);
}
return list;
}
}
Let's implement an animated swipe. To tell the truth, if you implement swipe, you will get an animation that shifts sideways or snaps away. ** Colors and icons don't come with it, so you can implement it here. This time, ** delete only when swiping left **. The color is red and the icon is ** your own ** trash can mark.
MainActivity.java
// ...abridgement
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_note);
// ...abridgement...
mRecyclerViewList.setAdapter(new Adapter(createData())); //A little while ago
//Add from here
final Drawable deleteIcon = ContextCompat.getDrawable(this, R.drawable.ic_action_delete);
ItemTouchHelper.SimpleCallback callback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
@Override
public boolean onMove (@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder viewHolder1) {
return false;
}
@Override
public void onSwiped (@NonNull RecyclerView.ViewHolder viewHolder, int i) {
int swipedPosition = viewHolder.getAdapterPosition();
Adapter adapter = (Adapter) mRecyclerView.getAdapter();
//If you want to register, the process of deleting from the list is here
//Notify that it has been deleted and reflect it.
adapter.notifyItemRemoved(swipedPosition);
}
@Override
public void onChildDraw (@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
View itemView = viewHolder.itemView;
//When canceled
if (dX == 0f && !isCurrentlyActive) {
clearCanvas(c, itemView.getRight() + (int) dX, itemView.getTop(), itemView.getRight(), itemView.getBottom());
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, false);
return;
}
ColorDrawable background = new ColorDrawable();
background .setColor(Color.parseColor("#f44336"));
background.setBounds(itemView.getRight() + (int)dX, itemView.getTop(), itemView.getRight(), itemView.getBottom());
background.draw(c);
int deleteIconTop = itemView.getTop() + (itemView.getHeight() - deleteIcon.getIntrinsicHeight()) / 2;
int deleteIconMargin = (itemView.getHeight() - deleteIcon.getIntrinsicHeight()) / 2;
int deleteIconLeft = itemView.getRight() - deleteIconMargin - deleteIcon.getIntrinsicWidth();
int deleteIconRight = itemView.getRight() - deleteIconMargin;
int deleteIconBottom = deleteIconTop + deleteIcon.getIntrinsicHeight();
deleteIcon.setBounds(deleteIconLeft, deleteIconTop, deleteIconRight, deleteIconBottom);
deleteIcon.draw(c);
}
};
new ItemTouchHelper(callback).attachToRecyclerView(mRecyclerView);
}
private void clearCanvas(Canvas c, int left, int top, int right, int bottom) {
Paint paint = new Paint();
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
c.drawRect(left, top, right, bottom, paint);
}
//abridgement...
Icon images, calculations, color codes, and processing ideas are "Swipe to delete the Android list # How do you do that?" By @ shts And from that Github. I'm sorry without permission.
It is ʻonSwiped`` that fires when the swipe is completed, but even if you cancel it,
ʻonChildDraw is called every frame on the way. It seems that ```onMove
is for when drag and drop is also allowed, but should I return false
?
As an additional note, it seems that you can allow both right swipe and left swipe as follows.
ItemTouchHelper.SimpleCallback callback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public boolean onMove (@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder viewHolder1) {
return false;
}
@Override
public void onSwiped (@NonNull RecyclerView.ViewHolder viewHolder, int i) {
}
@Override
public void onChildDraw (@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
if (dX < 0) {
//When swiping left
} else {
//When swiping right
}
}
};
It's hidden and you can't see it at first glance, but be careful because it says ʻItemTouchHelper.LEFT | ItemTouchHelper.RIGHT`` at the end of the first line.
ʻonChildDraw is as above, but I don't know how to divide the processing in` ʻonSwiped
. (Do you use `ʻi``?)
First of all, thank you to the posters of the site for your reference. It's a little rough, but I'll introduce it here.
Basics of RecyclerView I tried to imitate the RecyclerView itself in almost the same way.
Drag and drop move and swipe removal in RecyclerView I got the image of a swipe.
Swipe to delete the list on Android #How do you do that? I have rewritten the swipe process in Java. → Images and detailed processing are from the attached GitHub.
Note on how to delete an element with a horizontal swipe using RecyclerView on Android I referred to how to update Adapter in Java.
How to delete items in RecyclerView by swiping I referred to the additional processing outside the post.
Thank you everyone. I'm sorry that I can only create processing by wrapping around other people's code in this way, but that's why I hope to benefit someone.
Twitter: https://twitter.com/Cyber_Hacnosuke (Please follow me.) Please like it too.
Recommended Posts