A
A
arlanchik52018-03-12 22:48:03
Java
arlanchik5, 2018-03-12 22:48:03

Where is the error in the code, why is this happening (android studio)?

Hello, why are cards with image not created side by side, but always at the very beginning? I use recycler, cardview, picasso.

public class Home extends Fragment {
    private ArrayList<String> imageurl = new ArrayList<>();
    private RecyclerView recyclerView;
    @Nullable
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View rootView =inflater.inflate(R.layout.homefragment, container, false);
            recyclerView=rootView.findViewById(R.id.recyclerhome);
            getImages();
            return rootView;
        }
    private void getImages(){
        imageurl.add("https://pp.userapi.com/c639616/v639616425/47bd3/iqmvzRCNBm4.jpg");
        imageurl.add("https://pp.userapi.com/c639616/v639616425/47bd3/iqmvzRCNBm4.jpg");
        initRecyclerView();
    }
    private void initRecyclerView(){
        LinearLayoutManager layoutManager=new LinearLayoutManager(getContext(),LinearLayoutManager.HORIZONTAL,false);
        recyclerView.setLayoutManager(layoutManager);
        RecyclerPopAdapter popAdapter= new RecyclerPopAdapter(imageurl,getContext());
        recyclerView.setAdapter(popAdapter);
    }
}

public class RecyclerPopAdapter extends RecyclerView.Adapter<RecyclerPopAdapter.ViewHolder> {
    private static final String TAG= "RecyclerPopAdapter";
    private ArrayList<String> image = new ArrayList<>();
    private Context context;

    RecyclerPopAdapter(ArrayList<String> image, Context context) {
        this.image= image;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.d(TAG,"onCreateViewHolder: called.");
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.card_popul,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Log.d(TAG,"onBindViewHolder: called.");
        Picasso.with(context).load(image.get(position)).into(holder.image);
        holder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

    }
    @Override
    public int getItemCount() {
        return image.size();
    }
    class ViewHolder extends RecyclerView.ViewHolder{
        ImageView image;
         ViewHolder(View itemView) {
            super(itemView);
            image= itemView.findViewById(R.id.imagepopul);
        }
    }
}

5aa6d9d098b44110339438.jpeg5aa6d9e019828329423786.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2018-03-13
@arlanchik5

The problem is in android:layout_width="match_parent" in RelativeLayout.
Other problems:
RelativeLayout is not needed here, it is too heavy. Nothing is needed here at all, cardview is enough.
LinearLayout with one element is not needed, it's an oxymoron.
android:orientation="horizontal" is not needed for the recycler.
android:textSize needs to be specified in sp, of course.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question