M
M
mond_bond2017-03-17 21:06:54
Java
mond_bond, 2017-03-17 21:06:54

Does the adapter not call onCreateViewHolder at all?

Fragment

public class PostFragment extends BaseFragment implements PostView {
    @Inject
    PostPresenter presenter;

    private RecyclerView recyclerView;
    private long categoryId
    PostAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_post, container, false);

        recyclerView = (RecyclerView) v.findViewById(R.id.postFragmentRecycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        String s  = "word";
        ArrayList<String> array = new ArrayList<>();
        for(int i = 0; i != 20; i++) {
            array.add(s);
        }

        adapter = new PostAdapter(array);
        recyclerView.setAdapter(adapter);

        Bundle arguments = getArguments();
        categoryId = arguments.getLong(PublicActivity.CATEGORY_NAME);

        return v;
    }

The adapter itself
public class PostAdapter extends RecyclerView.Adapter {

    private ArrayList<String> arrayList;

    public PostAdapter(ArrayList<String> arrayList) {
        Log.d("ADAPTER", "constructor");
        this.arrayList = arrayList;
    }

     class MyViewHolder extends RecyclerView.ViewHolder {

        public View view;

        MyViewHolder(View itemView) {
            super(itemView);
            this.view = itemView;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.d("ADAPTER", "CREATE");
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.post_item, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        Log.d("ADAPTER", "bind");
    }

    @Override
    public int getItemCount() {
        Log.d("ADAPTER", "size = " + String.valueOf(arrayList.size()));
        return arrayList.size();
    }
}

Fragment picture
<RelativeLayout 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="exp.glorio.view.fragments.PostFragment">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/postFragmentRecycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>
</RelativeLayout>

and direct output
03-17 17:11:23.753 30538-30538/exp.glorio D/ADAPTER: constructor
03-17 17:11:23.903 30538-30538/exp.glorio D/ADAPTER: size = 20
03-17 17:11:23.903 30538-30538/exp.glorio D/ADAPTER: size = 20
03-17 17:11:23.953 30538-30538/exp.glorio D/ADAPTER: size = 20
03-17 17:11:23.953 30538-30538/exp.glorio D/ADAPTER: size = 20

As you can see from the output, the constructor and the count are called, but the binding and onCreateViewHolder are not. This is not the first time I work with an adapter and this has not happened. There are many other adapters in the application, they work.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mond_bond, 2017-03-18
@mond_bond

The problem was in the root layout of the activity. It was android.support.constraint.ConstraintLayout. Combining it with the "match_parent" option for the fragment container gave this result. If you replace the ConstraintLayout with any other - everything will work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question