使 Recyclerview 固定高度和可滚动 [英] Making Recyclerview Fixed Height and Scrollable

查看:57
本文介绍了使 Recyclerview 固定高度和可滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决了下面的检查答案...

SOLVED CHECK ANSWER BELOW...

所以我正在尝试为我的 Android 应用程序创建评论功能,我想在 recyclerview 中显示评论,然后在 recyclerview 下方有一个按钮和文本视图来添加评论.如果有很多评论,我想让 recyclerview 具有一定的高度并使其可滚动,因为我不希望用户必须向下滚动屏幕才能找到添加按钮.

So I am trying to create a comments functionality for my Android app and I want to display the comments inside a recyclerview and then have a button and textview below the recyclerview to add comments. I want to have the recyclerview a certain height and make it scrollable if there are lots of comments because I dont want the users to have to scroll down the screen to find the add button.

我无法让它工作,所以我想知道是否还有其他人遇到过这个问题.

I couldn't get it to work so I was wondering if anyone else had that issue.

我已经准备好了所有的适配器和一切,我只是在使用 recyclerview 时遇到了问题.

I have all the adapter and everything set up, I am just having trouble with the recyclerview.

谢谢.

我可能不清楚我想要完成什么.我正在尝试创建一个卡片视图,它将显示所有评论和添加新评论的能力.recyclerview 将占据大约 80% 的高度,最后 20% 用于编辑文本和按钮.

I prob was not clear on what I was trying to accomplish. I am trying to create a cardview where it will display all the comments and the ability to add a new comment. The recyclerview will take up approx 80% of the height and then the last 20% is for the edittext and button.

我的 XML(滚动到 recyclerview 所在的最后一个 cardview)

My XML (Scroll to last cardview where the recyclerview is)

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/scrollview">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ProfilePageActivity"
    >

    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/profilepagetoolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        android:minHeight="?attr/actionBarSize">

    </android.support.v7.widget.Toolbar>
    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:layout_marginTop="35dp"
        android:layout_below="@+id/profilepagetoolbar"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:id="@+id/aboutCard">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:gravity="center_vertical"
            android:orientation="vertical"
            android:layout_alignTop="@+id/aboutCard"
            android:focusable="true"
            android:focusableInTouchMode="true">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="4dp"
                android:layout_marginEnd="10dp"
                android:layout_marginStart="10dp"
                android:layout_marginTop="-60dp"
                android:gravity="center_vertical"
                android:maxLines="1"
                android:textColor="@color/text"
                android:textSize="20sp"
                android:text="ABOUT" />

            <View
                android:layout_width="match_parent"
                android:layout_height="1px"
                android:background="@color/dividers" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:paddingTop="10dp">

                <ImageView
                    android:id="@+id/nameicon"
                    android:layout_width="24dp"
                    android:layout_height="24dp"
                    android:layout_margin="8dp"
                    android:transitionName="appIcon"
                    android:background="@drawable/ic_account_circle_black_24dp"/>
                <TextView
                    android:id="@+id/Name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="4dp"
                    android:layout_marginEnd="10dp"
                    android:layout_marginStart="10dp"
                    android:layout_marginTop="8dp"
                    android:gravity="center_vertical"
                    android:maxLines="1"
                    android:textColor="@color/secondary"
                    android:textSize="20sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:paddingTop="10dp">

                <ImageView
                    android:id="@+id/locationicon"
                    android:layout_width="24dp"
                    android:layout_height="24dp"
                    android:layout_margin="8dp"
                    android:transitionName="appIcon"
                    android:background="@drawable/ic_map_black_24dp"/>
                <TextView
                    android:id="@+id/Location"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="4dp"
                    android:layout_marginEnd="10dp"
                    android:layout_marginStart="10dp"
                    android:layout_marginTop="11dp"
                    android:gravity="center_vertical"
                    android:maxLines="1"
                    android:textColor="@color/secondary"
                    android:textSize="15sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:paddingTop="10dp">

                <ImageView
                    android:id="@+id/websiteIcon"
                    android:layout_width="24dp"
                    android:layout_height="24dp"
                    android:layout_margin="8dp"
                    android:transitionName="appIcon"
                    android:background="@drawable/ic_explore_black_24dp"/>
                <TextView
                    android:id="@+id/Website"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="4dp"
                    android:layout_marginEnd="10dp"
                    android:layout_marginStart="10dp"
                    android:layout_marginTop="11dp"
                    android:gravity="center_vertical"
                    android:maxLines="1"
                    android:textColor="@color/secondary"
                    android:textSize="15sp" />
            </LinearLayout>

        </LinearLayout>
    </android.support.v7.widget.CardView>
    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:layout_marginTop="35dp"
        android:layout_below="@+id/aboutCard"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:id="@+id/writeComment"
        android:layout_alignParentTop="false"
        android:layout_alignParentBottom="false">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:gravity="center_vertical"
            android:orientation="vertical"
            android:focusable="true"
            android:focusableInTouchMode="true">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="4dp"
                android:layout_marginEnd="10dp"
                android:layout_marginStart="10dp"
                android:layout_marginTop="-100dp"
                android:gravity="center_vertical"
                android:maxLines="1"
                android:textColor="@color/text"
                android:textSize="20sp"
                android:text="Comments" />

            <View
                android:layout_width="match_parent"
                android:layout_height="1px"
                android:background="@color/dividers"
                android:id="@+id/divider"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:weightSum="1">

                <EditText
                    android:layout_width="244dp"
                    android:layout_height="wrap_content"
                    android:id="@+id/editComment"
                    android:layout_below="@+id/divider"
                    android:textColor="@color/text"
                    android:hint="Write a comment..."/>

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Create"
                    android:id="@+id/btnComment"
                    android:layout_gravity="center_horizontal" />
            </LinearLayout>
        </LinearLayout>
    </android.support.v7.widget.CardView>
    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        android:layout_marginTop="20dp"
        android:layout_below="@+id/writeComment"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:id="@+id/commentsCard">

        <LinearLayout
            android:layout_alignParentTop="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="4dp"
                android:layout_marginEnd="10dp"
                android:layout_marginStart="10dp"
                android:layout_marginTop="0dp"
                android:gravity="center_vertical"
                android:maxLines="1"
                android:textColor="@color/text"
                android:textSize="20sp"
                android:text="Comments" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/commentsList"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:focusableInTouchMode="true" />

        </LinearLayout>
    </android.support.v7.widget.CardView>
