0

I have a recyclerView and searchView in my app. Does anybody know how it is possible that no items are shown in recyclerView but when I click (set focus) to searchView, the item is shown? I tried debugger and when i click to the searchView, onBindViewHolder starts.

RecyclerView Adapter

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();
    }

}

fragment layout

 @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);

    }

Search method

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 - basically, it is styled editText - I had to implement is that way

 <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>
Berriel
  • 8,463
  • 3
  • 25
  • 54
Stepan
  • 797
  • 2
  • 20
  • 30

1 Answers1

0

Try notifyDataSetChanged() after setting adapter to recyclerView in OnViewCreated() method.

Nikhil
  • 3,493
  • 7
  • 30
  • 43