build.gradle
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:design:23.0.1'
compile 'com.android.support:recyclerview-v7:23.0.1'
}
menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
android:title="@string/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always|collapseActionView" />
</menu>
tab_one_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/fragment_bg">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
list_row.xml
<?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"
android:padding="16dp">
<TextView
android:id="@+id/country_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
<TextView
android:id="@+id/country_iso"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp" />
</LinearLayout>
CountryModel.java
package com.tutorialsbuzz.recyclerview.TabFragments;
public class CountryModel {
String name;
String isocode;
CountryModel(String name, String isocode) {
this.name = name;
this.isocode = isocode;
}
public String getName() {
return name;
}
public String getisoCode() {
return isocode;
}
}
RVAdapter.java
package com.tutorialsbuzz.recyclerview.TabFragments;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.tutorialsbuzz.recyclerview.R;
import java.util.ArrayList;
import java.util.List;
public class RVAdapter extends RecyclerView.Adapter<ItemViewHolder> {
private List<CountryModel> mCountryModel;
public RVAdapter(List<CountryModel> countryModel) {
mCountryModel = new ArrayList<>(countryModel);
}
@Override
public void onBindViewHolder(ItemViewHolder itemViewHolder, int i) {
final CountryModel model = mCountryModel.get(i);
itemViewHolder.bind(model);
}
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_row, viewGroup, false);
return new ItemViewHolder(view);
}
@Override
public int getItemCount() {
return mCountryModel.size();
}
/** Filter Logic**/
public void animateTo(List<CountryModel> models) {
applyAndAnimateRemovals(models);
applyAndAnimateAdditions(models);
applyAndAnimateMovedItems(models);
}
private void applyAndAnimateRemovals(List<CountryModel> newModels) {
for (int i = mCountryModel.size() - 1; i >= 0; i--) {
final CountryModel model = mCountryModel.get(i);
if (!newModels.contains(model)) {
removeItem(i);
}
}
}
private void applyAndAnimateAdditions(List<CountryModel> newModels) {
for (int i = 0, count = newModels.size(); i < count; i++) {
final CountryModel model = newModels.get(i);
if (!mCountryModel.contains(model)) {
addItem(i, model);
}
}
}
private void applyAndAnimateMovedItems(List<CountryModel> newModels) {
for (int toPosition = newModels.size() - 1; toPosition >= 0; toPosition--) {
final CountryModel model = newModels.get(toPosition);
final int fromPosition = mCountryModel.indexOf(model);
if (fromPosition >= 0 && fromPosition != toPosition) {
moveItem(fromPosition, toPosition);
}
}
}
public CountryModel removeItem(int position) {
final CountryModel model = mCountryModel.remove(position);
notifyItemRemoved(position);
return model;
}
public void addItem(int position, CountryModel model) {
mCountryModel.add(position, model);
notifyItemInserted(position);
}
public void moveItem(int fromPosition, int toPosition) {
final CountryModel model = mCountryModel.remove(fromPosition);
mCountryModel.add(toPosition, model);
notifyItemMoved(fromPosition, toPosition);
}
}
ItemViewHolder.java
package com.tutorialsbuzz.recyclerview.TabFragments;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import com.tutorialsbuzz.recyclerview.R;
public class ItemViewHolder extends RecyclerView.ViewHolder {
public TextView name_TextView;
public TextView iso_TextView;
public ItemViewHolder(View itemView) {
super(itemView);
name_TextView = (TextView) itemView.findViewById(R.id.country_name);
iso_TextView = (TextView) itemView.findViewById(R.id.country_iso);
}
public void bind(CountryModel countryModel) {
name_TextView.setText(countryModel.getName());
iso_TextView.setText(countryModel.getisoCode());
}
}
TabOneFragment.java
package com.tutorialsbuzz.recyclerview.TabFragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
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 com.tutorialsbuzz.recyclerview.R;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class TabOneFragment extends Fragment implements SearchView.OnQueryTextListener {
private RecyclerView recyclerview;
private List<CountryModel> mCountryModel;
private RVAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_one_fragment, container, false);
recyclerview = (RecyclerView) view.findViewById(R.id.recyclerview);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerview.setLayoutManager(layoutManager);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setHasOptionsMenu(true);
String[] locales = Locale.getISOCountries();
mCountryModel = new ArrayList<>();
for (String countryCode : locales) {
Locale obj = new Locale("", countryCode);
mCountryModel.add(new CountryModel(obj.getDisplayCountry(), obj.getISO3Country()));
}
adapter = new RVAdapter(mCountryModel);
recyclerview.setAdapter(adapter);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_main, menu);
final MenuItem item = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(this);
MenuItemCompat.setOnActionExpandListener(item,
new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Do something when collapsed
adapter.animateTo(mCountryModel);
recyclerview.scrollToPosition(0);
return true; // Return true to collapse action view
}
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
// Do something when expanded
return true; // Return true to expand action view
}
});
}
@Override
public boolean onQueryTextChange(String newText) {
final List<CountryModel> filteredModelList = filter(mCountryModel, newText);
adapter.animateTo(filteredModelList);
recyclerview.scrollToPosition(0);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
private List<CountryModel> filter(List<CountryModel> models, String query) {
query = query.toLowerCase();
final List<CountryModel> filteredModelList = new ArrayList<>();
for (CountryModel model : models) {
final String text = model.getName().toLowerCase();
if (text.contains(query)) {
filteredModelList.add(model);
}
}
return filteredModelList;
}
}
No comments:
Post a Comment