</RelativeLayout>
</ScrollView>

第二条和第一条评论被截断.

推荐答案

所以我的问题是由于某种原因,Recyclerview 没有 wrap_contents.我做了一些研究(感谢 stackoverflow),发现很多人都遇到了这个问题,他们发布了这个问题的解决方案.

So what my problem was that for some reason, Recyclerview didnt wrap_contents. I did some research (thanks stackoverflow) and found out a lot of people were having this issue and they posted solutions for this issue.

基本上,我不得不使用定制的线性布局管理器来解决这个问题.

Basically, I had to use a customized linearlayoutmanger to fix the issue.

我会发布他们发布的解决方案以及指向他们问题的链接.感谢任何试图提供帮助的人,我很感激.

I will post the solution they posted and links to their questions. thanks for whoever tried to help, I appreciate it.

这是我需要的额外文件.然后我不得不将我的 recyclerview 设置为使用这种布局而不是默认布局.

This is the extra file I needed. And then I had to set my recyclerview to use this layout instead of the default one.

public class MyLinearLayoutManager extends LinearLayoutManager {

public MyLinearLayoutManager(Context context, int orientation, boolean reverseLayout)    {
    super(context, orientation, reverseLayout);
}

private int[] mMeasuredDimension = new int[2];

@Override
public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state,
                      int widthSpec, int heightSpec) {
    final int widthMode = View.MeasureSpec.getMode(widthSpec);
    final int heightMode = View.MeasureSpec.getMode(heightSpec);
    final int widthSize = View.MeasureSpec.getSize(widthSpec);
    final int heightSize = View.MeasureSpec.getSize(heightSpec);
    int width = 0;
    int height = 0;
    for (int i = 0; i < getItemCount(); i++) {
        measureScrapChild(recycler, i,
                View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                mMeasuredDimension);

        if (getOrientation() == HORIZONTAL) {
            width = width + mMeasuredDimension[0];
            if (i == 0) {
                height = mMeasuredDimension[1];
            }
        } else {
            height = height + mMeasuredDimension[1];
            if (i == 0) {
                width = mMeasuredDimension[0];
            }
        }
    }
    switch (widthMode) {
        case View.MeasureSpec.EXACTLY:
            width = widthSize;
        case View.MeasureSpec.AT_MOST:
        case View.MeasureSpec.UNSPECIFIED:
    }

    switch (heightMode) {
        case View.MeasureSpec.EXACTLY:
            height = heightSize;
        case View.MeasureSpec.AT_MOST:
        case View.MeasureSpec.UNSPECIFIED:
    }

    setMeasuredDimension(width, height);
}

private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,
                               int heightSpec, int[] measuredDimension) {
    View view = recycler.getViewForPosition(position);
    if (view != null) {
        RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
        int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,
                getPaddingLeft() + getPaddingRight(), p.width);
        int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,
                getPaddingTop() + getPaddingBottom(), p.height);
        view.measure(childWidthSpec, childHeightSpec);
        measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin;
        measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin;
        recycler.recycleView(view);
    }
}
}

在那之后.改变这个:

 mProductsRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));

为此:

mRecyclerView.setLayoutManager(new MyLinearLayoutManager(getApplicationContext(),1,false));

三个新参数(Context, int Orientation, boolean reverse);基本上,我为方向设置了 1,因此它垂直显示,而为反向设置为 false,因此它显示了它在我的列表中的排序方式.

Three new paramets(Context, int Orientation, boolean reverse); basically, i put 1 for orientation so it shows vertiacally, and false for revers so it shows up how it is ordered in my List.

链接到与我有相同问题的其他人的问题.嵌套的 Recycler 视图高度不会包裹其内容

Links to other people's question with same problem as me. Nested Recycler view height doesn't wrap its content

嵌套的 Recycler 视图高度不包裹其内容

再次感谢各位.希望这对其他人有帮助

Thanks once again guys. Hope this helps someone else

这篇关于使 Recyclerview 固定高度和可滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