LinearLayout中的RecyclerView以及其他几个视图

时间:2016-05-04 10:11:35

标签: android android-layout android-recyclerview

我在LinearLayout中有一个recyclerView。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/book_detail_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.BookDetailActivity">
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:orientation="vertical">

        <ImageView
            android:layout_width="250dp"
            android:layout_height="270dp"
            android:id="@+id/book_image"
            android:layout_marginTop="@dimen/activity_vertical_margin"
            android:layout_gravity="center"
            android:src="@drawable/book_default"/>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dip"
            android:layout_marginTop="@dimen/activity_vertical_margin"
            android:background="#EEEEEE"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:id="@+id/book_authors"
            android:layout_marginTop="@dimen/activity_vertical_margin"
            android:fontFamily="sans-serif-condensed" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
            <RatingBar
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                style="?android:attr/ratingBarStyleSmall"
                android:max="5"
                android:id="@+id/book_rating"
                android:stepSize="0.5"
                android:paddingTop="10dp"
                android:isIndicator="true"
                android:progressTint="#FFA500"
                android:secondaryProgressTint="#D1D6D8"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingTop="10dp"
                android:paddingLeft="5dp"
                android:id="@+id/book_rating_text"
                android:fontFamily="sans-serif-condensed" />
        </LinearLayout>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:id="@+id/book_reviews_text"
            android:textColor="@color/redColor"
            android:fontFamily="sans-serif-condensed" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dip"
            android:layout_marginTop="@dimen/activity_vertical_margin"
            android:background="#EEEEEE"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:fadingEdge="vertical"
            android:fadingEdgeLength="20dp"
            android:paddingTop="@dimen/activity_vertical_margin"
            android:id="@+id/book_description"
            android:onClick="toggleText"
            android:ellipsize="end"
            android:maxLines="3" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dip"
            android:id="@+id/book_description_below_line"
            android:layout_marginTop="@dimen/activity_vertical_margin"
            android:background="#EEEEEE"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/dark_translucent_black"
            android:textAlignment="center"
            android:textSize="15dp"
            android:layout_marginTop="@dimen/activity_vertical_margin"
            android:text="People near you who are willing to lend this book"/>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/lender_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="400dp"
            android:scrollbars="vertical" />
    </LinearLayout>
</ScrollView>

问题在于,当我给这个rec​​yclerView充气时,完整的活动不会滚动(只有回收者视图的一部分)。此外,我已指定recyclerView的高度,否则我无法看到它。

所以我的两个问题是:

  1. 如何动态地提供此RecyclerView高度(layout_height: wrap_content无效)
  2. 当内容更多时,如何滚动整个活动(而不仅仅是recyclelerView部分)。
  3. PS我是Android的新手,虽然我之前已经实现了recyclerView,但在这些情况下只有一个元素(父级LinearLayout中有一个recyclelerView)

1 个答案:

答案 0 :(得分:0)

ExpandableHeightListView.java

import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ListView;
import android.content.Context;

public class ExpandableHeightListView extends ListView
{

    boolean expanded = false;

    public ExpandableHeightListView(Context context)
    {
        super(context);
    }

    public ExpandableHeightListView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public ExpandableHeightListView(Context context, AttributeSet attrs,
            int defStyle)
    {
        super(context, attrs, defStyle);
    }

    public boolean isExpanded()
    {
        return expanded;
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {

        if (isExpanded())
        {
            // Calculate entire height by providing a very large height hint.
            // But do not use the highest 2 bits of this integer; those are
            // reserved for the MeasureSpec mode.
            int expandSpec = MeasureSpec.makeMeasureSpec(
                    Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, expandSpec);

            ViewGroup.LayoutParams params = getLayoutParams();
            params.height = getMeasuredHeight();
        }
        else
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    public void setExpanded(boolean expanded)
    {
        this.expanded = expanded;
    }
}

添加此自定义的java文件并在xml中使用它。 并在你的MainActivity中这样做:

   ExpandableHeightListView  listview= (ExpandableHeightListView) findViewById(R.id.comment_listview);

并设置适配器并设置一个属性

listview.setAdapter("Your Adapter");
listview.setExpanded(true);