Android - how to change Recyclerview height dynamically?

前端 未结 5 789
天涯浪人
天涯浪人 2020-12-28 16:36

I\'m stuck with an issue about changing Recycler height based on its total items. What I have tried is to use Layout Param like this:

        ViewGroup.Layo         


        
5条回答
  •  萌比男神i
    2020-12-28 17:23

    I tried this. It worked. May be help.

    @Override
    public void onBindViewHolder(FeedListRowHolder feedListRowHolder, int i) {
    
        //this change height of rcv
         LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
         params.height =80; //height recycleviewer
         feedListRowHolder.itemView.setLayoutParams(params);
    
        FeedItem feedItem = feedItemList.get(i);
    
        Picasso.with(mContext).load(feedItem.getThumbnail())
                .error(R.drawable.placeholder)
                .placeholder(R.drawable.placeholder)
                .into(feedListRowHolder.thumbnail);
    
        feedListRowHolder.title.setText(Html.fromHtml(feedItem.getTitle()));
        feedListRowHolder.itemView.setActivated(selectedItems.get(i, false));
    
        feedListRowHolder.setClickListener(new FeedListRowHolder.ClickListener() {
            public void onClick(View v, int pos, boolean isLongClick) {
                if (isLongClick) {
    
                    // View v at position pos is long-clicked.
                    String poslx = pos + "";
                    Toast.makeText(mContext, "longclick " + poslx, Toast.LENGTH_SHORT).show();
    
                } else {
                    // View v at position pos is clicked.
                    String possx = pos + "";
                    Toast.makeText(mContext, "shortclick " + possx, Toast.LENGTH_SHORT).show();
                    toggleSelection(pos);
                }
            }
        });
    
    }
    

提交回复
热议问题