我有一个recyclerView,但只有当用户点击工具栏中的searchView后,才会显示项目(存储在数据库中),之后通常会显示回收站视图中的项目。我对此有点困惑。
你能帮我吗?
片段
//list init
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
database = new ArrayList<>();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_contacts, container, false);
ButterKnife.bind(this, view);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
database = SugarRecord.listAll(ContactORM.class);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mAdapter = new ContactsAdapter(getContext(), database);
recyclerView.setAdapter(mAdapter);
}
适配器
class ContactsAdapter extends RecyclerView.Adapter<ContactsAdapter.ViewHolder> {
List<ContactORM> database;
ContactORM contactORM;
Context mContext;
public ContactsAdapter(Context context, List<ContactORM> contacts) {
this.mContext = context;
this.database = contacts;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int type) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item_contact, parent, false);
return new ViewHolder(view);
}
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) {
contactORM = database.get(holder.getAdapterPosition());
if ((contactORM.getLastname() != null)) {
holder.name.setText(contactORM.getName() + " " + contactORM.getLastname());
} else {
holder.name.setText(contactORM.getName());
}
//if photo is not null, load image
if (!(contactORM.getPhoto() == null)) {
Picasso.with(mContext).load(contactORM.getPhoto()).resize(100, 100).into(holder.avatar);
}
}
@Override
public int getItemCount() {
return database.size();
}
}
XML布局
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.contacts.ContactsFragment">
<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">
<!-- starts custom searchView -->
<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>
<!-- end custom searchView -->
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
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>