我的应用中有recyclerView
和searchView
。有人知道recyclerView
中没有显示任何项目的可能性,但当我点击(设置焦点)到searchView
时,会显示该项目吗?我尝试了调试器,当我点击searchView
时,onBindViewHolder
启动。
RecyclerView适配器
class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
List<Contacts> contacts;
Context mContext;
public MyAdapter() {
}
public MyAdapter(Context context, List<Contacts> contact) {
this.mContext = context;
this.contacts = contact;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int type) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item_contact, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
public class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.avatar)
CircleImageView avatar;
@BindView(R.id.name)
TextView name;
public ViewHolder(View rowView) {
super(rowView);
ButterKnife.bind(this, rowView);
}
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
Contacts contact = contacts.get(position);
if ((contact.getLastname() != null)) {
holder.name.setText(contact.getName() + " " + contact.getLastname());
} else {
holder.name.setText(contact.getName());
}
//if photo is not null, load image
if (!(contact.getPhoto() == null)) {
Picasso.with(mContext).load(contact.getPhoto()).resize(100, 100).into(holder.avatar);
}
}
@Override
public int getItemCount() {
return contacts.size();
}
}
片段布局
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
((AccountActivity) getActivity()).getSupportActionBar().setTitle(R.string.title_contacts);
Contacts contacts = new Contacts();
contacts.setName("name");
database.add(contacts);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mAdapter = new MyAdapter(getContext(), database);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(mAdapter);
}
搜索方法
public void searchViewFilter() {
search.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence query, int start, int before, int count) {
dataSource.clear();
query = query.toString().toLowerCase();
for (int i = 0; i < contactList.size(); i++) {
final String text = (contactList.get(i).getName().toLowerCase());
if (text.contains(query)) {
dataSource.add(contactList.get(i));
}
}
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mAdapter = new MyAdapter(getActivity(), dataSource);
recyclerView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
Layo
ut - 基本上,它的样式是editText - 我必须以那种方式实现
<android.support.design.widget.CoordinatorLayout
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:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="horizontal"
app:layout_scrollFlags="scroll|enterAlways">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginTop="11dp"
android:src="@drawable/ic_search_white_24px"/>
<EditText
android:id="@+id/search"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="9"
android:hint="@string/search_contact"
android:textColor="@android:color/white"
android:textSize="18sp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<ImageButton
android:id="@+id/ib_clearText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_marginTop="11dp"
android:background="@color/colorPrimary"
android:src="@drawable/ic_clear_white_18px"/>
</LinearLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<!-- floating action button -->
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="20dp"
android:src="@drawable/ic_add_white_24px"/>
</android.support.design.widget.CoordinatorLayout>
答案 0 :(得分:0)
在notifyDataSetChanged()
方法中将adapter
设置为recyclerView
后尝试OnViewCreated()
。