Recyclerview with searchview in actionbar to add searchview to you can search in item from recyclerview list.it is use to two methods are override to onQueryTextSubmit And onQueryTextChange
onQueryTextSubmit
In this method to use of you can input searchview and click to search item when you can searchable word.but it can first input type to search item and then click button after searchable item show.
onQueryTextChange
In this method to use of at a time to input word to search result show on at a time. ane refresh adapter to at a time and display search item in this method.
Add Depedancy AndroidManifest.xml File
implementation 'com.android.support:recyclerview-v7:28.0.0-alpha3'
Add Menu Resource File Use to Searchview main_menu.xml
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/app_bar_search" android:icon="@drawable/ic_search_black_24dp" android:title="Search" app:showAsAction="always |collapseActionView" app:actionViewClass="android.support.v7.widget.SearchView" /> </menu>
Add Custom Layout File to Use Set Adapter in Recyclerview item_row.xml
<?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" android:layout_margin="5dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:layout_width="100dp" android:layout_height="100dp" android:id="@+id/img_view"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_gravity="center" android:textSize="20dp" android:id="@+id/text_view"/> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#000000" /> </LinearLayout>
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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"> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/recycler_view"/> </LinearLayout>
Create Getter Setter Method Class to use this set and get data in list item. FruitsData.java
package com.example.bhaumik.recyclerviewdemo; public class FruitsData { public String name; public int image; public FruitsData(String name, int image) { this.name = name; this.image = image; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getImage() { return image; } public void setImage(int image) { this.image = image; } }
MainActivity.java
package com.example.bhaumik.recyclerviewdemo; import android.support.v4.view.MenuItemCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.SearchView; import android.view.Menu; import android.view.MenuItem; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { RecyclerView recyclerView; MyAdapter myAdapter; List<FruitsData> list = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); list.add(new FruitsData("Apple",R.drawable.apple)); list.add(new FruitsData("Banana",R.drawable.banana)); list.add(new FruitsData("Grapes",R.drawable.grapes)); list.add(new FruitsData("JackFruit",R.drawable.jack_fruit)); list.add(new FruitsData("Lemon",R.drawable.lemon)); list.add(new FruitsData("Mango",R.drawable.mango)); list.add(new FruitsData("Orange",R.drawable.orange)); list.add(new FruitsData("Papaya",R.drawable.papaya)); list.add(new FruitsData("pear",R.drawable.pear)); list.add(new FruitsData("Pineapple",R.drawable.pine_apple)); recyclerView = findViewById(R.id.recycler_view); recyclerView.setLayoutManager(new LinearLayoutManager(this)); myAdapter = new MyAdapter(MainActivity.this,list); recyclerView.setAdapter(myAdapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main_menu,menu); MenuItem menuItem = menu.findItem(R.id.app_bar_search); SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { return true; } @Override public boolean onQueryTextChange(String newText) { if (myAdapter != null){ myAdapter.getFilter().filter(newText); } return true; } }); return true; } }
Create Adapter to Set Data in recyclerview MyAdapter.java
package com.example.bhaumik.recyclerviewdemo; import android.content.Context; import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Filter; import android.widget.Filterable; import android.widget.ImageView; import android.widget.TextView; import java.util.ArrayList; import java.util.List; public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> implements Filterable { Context context; List<FruitsData> list = new ArrayList<>(); List<FruitsData> list1 = new ArrayList<>(); public MyAdapter(Context context, List<FruitsData> list) { this.context = context; this.list = list; this.list1 = list; } @NonNull @Override public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View view = LayoutInflater.from(context).inflate(R.layout.item_list,viewGroup,false); return new MyHolder(view); } @Override public void onBindViewHolder(@NonNull MyHolder myHolder, int i) { myHolder.imageView.setImageResource(list.get(i).getImage()); myHolder.textView.setText(list.get(i).getName()); } @Override public int getItemCount() { return list.size(); } public static class MyHolder extends RecyclerView.ViewHolder{ ImageView imageView; TextView textView; public MyHolder(@NonNull View itemView) { super(itemView); imageView = itemView.findViewById(R.id.img_view); textView = itemView.findViewById(R.id.text_view); } } @Override public Filter getFilter() { return new Filter() { @Override protected FilterResults performFiltering(CharSequence constraint) { String charString = constraint.toString(); if (charString.isEmpty()){ list = list1; }else{ List<FruitsData> filterList = new ArrayList<>(); for (FruitsData data : list1){ if (data.getName().toLowerCase().contains(charString)){ filterList.add(data); } } list = filterList; } FilterResults filterResults = new FilterResults(); filterResults.values = list; return filterResults; } @Override protected void publishResults(CharSequence constraint, FilterResults results) { list = (List<FruitsData>) results.values; notifyDataSetChanged(); } }; } }
0 comments:
Post a Comment