Willey3x37
Willey3x37

Reputation: 310

Why is my RecyclerViewAdapter not showing in View?

I tried to migrate my Chat Bot from ScrollView to a RecyclerView for performance, but unfortunally every method in my Adapter is called correctly but nothing is showed.

My Custom RecyclerView Adapter:

public class ChatViewAdapter extends RecyclerView.Adapter<ChatViewAdapter.ChatViewHolder> {
    private LinkedList<ChatBubbleModel> bubbles;
    private ViewGroup group;

    public ChatViewAdapter() {
        this(new LinkedList<ChatBubbleModel>());
    }

    public ChatViewAdapter(LinkedList<ChatBubbleModel> bubbles) {
        this.bubbles = bubbles;
    }

    public LinkedList<ChatBubbleModel> getBubbles() {
        return bubbles;
    }

    public void addBubble(ChatBubbleModel bubble) {
        this.bubbles.add(bubble);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ChatViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        this.group = parent;
        return new ChatViewHolder(new TextView(parent.getContext()));
    }

    @Override
    public void onBindViewHolder(@NonNull ChatViewHolder holder, int position) {
        final ChatBubbleModel instance = this.bubbles.get(position);
        if (instance.getUserType() == ChatBubbleModel.UserType.USER) {
            holder.setTextView(new RightChatBubble(instance.getOwner(), instance.getMessage(), group.getContext()));
        } else {
            holder.setTextView(new LeftChatBubble(instance.getOwner(), instance.getMessage(), group.getContext()));
        }
    }

    @Override
    public int getItemCount() {
        return this.bubbles.size();
    }

    public static class ChatViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        private TextView textView;

        public ChatViewHolder(TextView v) {
            super(v);
            textView = v;
        }

        public void setTextView(TextView view) {
            this.textView = view;
        }

    }
}

And in the Fragmnet where I use it:

final View root = inflater.inflate(R.layout.fragment_chatview, container, false);
        chatView = root.findViewById(R.id.chatView);
        chatView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        chatView.setLayoutManager(layoutManager);
        this.chatAdapter = new ChatViewAdapter(new LinkedList<ChatBubbleModel>());
        chatView.setAdapter(chatAdapter);

The Impl of the View I use:

public class LeftChatBubble extends androidx.appcompat.widget.AppCompatTextView {

    private final static int leftRightPadding = 50;
    private final static int topBottomPadding = 20;

    public LeftChatBubble(Context context) {
        this(context, null, -1);

    }

    @SuppressLint("SetTextI18n")
    public LeftChatBubble(String owner, String text, Context context) {
        super(context);
        setText(owner + "\n" + text);
        setBackground(ContextCompat.getDrawable(context, R.drawable.inset));

        setPadding(pixelToDp(leftRightPadding, context), pixelToDp(topBottomPadding, context), pixelToDp(leftRightPadding, context), pixelToDp(topBottomPadding, context));
        setTextAlignment(TEXT_ALIGNMENT_VIEW_START);
        setTextColor(Color.WHITE);

    }

    public LeftChatBubble(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public LeftChatBubble(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setBackground(ContextCompat.getDrawable(context, R.drawable.ic_chatbubleleft));

        setPadding(pixelToDp(leftRightPadding, context), pixelToDp(topBottomPadding, context), pixelToDp(leftRightPadding, context), pixelToDp(topBottomPadding, context));
        setTextAlignment(TEXT_ALIGNMENT_VIEW_START);
        setTextColor(Color.WHITE);


    }

    private int pixelToDp(int px, Context context) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (px * scale + 0.5f);

    }


}

Does anyone have an Idea why there is nothing showed on the device? The Used Layout is correct bc it worked before.

Upvotes: 0

Views: 52

Answers (2)

Willey3x37
Willey3x37

Reputation: 310

The Problem was that I not created the Correct View Class in onCreate Mathod of Adapter... But thank you all for your helpful answers.

Upvotes: 0

Damilola Omoyiwola
Damilola Omoyiwola

Reputation: 116

You did not seem to inflate your item view in the onCreateViewHolder like

@NonNull
@Override
public ChatViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    this.group = parent;
    LayoutInflater inflater = LayoutInflater.from(this);
    View view = inflater.inflate(R.layout.item_view, parent, false);
    return new ChatViewHolder(view);
}

Also, change your ViewHolder args to be a View.

Upvotes: 1

Related Questions