티스토리 뷰

android

FilterRecyclerView

구름나드리 2018. 10. 5. 17:42


SearchView + RecyclerView






RecyclerView & CardView Dependencies:

developer.android.com/topic/libraries/support-library/packages.html#v7-recyclerview

developer.android.com/topic/libraries/support-library/packages.html#v7-cardview




dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.0'
implementation 'com.android.support:design:27.1.0'
implementation 'com.android.support:cardview-v7:27.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}




ExampleAdapter.java



public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> implements Filterable {

private List<ExampleItem> exampleList;
private List<ExampleItem> exampleListFull;

public class ExampleViewHolder extends RecyclerView.ViewHolder {

ImageView imageView;
TextView textView1;
TextView textView2;

public ExampleViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.image_view);
textView1 = itemView.findViewById(R.id.text_view1);
textView2 = itemView.findViewById(R.id.text_view2);
}
}

public ExampleAdapter(List<ExampleItem> exampleList) {
this.exampleList = exampleList;
exampleListFull = new ArrayList<>(exampleList);
}

@Override
public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item,
parent, false);
return new ExampleViewHolder(v);
}

@Override
public void onBindViewHolder(ExampleViewHolder holder, int position) {
ExampleItem currentItem = exampleList.get(position);

holder.imageView.setImageResource(currentItem.getImageResource());
holder.textView1.setText(currentItem.getText1());
holder.textView2.setText(currentItem.getText2());
}

@Override
public int getItemCount() {
return exampleList.size();
}

@Override
public Filter getFilter() {
return exampleFilter;
}

private Filter exampleFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {

List<ExampleItem> filteredList = new ArrayList<>();
if (constraint == null || constraint.length() == 0) {
filteredList.addAll(exampleListFull);
} else {
String filterPatten = constraint.toString().toLowerCase().trim();

for (ExampleItem item : exampleList) {
if (item.getText1().toLowerCase().contains(filterPatten)) {
filteredList.add(item);
}
}
}

FilterResults results = new FilterResults();
results.values = filteredList;

return results;
}

@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
exampleList.clear();
exampleList.addAll((List) results.values);
notifyDataSetChanged();
}
};
}



example_menu.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_search"
android:icon="@drawable/ic_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="example"/>

</menu>



MainActivity.java


public class MainActivity extends AppCompatActivity {

private ExampleAdapter exampleAdapter;
private List<ExampleItem> exampleList;

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

fillExampleList();
setUpRecyclerView();
}


private void fillExampleList() {

exampleList = new ArrayList<>();
exampleList.add(new ExampleItem(R.drawable.ic_android, "One", "televise"));
exampleList.add(new ExampleItem(R.drawable.ic_audio, "Two", "televise"));
exampleList.add(new ExampleItem(R.drawable.ic_sun, "Three", "televise"));
exampleList.add(new ExampleItem(R.drawable.ic_android, "Four", "televise"));
exampleList.add(new ExampleItem(R.drawable.ic_audio, "Five", "televise"));
exampleList.add(new ExampleItem(R.drawable.ic_sun, "Six", "televise"));
exampleList.add(new ExampleItem(R.drawable.ic_android, "Seven", "televise"));
exampleList.add(new ExampleItem(R.drawable.ic_audio, "Eight", "televise"));
exampleList.add(new ExampleItem(R.drawable.ic_sun, "Nine", "televise"));

}

private void setUpRecyclerView() {

RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);

exampleAdapter = new ExampleAdapter(exampleList);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(exampleAdapter);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.example_menu, menu);

MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();

searchView.setImeOptions(EditorInfo.IME_ACTION_DONE);

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
exampleAdapter.getFilter().filter(newText);
return false;
}
});
return true;
}
}


activity_main.xml

<?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"
tools:context=".MainActivity">

<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
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" />

</android.support.constraint.ConstraintLayout>


ExampleItem.java


public class ExampleItem {

private int imageResource;
private String text1;
private String text2;

public ExampleItem(int imageResource, String text1, String text2) {
this.imageResource = imageResource;
this.text1 = text1;
this.text2 = text2;
}

public int getImageResource() {
return imageResource;
}

public void setImageResource(int imageResource) {
this.imageResource = imageResource;
}

public String getText1() {
return text1;
}

public void setText1(String text1) {
this.text1 = text1;
}

public String getText2() {
return text2;
}

public void setText2(String text2) {
this.text2 = text2;
}

}




example_item.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 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="wrap_content"
android:layout_marginBottom="4dp"
app:cardCornerRadius="4dp">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp">

<ImageView
android:id="@+id/image_view"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="2dp" />

<TextView
android:id="@+id/text_view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/image_view"
android:text="Line 1"
android:textColor="@android:color/black"
android:textSize="20sp"
android:textStyle="bold" />

<TextView
android:id="@+id/text_view2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text_view1"
android:layout_marginStart="8dp"
android:layout_toEndOf="@+id/image_view"
android:text="Line 2"
android:textSize="15sp" />

</RelativeLayout>

</android.support.v7.widget.CardView>


'android' 카테고리의 다른 글

Remove the ActionBar  (0) 2018.10.16
TextSwitcher 사용  (0) 2018.10.16
BottomNavigationView  (0) 2018.10.04
Navigation - Part 2  (0) 2018.09.28
Navigation - Part 1  (0) 2018.09.28
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함