A
A
arlanchik52018-03-10 22:30:25
Android
arlanchik5, 2018-03-10 22:30:25

How to make horizontal recyclerview?

Hello, I can’t make a horizontal recycler. There are two layouts, one with a recycler, the other with a cardview that contains an image. And the card should be filled with these images from the array (picasso).

public class HomeFragment extends Fragment {


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.homerecycler, container, false);
        RecyclerView recyclerView=rootView.findViewById(R.id.rec1);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL,false));
        recyclerView.setAdapter(new HorizontalAdapter(getContext()));

        return rootView;
    }
    public interface FragmentListener {
    }
    public class HorizontalAdapter extends RecyclerView.Adapter<HorizontalAdapter.HorizontalViewHolder> {
        private Context context;
        private String[] items1={"http://topmemas.top/img/img/1517414160.jpg",
                "http://topmemas.top/img/img/1518633309.jpg",
                "http://topmemas.top/img/img/1517859960.jpg",};


        public HorizontalAdapter(Context context){
            this.context=context;

        }
        @Override
        public HorizontalViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater inflater=LayoutInflater.from(parent.getContext());
            View view = inflater.inflate(R.layout.homefragment,parent,false);

            return new HorizontalAdapter.HorizontalViewHolder(view);
        }
        @Override
        public void onBindViewHolder(HorizontalAdapter.HorizontalViewHolder holder, int position) {
            holder.image1.setImageURI(Uri.parse(items1[position]));
        }


        @Override
        public int getItemCount() {
            return items1.length;

        }

        public class HorizontalViewHolder extends RecyclerView.ViewHolder{
            ImageView image1;
            public HorizontalViewHolder(View view){
                super(view);
                image1=view.findViewById(R.id.imagepopul);
                Picasso.with(context).load(items1[getPosition()]).placeholder(R.drawable.image).fit().into(image1);}

        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2018-03-11
@zagayevskiy

Picasso.with(context).load(items1[getPosition()]).placeholder(R.drawable.image).fit().into(image1);}
move to onBindViewHolder. throw out setImageUri. The rest seems to be all right.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question