Android - how to change Recyclerview height dynamically?

前端 未结 5 769
天涯浪人
天涯浪人 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条回答
  • 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);
                }
            }
        });
    
    }
    
    0 讨论(0)
  • 2020-12-28 17:27

    Although the question was asked quite some time ago, I figured some might find this answer helpful.

    I have a RecyclerView with adapter. The height is set in onBindViewHolder method of the adapter:

    Layout:

      <android.support.v7.widget.RecyclerView
      android:id="@+id/container"
      ...
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>
    

    Adapter:

    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    
    public abstract static class ViewHolder extends RecyclerView.ViewHolder {
         public ViewHolder(View itemView) {
            super(itemView);
        }
        public abstract void setFixedHeight();
    }
    
    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
    
        return new ViewHolder(view) {
    
            @Override
            public void setFixedHeight() {
                //  magic happening here
                ViewGroup.LayoutParams parentParams = parent.getLayoutParams();
                parentParams.height =
                    ((RecyclerView) parent).computeVerticalScrollRange()
                        + parent.getPaddingTop()
                        + parent.getPaddingBottom();
                parent.setLayoutParams(parentParams);
            }
        }; 
    }
    
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.setFixedHeight();
        }
    
    // other methods here
    }
    

    Setting adapter:

    recyclerView.setAdapter(new MyAdapter(...));

    Note: Use((RecyclerView) parent).computeHorizontalScrollRange() with horizontal scroll

    0 讨论(0)
  • 2020-12-28 17:35

    If you just want your recycler view to size automatically as per number of items then, why don't you put RecyclerView height as wrap_content.

    If you have multiple ScrollView in layout then try wrapping RecyclerView in NestScrollView and set it's height as wrap_content too.

    code :

    <android.support.v7.widget.RecyclerView
                    android:id="@+id/recyclerView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    />
    
    0 讨论(0)
  • 2020-12-28 17:37

    This code works I am sure about it

    ViewGroup.LayoutParams params=recyclerview.getLayoutParams();
    params.height=100;
    recyclerview.setLayoutParams(params);
    

    The other thing you could do is make a linear layout as the parent of the recycler view and then increase the height dynamically for the parent view

    Consider the following XML below

    <LinearLayout 
          android:id="@+id/yourLayoutId">
    
        <RecyclerView android:width="match_parent" android:height="wrap_content">
        </RecyclerView>
    
    </LinearLayout
    

    Consider the following code

    LinearLayout layout = (LinearLayout)findViewById(R.id.yourLayoutId);
    LinearLayout.LayoutParams lp = (LayoutParams) layout.getLayoutParams();
    lp.height = 100;
    
    0 讨论(0)
  • 2020-12-28 17:42

    You should use LayoutParams of parent's view in setLayoutParams(params).

    For example:

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <android.support.v7.widget.RecyclerView
            android:id="@+id/images"
            android:layout_width="wrap_content"
            android:layout_height="360dp"
            >
        </android.support.v7.widget.RecyclerView>
     </Relativelayout>
    

    Change LayoutParams in the code.

      RelativeLayout.LayoutParams lp =
                new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 500);
     recyclerView.setLayoutParams(lp);
    
    0 讨论(0)
提交回复
热议问题