D
D
DARKENN2020-07-17 17:40:10
Java
DARKENN, 2020-07-17 17:40:10

Why doesn't CarouselLayoutManager display items?

If I do it with a regular recyclerview, then everything is displayed correctly, but if I use the CarouselLayoutManager library, then nothing is displayed on the screen.

Activity code

private List<String> keyList = new ArrayList<>();
private List<Winner> result_winners = new ArrayList<>();

winner_adapter = new WinnerAdapter(result_winners);
        CarouselLayoutManager layoutManager = new CarouselLayoutManager(CarouselLayoutManager.HORIZONTAL, true);
        layoutManager.setPostLayoutListener(new CarouselZoomPostLayoutListener());
        layoutManager.setMaxVisibleItems(2);
        winners_recycler = findViewById(R.id.winners_recycler);
        winners_recycler.setLayoutManager(layoutManager);
        winners_recycler.setHasFixedSize(true);
        winners_recycler.setAdapter(winner_adapter);
        winners_recycler.addOnScrollListener(new CenterScrollListener());

    private void updateList() {
        database.getReference("winners").addChildEventListener(new ChildEventListener() {
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                result_winners.add(dataSnapshot.getValue(Winner.class));
                winner_adapter.notifyDataSetChanged();
                keyList.add(dataSnapshot.getKey());
            }
            public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
            }
            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
                int index = keyList.indexOf(dataSnapshot.getKey());
                result_winners.remove(index);
                keyList.remove(index);
                winner_adapter.notifyDataSetChanged();
            }
            public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
            }
            public void onCancelled(@NonNull DatabaseError databaseError) {
            }
        });
    }


Adapter
public class WinnerAdapter extends Adapter<WinnerAdapter.WinnerViewHolder> {
    private List<Winner> list;
    class WinnerViewHolder extends ViewHolder {
        ImageView image;
        TextView name;
        public WinnerViewHolder(View itemView) {
            super(itemView);
            this.name = itemView.findViewById(R.id.name);
            this.image = itemView.findViewById(R.id.image);
        }
    }
    public WinnerAdapter(List<Winner> list) {
        this.list = list;
    }
    public WinnerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new WinnerViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.winner_model_top, parent, false));
    }
    public void onBindViewHolder(WinnerViewHolder holder, int position) {
        Winner winner = list.get(position);
        Picasso.get()
                .load(winner.photo_profile)
                .transform(new CircleTransform())
                .into(holder.image);
        holder.name.setText(winner.name);
    }
    public int getItemCount() {
        return this.list.size();
    }
}

item
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/image"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentStart="true"
        app:srcCompat="@drawable/ic_default_avatar_1" />

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/image"
        android:layout_alignStart="@+id/image"
        android:layout_alignEnd="@+id/image"
        android:layout_marginStart="0dp"
        android:fontFamily="sans-serif-medium"
        android:textAlignment="center"
        android:textColor="@color/white"
        android:textSize="16sp" />
</RelativeLayout>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DARKENN, 2020-07-25
@DARKENN

This library requires a fixed size Recycler View

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question