2016-09-05 2 views
0

У меня есть recyclerView и searchView в моем приложении. Кто-нибудь знает, как возможно, что никакие предметы не отображаются в recyclerView, но когда я нажимаю (задавать фокус) на searchView, элемент отображается? Я попробовал отладчик, и когда я нажимаю на searchView, начинается onBindViewHolder.Проблемы с recyclerView

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 

ут - в основном, это стиль 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

ok, добавил код – Stepan

+0

, пожалуйста, проверьте значение базы данных, он может быть равен нулю, пока вы создаете рецикловый вид. –

ответ

0

Попробуйте notifyDataSetChanged() после установки adapter в recyclerView в OnViewCreated() методом.

+0

@ Степан проверяет, работает ли это – Nikhil

Смежные вопросы