2016-12-24 8 views
0

Ich muss Listview und Rest der Ansichten nach der Listview in meinem Fragment anzeigen.Scrollbare Ansicht mit LinearLayout und ListView

Zuerst habe ich versucht mit ScrollView anstelle von LinearLayout aber wenn ich es benutze ListView nur zeigen nur eine Zeile.

Problem mit LinearLayout ist nicht Scrollen der gesamten Ansicht. Es scrollt nur durch ListView. So sind andere Ansichten immer von der Hauptansicht verborgen.

Ich möchte gesamte Ansicht scrollbar mit erweiterten listview machen. Es gibt also keine zwei Scroll-Ansichten. wie kann ich das machen?

Mein Fragment Layout ist wie folgt.

<FrameLayout> 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <ListView> 
      /** List View **/ 
     </ListView> 

     <LinearLayout> 
      /** Layouts that need to be display after list view **/ 
     </LinearLayout> 

    </LinearLayout> 
</FrameLayout> 
+0

Sie möchten eine lineare Layoutansicht am Ende der Listenansicht oder auf dem gleichen Bildschirm? –

+0

Gleicher Bildschirm nach Listenansicht. –

+0

Lassen Sie mich meine Frage löschen, wollen Sie 70% der Bildschirm zeigen Listenansicht und 30% zeigen Ihr Layout? oder nach der Vervollständigung beim Scrollen der Listenansicht ist der letzte Eintrag der Listenansicht diese Ansicht? –

Antwort

1

können sich diese Liste Ansicht verwenden, mit Scroll-Ansicht

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) 
    { 
     // HACK! TAKE THAT ANDROID! 
     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; 
    } 
} 

und in der Klasse

ExpandableHeightListView listView = new ExpandableHeightListView(this); 
listView.setAdapter(adapter); 
listView.setExpanded(true); 

in xml

<yourpackagename.ExpandableHeightListView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
      /> 

die Listenansicht voll ausgebaut wird

+0

Danke. es funktionierte. Ich weiß nicht, warum wir diesen Hack machen müssen, um ListView erweiterbar zu machen. Es sollte XML-Wege geben, dies zu tun. –

2
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/adLinear" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true"> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <ListView 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="3" /> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:background="@color/colorAccent"> 


     </LinearLayout> 

    </LinearLayout> 

</ScrollView> 

versuchen Sie dies.

+0

ändern Sie die übergeordnete 'LinearLayout'-Breite in' match_parent'. – Ironman

+0

nein. es scrollt überhaupt nicht. Die Hälfte meiner ListView-Anzeige und der Rest der Ansicht sind unten und ich kann nicht scrollen, um sie anzuzeigen. –

+0

ohne Scroll-Ansicht funktioniert es gut. aber listview ist nicht vollständig erweitert. Ich möchte zunächst die erweiterte Listenansicht und dann die restlichen Ansichten anzeigen. –

1

In Xml:

<!--Required to scroll the list view with Linear Layout--> 
     <ScrollView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:fillViewport="true"> 

      <!--Direct child container of scroll view--> 
      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical"> 

       <com.example.custom.ScrollableListView 
        android:layout_width="match_parent" 
        android:layout_height="0dp" 
        android:layout_weight="1" 
        android:choiceMode="singleChoice" 
        android:divider="@color/colorDivider" 
        android:dividerHeight="0.09dp" 
        android:fadingEdge="none" 
        android:listSelector="@color/colorSelectedGreen" 
        android:orientation="vertical" 
        android:scrollbars="none" /> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" /> 
      </LinearLayout> 
      <!--Direct child container of scroll view--> 

     </ScrollView> 
     <!--Required to scroll the list view with Linear Layout--> 

erstellen ScrollableListView benutzerdefinierte Klasse in Java:

public class ScrollableListView extends ListView { 

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

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

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

    @Override 
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 4, MeasureSpec.AT_MOST)); 
    } 
} 
1

Sie sollten mit Recycler Ansichten versuchen wie jetzt google Ansichten mit Recycler empfiehlt.

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:scrollbars="vertical"> 


<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="#ffffff" 
    android:orientation="vertical"> 

    <ImageView 

     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:adjustViewBounds="true" 
     android:scaleType="centerCrop" 
     android:src="@drawable/loader2" /> 

    <TextView 

     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="5dp" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="5dp" 
     android:text="Title" 
     android:textColor="@android:color/black" 
     android:textSize="26sp" 
     /> 

    <app.so.city.appconstants.CustomTextViews.SourceRegularTextView 

     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="start" 
     android:layout_marginBottom="-40dp" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:lineSpacingMultiplier="1.3" 
     android:textColor="#CC000000" 
     android:textSize="16sp" /> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/article_recycler_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="center" 
     android:background="#ffffff" /> 

</LinearLayout> 

Sie stellen eine beliebige Anzahl von Ansichten innerhalb der verschachtelten Scroll-Ansicht: Sie können innerhalb einer verschachtelten Scroll-Ansicht so etwas wie dies die Recycler Ansicht setzen.

Für das Scrolling perfekt und reibungslos funktionieren, werden Sie auch die folgende Zeile Code zu Java-Code für Recycler Ansichten hinzufügen müssen:

recycler_view.setNestedScrollingEnabled(false); 

Probieren Sie es und lassen Sie mich, wenn diese wissen funktioniert auch für dich.

Verwandte Themen