2016-05-04 14 views
2

Ich habe eine recyclerView in meinem LinearLayout.RecyclerView in einem LinearLayout zusammen mit mehreren anderen Ansichten

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

Das Problem ist, dass, wenn ich diese recyclerView aufblasen, die komplette Aktivität nicht scrollt (Nur der Teil der Recycler Ansicht der Fall ist). Außerdem habe ich die Höhe von recyclerView angegeben, sonst kann ich es nicht sehen.

Also meine beiden Probleme sind:

  1. Wie diese RecyclerView Höhe dynamisch geben (layout_height: wrap_content nicht funktioniert)
  2. Wie die gesamte Aktivität zu bewegen (und nicht nur die recyclerView Teil) wenn Inhalt mehr ist.

PS Ich bin neu in Android, obwohl ich früher recyclerView umgesetzt haben, aber in diesen Fällen gab es nur ein Element (ein recyclerView innerhalb eines übergeordneten Linearlayout)

+0

@ b1izzard ich auch diesen Beitrag gefunden hatte, aber Sie können mir sagen, warum wrap_content ist hier nicht arbeiten, Gibt es keinen anderen Weg, als Höhe aus Java-Datei zu setzen? – Abhishek

+0

Welchen LayoutManager verwenden Sie? – AbhayBohra

+0

@Abhay LinearLayoutManager – Abhishek

Antwort

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

Fügen Sie diese angepasste Java-Datei hinzu und verwenden Sie sie in XML. und in Ihrem MainActivity tun wie folgt aus:

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

und setzen Adapter und stellen Sie eine Eigenschaft

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

Ich benutze RecyclerView nicht listView – Abhishek

+0

Ja, ich weiß, Sie sind Ich benutze LinearLayoutManager deshalb habe ich vorgeschlagen, dass Sie diese benutzerdefinierte Listview-Klasse verwenden. Es wird Ihnen helfen, Ihr Problem zu lösen – AbhayBohra

Verwandte Themen