D
D
dimasik1002002017-11-28 20:31:07
Java
dimasik100200, 2017-11-28 20:31:07

How to write adapter for CardView?

For example, I have a card.
5a1d9c326dd5c506134654.png
However, when the application starts, half of the content does not appear. How to add the adapter correctly so that everything appears?
5a1d9d3f9edfa962026543.jpeg
Here is the full code of the fragment where the card should be located

package com.example.asus.testapp;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * A simple {@link Fragment} subclass.
 */
public class NewsFragment extends Fragment {
    public NewsFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        RecyclerView recyclerView = (RecyclerView) inflater.inflate(
                R.layout.recycler_view, container, false);
        ContentAdapter adapter = new ContentAdapter(recyclerView.getContext());
        recyclerView.setAdapter(adapter);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        return recyclerView;
    }
    // toolbar
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true); // This Fragment has a menu
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
        inflater.inflate(R.menu.menu_news, menu); // Inflate the Fragment's menu
        super.onCreateOptionsMenu(menu, inflater);
    }
    // recycler
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
            super(inflater.inflate(R.layout.card_feed, parent, false));
        }
    }
    public static class ContentAdapter extends RecyclerView.Adapter<ViewHolder> {
        // Установим количество элементов списка в RecyclerView.
        private static final int LENGTH = 18;

        public ContentAdapter(Context context) {
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new ViewHolder(LayoutInflater.from(parent.getContext()), parent);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            // no-op
        }

        @Override
        public int getItemCount() {
            return LENGTH;
        }
    }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2017-11-29
@klim76

it 's all screwed up here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question