1.创建一个学生类,保存姓名和年龄信息
package com.zmm.recyclerdynamicsearch;
/**
* Description:
* Author:zhangmengmeng
* Date:2017/4/1
* Time:下午1:11
*/
public class StudentBean {
private String name;
private int age;
public StudentBean(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
2.RecyclerView 的Adapter
package com.zmm.recyclerdynamicsearch;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.List;
/**
* Description:
* Author:zhangmengmeng
* Date:2017/4/1
* Time:上午11:57
*/
public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.SearchHolder> {
private List<StudentBean> mStudentBeanList;
private OnItemClickListener mOnItemClickListener;
public interface OnItemClickListener{
void OnItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
mOnItemClickListener = onItemClickListener;
}
public SearchAdapter(List<StudentBean> studentBeanList) {
mStudentBeanList = studentBeanList;
}
@Override
public SearchHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = View.inflate(parent.getContext(), R.layout.item_search, null);
return new SearchHolder(view);
}
@Override
public void onBindViewHolder(SearchHolder holder, final int position) {
holder.setData(position);
holder.item_ll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mOnItemClickListener != null){
mOnItemClickListener.OnItemClick(position);
}
}
});
}
@Override
public int getItemCount() {
return mStudentBeanList.size();
}
class SearchHolder extends RecyclerView.ViewHolder{
private TextView item_name,item_age;
private LinearLayout item_ll;
public SearchHolder(View itemView) {
super(itemView);
item_name = (TextView) itemView.findViewById(R.id.item_name);
item_age = (TextView) itemView.findViewById(R.id.item_age);
item_ll = (LinearLayout) itemView.findViewById(R.id.item_ll);
}
public void setData(int position) {
item_name.setText(mStudentBeanList.get(position).getName());
item_age.setText(mStudentBeanList.get(position).getAge()+"");
}
}
}
3.布局文件:
3.1
<?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="horizontal"
android:background="#00ffff"
android:id="@+id/item_ll"
android:padding="10px">
<TextView
android:id="@+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="张三"
android:textSize="20sp"/>
<TextView
android:id="@+id/item_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100px"
android:text="13"
android:textSize="20sp"/>
</LinearLayout>
3.2
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@drawable/top_background"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center_vertical">
<EditText
android:id="@+id/et_search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:background="@drawable/search_frame"
android:hint="Searching..."
android:paddingLeft="32dp"
android:singleLine="true"
android:textSize="12sp"/>
<ImageView
android:id="@+id/iv_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:paddingRight="20dp"
android:src="@drawable/delete"
android:visibility="gone"/>
</RelativeLayout>
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
4.MainActivity
package com.zmm.recyclerdynamicsearch;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
public class MainActivity extends AppCompatActivity {
@InjectView(R.id.et_search)
EditText mEtSearch;
@InjectView(R.id.iv_delete)
ImageView mIvDelete;
@InjectView(R.id.recycler_view)
RecyclerView mRecyclerView;
private List<StudentBean> mStudentBeanList;
private List<StudentBean> mStudentBeanList2;
private SearchAdapter mSearchAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
initView();
}
private void initView() {
initData();
editTextSearchListener();
}
private void initData() {
if(mStudentBeanList != null && mStudentBeanList.size()>0){
mStudentBeanList.clear();
mStudentBeanList = null;
}
mStudentBeanList = new ArrayList<>();
//创建list2的目的相当于数据库,里面存储全部数据
mStudentBeanList2 = new ArrayList<>();
for (int i = 0; i < 20; i++) {
StudentBean studentBean = new StudentBean("Giousa_" + i, i + 10);
mStudentBeanList.add(studentBean);
mStudentBeanList2.add(studentBean);
}
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(layoutManager);
mSearchAdapter = new SearchAdapter(mStudentBeanList);
mRecyclerView.setAdapter(mSearchAdapter);
mSearchAdapter.setOnItemClickListener(new SearchAdapter.OnItemClickListener() {
@Override
public void OnItemClick(int position) {
Toast.makeText(MainActivity.this,mStudentBeanList.get(position).getName(),Toast.LENGTH_SHORT).show();
}
});
}
private void editTextSearchListener() {
mEtSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if(s.length() == 0){
mIvDelete.setVisibility(View.GONE);
}else {
mIvDelete.setVisibility(View.VISIBLE);
}
search();
}
});
}
private void search() {
String data = mEtSearch.getText().toString().trim();
mStudentBeanList.clear();
for (int i = 0; i < mStudentBeanList2.size(); i++) {
StudentBean studentBean = mStudentBeanList2.get(i);
String ageStr = studentBean.getAge()+"";
//原理很简单,只要检索的字符,被数据库包含,即可展示出来
if(studentBean.getName().contains(data) || ageStr.contains(data)){
mStudentBeanList.add(studentBean);
}
}
mSearchAdapter.notifyDataSetChanged();
}
@OnClick(R.id.iv_delete)
public void onClick() {
mEtSearch.setText("");
}
}