2015-09-01 80 views
4

解決CHECK回答以下...製作Recyclerview固定高度和滾動

所以我想創建我的Android應用程序的評論功能,我想顯示recyclerview內的意見,然後有一個按鈕的TextView在recyclerview下面添加評論。我想讓recyclerview具有一定的高度,並在可以滾動的情況下進行滾動,因爲我不希望用戶必須向下滾動屏幕才能找到添加按鈕。

我無法讓它工作,所以我想知道是否有其他人有這個問題。

我有所有的適配器和一切設置,我只是遇到recyclerview的麻煩。

謝謝。

我的概念並不清楚我想要完成什麼。我正在嘗試創建一個Cardview,它將顯示所有評論以及添加新評論的能力。回收者視圖將佔用高度的大約80%,然後最後的20%用於編輯文本和按鈕。

我的XML(滾動到最後cardview其中recyclerview是)

<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> 

第二和第一評論被切斷。

enter image description here

+0

爲什麼不使用'FrameLayout'? – akash93

回答

8

那麼我的問題是,由於某種原因,Recyclerview didnt wrap_contents。我做了一些研究(感謝stackoverflow),發現很多人都遇到了這個問題,他們爲這個問題發佈瞭解決方案。

基本上,我不得不使用定製的linearlayoutmanger來解決這個問題。

我會發布他們發佈的解決方案並鏈接到他們的問題。感謝誰試圖幫助,我感謝。

這是我需要的額外文件。然後,我不得不將我的recyclerview設置爲使用此佈局而不是默認佈局。

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)); 

三個新paramets(上下文,INT方向,布爾反向); 基本上,我把1的方向,所以它顯示verticalally,併爲反轉,所以它顯示了它是如何在我的列表中排序。

鏈接到與我同樣問題的其他人的問題。 Nested Recycler view height doesn't wrap its content

Nested Recycler view height doesn't wrap its content

再次感謝你們。希望這可以幫助別人

+3

適配器設置後,我面臨以下問題 - 'java.lang.IndexOutOfBoundsException:無效的項目位置0(0)。項目數量:0'任何解決方案? – ketan

+0

我也面臨同樣的問題,你有沒有得到任何工作解決方案? –

+0

@ketan只是將崩潰的代碼放入postDelayed(1000毫秒) –

3

您的活動添加該代碼,將設置你的回收以用戶的屏幕窗口的90%的高度。

DisplayMetrics displaymetrics = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 

int a = (displaymetrics.heightPixels*90)/100; 

recylcerView.getLayoutParams().height =a; 

及以下的recyclerView

您的評論佈局這樣

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <LinearLayout 
     android:layout_alignParentTop="true" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/mudit" 
     > 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/my_recycler_view" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:elevation="5dp" 
      android:scrollbars="vertical" /> 
    </LinearLayout> 

    <RelativeLayout 
     android:layout_below="@+id/mudit" 
     android:id="@+id/rl_commentWrap" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" > 

     <EditText 
      android:id="@+id/editText1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentLeft="true" 
      android:ems="10" /> 

     <Button 
      android:id="@+id/plusButton" 
      style="android:buttonStyleSmall" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentRight="true" 
      android:text="Send" 
      android:textColor="#FFFFFF" 
      android:translationZ="5dp" /> 
    </RelativeLayout> 

</RelativeLayout> 
+0

但我如何讓它滾動,如果有更多的數據。現在不是這樣做,它只是切斷它。 recyclerview不會滾動 – NewKidOnTheBlock

+0

那麼,回收站視圖是可自動滾動的,您應該爲回收站視圖添加更多視圖,然後檢查; –

+0

我做了,由於某種原因,它不滾動。我做了你上面寫的...... – NewKidOnTheBlock

相關問題