不调用适配器方法

时间:2016-05-29 08:16:17

标签: android android-recyclerview

我在片段中有一个回收者视图。该列表显示为空。试图记录适配器方法(onCreateViewHolder和onBindViewHolder),事实证明它们没有被调用!

我知道他们大多都不会打电话,因为getItemCount会返回0.但是我在getItemCount中检查以下内容,只有当它不为空且&&&&&& amp;& amp; ;它的大小大于0.而顺便说一下,片段中的列表不是空的。

适配器代码:

public class SearchOfflineAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<Question> mSearchList;

    @Inject
    public SearchOfflineAdapter() {
        mSearchList = new ArrayList<>();
    }

    // Set questions list
    public void setSearchOfflineList(List<Question> searchList) {
        mSearchList = searchList;
        notifyDataSetChanged();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Timber.d("iiiiiiiiiiiiiiiii","onCreateViewHolder");
                View itemView = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_search_entries, parent, false);
        SearchEntriesViewHolder    searchEntriesViewHolder = new SearchEntriesViewHolder(itemView);
        return searchEntriesViewHolder;


    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        Timber.d("iiiiiiiiiiiiiiiii","onBindViewHolder");

        ((SearchEntriesViewHolder) holder).setItemData(mSearchList.get(position), holder);

    }

    @Override
    public int getItemCount() {
        Log.d("getITemCount hhhh", "===");

        return ( mSearchList!= null && mSearchList.size() > 0) ? mSearchList.size() : -1;

    }

    class SearchEntriesViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.text_view_search_entry_title)
        TextView tvSearchEntryTitle;
        @Bind(R.id.edit_text_search_entry)
        EditText txtSearchEntry;

        public SearchEntriesViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void setItemData(Question question, RecyclerView.ViewHolder viewHolder) {
            tvSearchEntryTitle.setText(question.getQas());

        }
    }
}

将适配器设置为recyclerview代码段:

mSearchEntriesAdapter.setSearchOfflineList(questionList);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(mSearchEntriesAdapter);

0 个答案:

没有答案