【ANDROID STUDIO】 recycler view action mode

package com.example.recyclerviewactionmode;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.view.ActionMode;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements SelectMode {
MyAdapter myAdapter;
ActionMode mActionMode;


private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.context_menu, menu);
return true;
}

@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}

@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.delete_all:
myAdapter.deleteAllSelected();
mode.finish();
return true;
default:
return false;
}
}

@Override
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
};


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

List<String> list = new ArrayList<>();
list.add("China");
list.add("France");
list.add("Germany");
list.add("India");
list.add("Russia");
list.add("United Kingdom");
list.add("United States");

RecyclerView recyclerView = findViewById(R.id.recyclerView);

recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);

myAdapter = new MyAdapter(list, this);
recyclerView.setAdapter(myAdapter);
}

@Override
public void onSelect() {
if (mActionMode != null) return;
mActionMode = startSupportActionMode(mActionModeCallback);
}

}
package com.example.recyclerviewactionmode;

import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>
implements SelectMode { //Added implements

private SelectMode mListener;
private List<String> nameList;
//Added following list
private SparseArray<Boolean> selectedList = new SparseArray<>();

public MyAdapter(List<String> list, SelectMode listener) { //Added listener to parameters
nameList = list;
mListener = listener;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.item, parent, false);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}

@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
final String name = nameList.get(position);
holder.textView.setText(name);
holder.itemView.setSelected(selectedList.get(position, false)); //New (for There's more)

holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.itemView.setSelected(!holder.itemView.isSelected());
if (holder.itemView.isSelected()) {
selectedList.put(position, true);
} else {
selectedList.remove(position);
}
onSelect();
}
});
}

@Override
public int getItemCount() {
if (nameList == null) {
return 0;
} else {
return nameList.size();
}
}

private void remove(int position) {
nameList.remove(position);
notifyItemRemoved(position);
}

//New method
public void deleteAllSelected() {
if (selectedList.size() == 0) {
return;
}
for (int index = nameList.size() - 1; index >= 0; index--) {
if (selectedList.get(index, false)) {
remove(index);
}
}
selectedList.clear();
}

@Override
public void onSelect() {
if (mListener != null) {
mListener.onSelect();
}
}

public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView textView;

public MyViewHolder(View itemVieww) {
super(itemVieww);
textView = itemView.findViewById(R.id.textView);
}
}

}
package com.example.recyclerviewactionmode;

public interface SelectMode {
void onSelect();
}

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:context=".MainActivity">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>


</androidx.constraintlayout.widget.ConstraintLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/delete_all"
android:title="@string/delete_all" />
</menu>

No comments:

When Bad People Make Good Art

I offer six guidelines on cancel culture ͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏...

Contact Form

Name

Email *

Message *