搜索视图在包含 RecyclerView 的片段中没有给出任何结果。单击“搜索”图标后,屏幕是空白的
The Search View is not giving any result in my Fragment containing RecyclerView. After I click on the Search icon the screen is just blank
我有一个包含 RecyclerView 和 Room Database 的片段,我想添加一个搜索视图。我已经成功添加了搜索图标,还在 Fragment 和 Adapter 中添加了代码。但问题是当我点击搜索图标时,屏幕是空白的。此外,当我输入任何内容时,它不会给我任何结果。
如果我关闭搜索视图并单击后退按钮,片段中的 RecyclerView 将不再可见。我已经在线检查了解决方案,它使用了类似的代码。所以,我不明白我的代码中有什么错误。谁能帮帮我?
我的片段代码
'''
package com.nitin.roomrecyclerview;
import androidx.appcompat.widget.SearchView;
import androidx.core.view.MenuItemCompat;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import java.util.ArrayList;
import java.util.List;
public class FragmentStatus extends Fragment implements MyViewHolder.OnStatusListener, SearchView.OnQueryTextListener, MenuItem.OnActionExpandListener {
private FragmentStatusViewModel mViewModel;
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private MyAdapter mAdapter;
FloatingActionButton floatingActionButton;
Bundle bundletest;
Bundle bundletest1;
Status status;
private List<Status> statuses = new ArrayList<>();
public static FragmentStatus newInstance() {
return new FragmentStatus();
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
inflater.inflate(R.menu.main, menu);
MenuItem menuItem = menu.findItem(R.id.search);
SearchView searchView = (SearchView) menuItem.getActionView();
searchView.setOnQueryTextListener(this);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
if (newText == null || newText.trim().isEmpty()) {
mAdapter.setfilter(statuses);
return false;
} else {
newText = newText.toLowerCase();
List<Status> newList = new ArrayList<>();
for (Status status : statuses) {
if (status.getTitle().toLowerCase().contains(newText)){
newList.add(status);
}
}
Toast.makeText(getActivity(), "newtext" + newText, Toast.LENGTH_SHORT).show();
mAdapter.setfilter(newList);
return true;
}
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.deleteallstatus:
FragmentStatusViewModel mViewModel = new ViewModelProvider(this).get(FragmentStatusViewModel.class);
mViewModel.deleteAllStatus();
Toast.makeText(getActivity(), "All Status Deleted", Toast.LENGTH_SHORT).show();
return true;
case R.id.action_settings:
Toast.makeText(getActivity(), "Setting Selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
// setHasOptionsMenu(true);
return inflater.inflate(R.layout.fragment_status_fragment, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
recyclerView = (getActivity()).findViewById(R.id.statusRecyclerView);
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
floatingActionButton = getActivity().findViewById(R.id.button_add_status);
floatingActionButton.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_fragmentStatus_to_updateFragment, null));
//status = new Status(title,description);
mAdapter = new MyAdapter(statuses, this);
recyclerView.setAdapter(mAdapter);
mViewModel = new ViewModelProvider(this).get(FragmentStatusViewModel.class);
mViewModel.getAllStatus().observe(getViewLifecycleOwner(), new Observer<List<Status>>() {
@Override
public void onChanged(List<Status> statuses) {
mAdapter.setStatuses(statuses);
}
});
if(getArguments() != null && getArguments().containsKey("ID") == true && bundletest != getArguments()){
int id = getArguments().getInt("ID", -1);
if(id == -1){
Toast.makeText(getActivity(), "Status can't be updated", Toast.LENGTH_LONG).show();
}
String title = getArguments().getString("title1");
String description = getArguments().getString("description1");
Status status = new Status(title,description);
mViewModel.update(status);
status.setId(id);
bundletest = getArguments();
Toast.makeText(getActivity(), "Status updated" + id, Toast.LENGTH_LONG).show();
}else if (getArguments() != null && !getArguments().containsKey("ID") && bundletest1 != getArguments()){
String title = getArguments().getString("title");
String description = getArguments().getString("description");
Status status = new Status(title,description);
mViewModel.insert(status);
bundletest1 = getArguments();
Toast.makeText(getActivity(), "Status saved", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(getActivity(), "Statuses displayed!", Toast.LENGTH_SHORT).show();
}
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT |
ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
mViewModel.delete(mAdapter.getStatusAt(viewHolder.getAdapterPosition()));
Toast.makeText(getActivity(), "Note Deleted", Toast.LENGTH_LONG).show();
}
}).attachToRecyclerView(recyclerView);
}
@Override
public void onStatusClick(Status status) {
Toast.makeText(getActivity(), "the id is " + status.getId(), Toast.LENGTH_SHORT).show();
Bundle bundle = new Bundle();
bundle.putInt("ID", status.getId());
bundle.putString("description", status.getDescription());
bundle.putString("title", status.getTitle());
Navigation.findNavController(getActivity().findViewById(R.id.recyclerLayout)).navigate(R.id.action_fragmentStatus_to_editStatusFragment, bundle);
}
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
mAdapter.setfilter(statuses);
return true;
}
}
'''
My Adapter Code
'''
package com.nitin.roomrecyclerview;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
private List<Status> statuses = new ArrayList<>();
private MyViewHolder.OnStatusListener listener;
public MyAdapter(List<Status> statuses, MyViewHolder.OnStatusListener listener) {
this.statuses = statuses;
this.listener = listener;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.status_recyclerview, parent, false);
MyViewHolder myViewHolder = new MyViewHolder(view, listener, statuses);
return myViewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
Status status = statuses.get(position);
holder.title_template.setText(status.getTitle());
holder.description_template.setText(status.getDescription());
}
@Override
public int getItemCount() {
return statuses.size();
}
public void setStatuses(List<Status> statuses){
this.statuses = statuses;
notifyDataSetChanged();
}
public Status getStatusAt(int position){
return statuses.get(position);
}
public void setfilter(List<Status> newList){
statuses.clear();
statuses.addAll(newList);
notifyDataSetChanged();
}
}
'''
我的 ViewHolder 代码
'''
package com.nitin.roomrecyclerview;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView title_template;
public TextView description_template;
private List<Status> statuses = new ArrayList<>();
OnStatusListener listener;
//Status status;
public MyViewHolder(@NonNull View itemView, OnStatusListener listener, List<Status> statuses) {
super(itemView);
this.title_template = itemView.findViewById(R.id.title_template);
this.description_template = itemView.findViewById(R.id.description_template);
this.listener = listener;
this.statuses = statuses;
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
listener.onStatusClick(statuses.get(getAdapterPosition()));
}
interface OnStatusListener{
void onStatusClick(Status status);
}
}
'''
我的菜单XML代码
'''
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never" />
<item
android:id="@+id/deleteallstatus"
android:orderInCategory="100"
android:title="Delete all status"
app:showAsAction="never" />
<item
android:id="@+id/search"
android:orderInCategory="100"
android:icon="@drawable/ic_search"
app:showAsAction="ifRoom|collapseActionView"
android:title="Search"
app:actionViewClass="androidx.appcompat.widget.SearchView" />
</menu>
'''
由于我使用的是LiveData...代码如下
我不得不在 Adapter 中实现 Filterable...然后代码如下所示。
在片段中
'''
package com.nitin.roomrecyclerview;
import androidx.activity.OnBackPressedCallback;
import androidx.appcompat.widget.SearchView;
import androidx.core.view.MenuItemCompat;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import java.util.ArrayList;
import java.util.List;
public class FragmentStatus extends Fragment implements MyViewHolder.OnStatusListener {
private FragmentStatusViewModel mViewModel;
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private MyAdapter mAdapter;
FloatingActionButton floatingActionButton;
Bundle bundletest;
Bundle bundletest1;
private List<Status> statuses = new ArrayList<>();
public static FragmentStatus newInstance() {
return new FragmentStatus();
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
inflater.inflate(R.menu.main, menu);
MenuItem menuItem = menu.findItem(R.id.search);
SearchView searchView = (SearchView) menuItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
mAdapter.getFilter().filter(newText);
return true;
}
});
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.deleteallstatus:
FragmentStatusViewModel mViewModel = new ViewModelProvider(this).get(FragmentStatusViewModel.class);
mViewModel.deleteAllStatus();
Toast.makeText(getActivity(), "All Status Deleted", Toast.LENGTH_SHORT).show();
return true;
case R.id.action_settings:
Toast.makeText(getActivity(), "Setting Selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
// setHasOptionsMenu(true);
return inflater.inflate(R.layout.fragment_status_fragment, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
recyclerView = (getActivity()).findViewById(R.id.statusRecyclerView);
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
floatingActionButton = getActivity().findViewById(R.id.button_add_status);
floatingActionButton.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_fragmentStatus_to_updateFragment, null));
//status = new Status(title,description);
mAdapter = new MyAdapter(getActivity(), statuses, this);
recyclerView.setAdapter(mAdapter);
mViewModel = new ViewModelProvider(this).get(FragmentStatusViewModel.class);
mViewModel.getAllStatus().observe(getViewLifecycleOwner(), new Observer<List<Status>>() {
@Override
public void onChanged(List<Status> statuses) {
mAdapter.setStatuses(statuses);
}
});
if(getArguments() != null && getArguments().containsKey("ID") == true && bundletest != getArguments()){
int id = getArguments().getInt("ID", -1);
if(id == -1){
Toast.makeText(getActivity(), "Status can't be updated", Toast.LENGTH_LONG).show();
}
String title = getArguments().getString("title1");
String description = getArguments().getString("description1");
Status status = new Status(title,description);
mViewModel.update(status);
status.setId(id);
bundletest = getArguments();
Toast.makeText(getActivity(), "Status updated" + id, Toast.LENGTH_LONG).show();
}else if (getArguments() != null && !getArguments().containsKey("ID") && bundletest1 != getArguments()){
String title = getArguments().getString("title");
String description = getArguments().getString("description");
Status status = new Status(title,description);
mViewModel.insert(status);
bundletest1 = getArguments();
Toast.makeText(getActivity(), "Status saved", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(getActivity(), "Statuses displayed!", Toast.LENGTH_SHORT).show();
}
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT |
ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
mViewModel.delete(mAdapter.getStatusAt(viewHolder.getAdapterPosition()));
Toast.makeText(getActivity(), "Note Deleted", Toast.LENGTH_LONG).show();
}
}).attachToRecyclerView(recyclerView);
}
@Override
public void onStatusClick(Status status) {
Toast.makeText(getActivity(), "the id is " + status.getId(), Toast.LENGTH_SHORT).show();
Bundle bundle = new Bundle();
bundle.putInt("ID", status.getId());
bundle.putString("description", status.getDescription());
bundle.putString("title", status.getTitle());
Navigation.findNavController(getActivity().findViewById(R.id.recyclerLayout)).navigate(R.id.action_fragmentStatus_to_editStatusFragment, bundle);
}
}
'''
适配器代码
'''
package com.nitin.roomrecyclerview;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import android.widget.Toast;
import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;
import com.nitin.roomrecyclerview.ui.home.HomeFragment;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> implements Filterable {
private static final String TAG = "TEST_1";
private List<Status> statuses;
private MyViewHolder.OnStatusListener listener;
ArrayList<Status> arrayList = new ArrayList<>();
Context context;
public MyAdapter(Context context, List<Status> statuses, MyViewHolder.OnStatusListener listener) {
this.statuses = statuses;
this.listener = listener;
this.context = context;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.status_recyclerview, parent, false);
MyViewHolder myViewHolder = new MyViewHolder(view, listener, statuses);
return myViewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
Status status = statuses.get(position);
holder.title_template.setText(status.getTitle());
holder.description_template.setText(status.getDescription());
}
@Override
public int getItemCount() {
return statuses.size();
}
public void setStatuses(List<Status> newstatuses) {
this.statuses = newstatuses;
arrayList.addAll(statuses);
notifyDataSetChanged();
}
public Status getStatusAt(int position) {
return statuses.get(position);
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
constraint = constraint.toString().toLowerCase().trim();
statuses.clear();
if (constraint.length() == 0) {
statuses.addAll(arrayList);
} else {
for (Status item : arrayList) {
if (item.getTitle().toLowerCase(Locale.getDefault()).contains(constraint)
|| item.getDescription().toLowerCase(Locale.getDefault()).contains(constraint)) {
statuses.add(item);
}
}
}
FilterResults results = new FilterResults();
results.values = statuses;
results.count = statuses.size();
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if(results.count == 0){
Toast.makeText(context, "No Results found", Toast.LENGTH_LONG).show();
notifyDataSetChanged();
}else {
notifyDataSetChanged();
}
}
};
}
}
'''
我有一个包含 RecyclerView 和 Room Database 的片段,我想添加一个搜索视图。我已经成功添加了搜索图标,还在 Fragment 和 Adapter 中添加了代码。但问题是当我点击搜索图标时,屏幕是空白的。此外,当我输入任何内容时,它不会给我任何结果。 如果我关闭搜索视图并单击后退按钮,片段中的 RecyclerView 将不再可见。我已经在线检查了解决方案,它使用了类似的代码。所以,我不明白我的代码中有什么错误。谁能帮帮我?
我的片段代码 '''
package com.nitin.roomrecyclerview;
import androidx.appcompat.widget.SearchView;
import androidx.core.view.MenuItemCompat;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import java.util.ArrayList;
import java.util.List;
public class FragmentStatus extends Fragment implements MyViewHolder.OnStatusListener, SearchView.OnQueryTextListener, MenuItem.OnActionExpandListener {
private FragmentStatusViewModel mViewModel;
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private MyAdapter mAdapter;
FloatingActionButton floatingActionButton;
Bundle bundletest;
Bundle bundletest1;
Status status;
private List<Status> statuses = new ArrayList<>();
public static FragmentStatus newInstance() {
return new FragmentStatus();
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
inflater.inflate(R.menu.main, menu);
MenuItem menuItem = menu.findItem(R.id.search);
SearchView searchView = (SearchView) menuItem.getActionView();
searchView.setOnQueryTextListener(this);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
if (newText == null || newText.trim().isEmpty()) {
mAdapter.setfilter(statuses);
return false;
} else {
newText = newText.toLowerCase();
List<Status> newList = new ArrayList<>();
for (Status status : statuses) {
if (status.getTitle().toLowerCase().contains(newText)){
newList.add(status);
}
}
Toast.makeText(getActivity(), "newtext" + newText, Toast.LENGTH_SHORT).show();
mAdapter.setfilter(newList);
return true;
}
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.deleteallstatus:
FragmentStatusViewModel mViewModel = new ViewModelProvider(this).get(FragmentStatusViewModel.class);
mViewModel.deleteAllStatus();
Toast.makeText(getActivity(), "All Status Deleted", Toast.LENGTH_SHORT).show();
return true;
case R.id.action_settings:
Toast.makeText(getActivity(), "Setting Selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
// setHasOptionsMenu(true);
return inflater.inflate(R.layout.fragment_status_fragment, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
recyclerView = (getActivity()).findViewById(R.id.statusRecyclerView);
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
floatingActionButton = getActivity().findViewById(R.id.button_add_status);
floatingActionButton.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_fragmentStatus_to_updateFragment, null));
//status = new Status(title,description);
mAdapter = new MyAdapter(statuses, this);
recyclerView.setAdapter(mAdapter);
mViewModel = new ViewModelProvider(this).get(FragmentStatusViewModel.class);
mViewModel.getAllStatus().observe(getViewLifecycleOwner(), new Observer<List<Status>>() {
@Override
public void onChanged(List<Status> statuses) {
mAdapter.setStatuses(statuses);
}
});
if(getArguments() != null && getArguments().containsKey("ID") == true && bundletest != getArguments()){
int id = getArguments().getInt("ID", -1);
if(id == -1){
Toast.makeText(getActivity(), "Status can't be updated", Toast.LENGTH_LONG).show();
}
String title = getArguments().getString("title1");
String description = getArguments().getString("description1");
Status status = new Status(title,description);
mViewModel.update(status);
status.setId(id);
bundletest = getArguments();
Toast.makeText(getActivity(), "Status updated" + id, Toast.LENGTH_LONG).show();
}else if (getArguments() != null && !getArguments().containsKey("ID") && bundletest1 != getArguments()){
String title = getArguments().getString("title");
String description = getArguments().getString("description");
Status status = new Status(title,description);
mViewModel.insert(status);
bundletest1 = getArguments();
Toast.makeText(getActivity(), "Status saved", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(getActivity(), "Statuses displayed!", Toast.LENGTH_SHORT).show();
}
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT |
ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
mViewModel.delete(mAdapter.getStatusAt(viewHolder.getAdapterPosition()));
Toast.makeText(getActivity(), "Note Deleted", Toast.LENGTH_LONG).show();
}
}).attachToRecyclerView(recyclerView);
}
@Override
public void onStatusClick(Status status) {
Toast.makeText(getActivity(), "the id is " + status.getId(), Toast.LENGTH_SHORT).show();
Bundle bundle = new Bundle();
bundle.putInt("ID", status.getId());
bundle.putString("description", status.getDescription());
bundle.putString("title", status.getTitle());
Navigation.findNavController(getActivity().findViewById(R.id.recyclerLayout)).navigate(R.id.action_fragmentStatus_to_editStatusFragment, bundle);
}
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
mAdapter.setfilter(statuses);
return true;
}
}
'''
My Adapter Code
'''
package com.nitin.roomrecyclerview;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
private List<Status> statuses = new ArrayList<>();
private MyViewHolder.OnStatusListener listener;
public MyAdapter(List<Status> statuses, MyViewHolder.OnStatusListener listener) {
this.statuses = statuses;
this.listener = listener;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.status_recyclerview, parent, false);
MyViewHolder myViewHolder = new MyViewHolder(view, listener, statuses);
return myViewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
Status status = statuses.get(position);
holder.title_template.setText(status.getTitle());
holder.description_template.setText(status.getDescription());
}
@Override
public int getItemCount() {
return statuses.size();
}
public void setStatuses(List<Status> statuses){
this.statuses = statuses;
notifyDataSetChanged();
}
public Status getStatusAt(int position){
return statuses.get(position);
}
public void setfilter(List<Status> newList){
statuses.clear();
statuses.addAll(newList);
notifyDataSetChanged();
}
}
'''
我的 ViewHolder 代码
'''
package com.nitin.roomrecyclerview;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView title_template;
public TextView description_template;
private List<Status> statuses = new ArrayList<>();
OnStatusListener listener;
//Status status;
public MyViewHolder(@NonNull View itemView, OnStatusListener listener, List<Status> statuses) {
super(itemView);
this.title_template = itemView.findViewById(R.id.title_template);
this.description_template = itemView.findViewById(R.id.description_template);
this.listener = listener;
this.statuses = statuses;
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
listener.onStatusClick(statuses.get(getAdapterPosition()));
}
interface OnStatusListener{
void onStatusClick(Status status);
}
}
'''
我的菜单XML代码
'''
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never" />
<item
android:id="@+id/deleteallstatus"
android:orderInCategory="100"
android:title="Delete all status"
app:showAsAction="never" />
<item
android:id="@+id/search"
android:orderInCategory="100"
android:icon="@drawable/ic_search"
app:showAsAction="ifRoom|collapseActionView"
android:title="Search"
app:actionViewClass="androidx.appcompat.widget.SearchView" />
</menu>
'''
由于我使用的是LiveData...代码如下
我不得不在 Adapter 中实现 Filterable...然后代码如下所示。
在片段中 '''
package com.nitin.roomrecyclerview;
import androidx.activity.OnBackPressedCallback;
import androidx.appcompat.widget.SearchView;
import androidx.core.view.MenuItemCompat;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import java.util.ArrayList;
import java.util.List;
public class FragmentStatus extends Fragment implements MyViewHolder.OnStatusListener {
private FragmentStatusViewModel mViewModel;
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private MyAdapter mAdapter;
FloatingActionButton floatingActionButton;
Bundle bundletest;
Bundle bundletest1;
private List<Status> statuses = new ArrayList<>();
public static FragmentStatus newInstance() {
return new FragmentStatus();
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
inflater.inflate(R.menu.main, menu);
MenuItem menuItem = menu.findItem(R.id.search);
SearchView searchView = (SearchView) menuItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
mAdapter.getFilter().filter(newText);
return true;
}
});
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.deleteallstatus:
FragmentStatusViewModel mViewModel = new ViewModelProvider(this).get(FragmentStatusViewModel.class);
mViewModel.deleteAllStatus();
Toast.makeText(getActivity(), "All Status Deleted", Toast.LENGTH_SHORT).show();
return true;
case R.id.action_settings:
Toast.makeText(getActivity(), "Setting Selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
// setHasOptionsMenu(true);
return inflater.inflate(R.layout.fragment_status_fragment, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
recyclerView = (getActivity()).findViewById(R.id.statusRecyclerView);
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
floatingActionButton = getActivity().findViewById(R.id.button_add_status);
floatingActionButton.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_fragmentStatus_to_updateFragment, null));
//status = new Status(title,description);
mAdapter = new MyAdapter(getActivity(), statuses, this);
recyclerView.setAdapter(mAdapter);
mViewModel = new ViewModelProvider(this).get(FragmentStatusViewModel.class);
mViewModel.getAllStatus().observe(getViewLifecycleOwner(), new Observer<List<Status>>() {
@Override
public void onChanged(List<Status> statuses) {
mAdapter.setStatuses(statuses);
}
});
if(getArguments() != null && getArguments().containsKey("ID") == true && bundletest != getArguments()){
int id = getArguments().getInt("ID", -1);
if(id == -1){
Toast.makeText(getActivity(), "Status can't be updated", Toast.LENGTH_LONG).show();
}
String title = getArguments().getString("title1");
String description = getArguments().getString("description1");
Status status = new Status(title,description);
mViewModel.update(status);
status.setId(id);
bundletest = getArguments();
Toast.makeText(getActivity(), "Status updated" + id, Toast.LENGTH_LONG).show();
}else if (getArguments() != null && !getArguments().containsKey("ID") && bundletest1 != getArguments()){
String title = getArguments().getString("title");
String description = getArguments().getString("description");
Status status = new Status(title,description);
mViewModel.insert(status);
bundletest1 = getArguments();
Toast.makeText(getActivity(), "Status saved", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(getActivity(), "Statuses displayed!", Toast.LENGTH_SHORT).show();
}
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT |
ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
mViewModel.delete(mAdapter.getStatusAt(viewHolder.getAdapterPosition()));
Toast.makeText(getActivity(), "Note Deleted", Toast.LENGTH_LONG).show();
}
}).attachToRecyclerView(recyclerView);
}
@Override
public void onStatusClick(Status status) {
Toast.makeText(getActivity(), "the id is " + status.getId(), Toast.LENGTH_SHORT).show();
Bundle bundle = new Bundle();
bundle.putInt("ID", status.getId());
bundle.putString("description", status.getDescription());
bundle.putString("title", status.getTitle());
Navigation.findNavController(getActivity().findViewById(R.id.recyclerLayout)).navigate(R.id.action_fragmentStatus_to_editStatusFragment, bundle);
}
}
'''
适配器代码 '''
package com.nitin.roomrecyclerview;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import android.widget.Toast;
import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;
import com.nitin.roomrecyclerview.ui.home.HomeFragment;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> implements Filterable {
private static final String TAG = "TEST_1";
private List<Status> statuses;
private MyViewHolder.OnStatusListener listener;
ArrayList<Status> arrayList = new ArrayList<>();
Context context;
public MyAdapter(Context context, List<Status> statuses, MyViewHolder.OnStatusListener listener) {
this.statuses = statuses;
this.listener = listener;
this.context = context;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.status_recyclerview, parent, false);
MyViewHolder myViewHolder = new MyViewHolder(view, listener, statuses);
return myViewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
Status status = statuses.get(position);
holder.title_template.setText(status.getTitle());
holder.description_template.setText(status.getDescription());
}
@Override
public int getItemCount() {
return statuses.size();
}
public void setStatuses(List<Status> newstatuses) {
this.statuses = newstatuses;
arrayList.addAll(statuses);
notifyDataSetChanged();
}
public Status getStatusAt(int position) {
return statuses.get(position);
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
constraint = constraint.toString().toLowerCase().trim();
statuses.clear();
if (constraint.length() == 0) {
statuses.addAll(arrayList);
} else {
for (Status item : arrayList) {
if (item.getTitle().toLowerCase(Locale.getDefault()).contains(constraint)
|| item.getDescription().toLowerCase(Locale.getDefault()).contains(constraint)) {
statuses.add(item);
}
}
}
FilterResults results = new FilterResults();
results.values = statuses;
results.count = statuses.size();
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if(results.count == 0){
Toast.makeText(context, "No Results found", Toast.LENGTH_LONG).show();
notifyDataSetChanged();
}else {
notifyDataSetChanged();
}
}
};
}
}
'''