0

Ich habe versucht, eine Listview unter einigen anderen Ansichten in Android platzieren. Zum Beispiel.Platzieren einer ListView unter anderen Ansicht in Android

Ich habe eine Scroll, die ich Andere Ansicht nach innen Einschließlich der Listenansicht

Ich will die Listenansicht zu lange, so lange 1000DP, so dass Benutzer Legen Sie zur Listenansicht und die Scroll der Liste Innerhalb eines Layout-Blätter können.

Ich habe das seit 2 Tagen gemacht. Jede Hilfe wird sehr geschätzt. Dies ist mein Code

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:fillViewport="true" > 

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:layout_marginBottom="50dip" 
    > 

    <TextView 
     android:id="@+id/tvheading" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_marginBottom="10dp" 
     android:layout_marginTop="2dp" 
     android:gravity="center" 
     android:text="@string/title_AllSensorhead" 
     android:textStyle="bold" /> 

    <View 
     style="@style/Divider" 
     android:layout_above="@+id/listView" /> 

    <ProgressBar 
     android:id="@+id/progressBar" 
     style="?android:attr/progressBarStyleLarge" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:layout_gravity="center_horizontal" 
     android:indeterminateDrawable="@drawable/progress" 
     android:visibility="gone" /> 


    <com.github.mikephil.charting.charts.PieChart 
     android:id="@+id/chart" 
     android:layout_width="match_parent" 
     android:layout_height="200dp" 
     android:layout_gravity="center" /> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/colorPrimaryDark" 
     android:orientation="vertical" 
     android:padding="8dp"> 

     <TextView 
      android:id="@+id/textSensorName" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginLeft="2dp" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:textColor="@color/black" 
      android:textSize="20dp" 
      android:textStyle="bold" /> 


     <TextView 
      android:id="@+id/textSensorNo" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="2dp" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:textColor="@color/white" 
      android:textSize="15dp" 
      android:textStyle="bold" /> 

    </LinearLayout> 

    <ListView 
     android:id="@+id/listView" 
     android:layout_width="match_parent" 
     android:layout_height="1000dip" 
     android:layout_marginTop="5dip" 
     > 
    </ListView> 


</LinearLayout> 

+2

Verwenden Sie ** RelativeLayout ** anstelle von ** ScrollView ** –

+1

Platzieren einer scrollbaren Ansicht (der ListView) in einer anderen scrollbaren Ansicht (der ScrollView) ist nie eine gute Idee. –

+0

Darüber hinaus ist das Verschachteln von Layouts schlecht für die Leistung. Sie brauchen nicht wirklich 2 LinearLayouts - eines innerhalb des anderen. –

Antwort

0

Verwenden Sie eine Scroll-Ansicht alle Android-XML-Datei einzuschließen .... das definierende Layout sollte anstelle von linearen Layout ....

+0

, Das definierende Layout ist ein ScrollVew aus dem Code, den ich gepostet habe. – devloper2009

+1

Wie Rotwang darauf hingewiesen hat _ "Das Platzieren einer scrollbaren Ansicht (der ListView) in einer anderen scrollbaren Ansicht (der ScrollView) ist nie eine gute Idee." _ – Shashanth

0

Gerade scrollbaren sein fügen Sie diese Zeilen Code in Ihre Java-Klasse:

ListView lv = (ListView) findViewById(R.id.layout_lv); 
lv.setOnTouchListener(new OnTouchListener() { 
    // Setting on Touch Listener for handling the touch inside ScrollView 
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     // Disallow the touch request for parent scroll on touch of child view 
     v.getParent().requestDisallowInterceptTouchEvent(true); 
     return false; 
    } 
}); 

Wenn Sie Listview innerhalb eines Scroll alle setzen dann Die ListView erstreckt sich nicht auf ihre volle Höhe. Unten ist eine Methode, um dieses Problem zu beheben.

/**** Method for Setting the Height of the ListView dynamically. 
**** Hack to fix the issue of not showing all the items of the ListView 
**** when placed inside a ScrollView ****/ 
public static void setListViewHeightBasedOnChildren(ListView listView) { 
    ListAdapter listAdapter = listView.getAdapter(); 
    if (listAdapter == null) 
     return; 

    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED); 
    int totalHeight = 0; 
    View view = null; 
    for (int i = 0; i < listAdapter.getCount(); i++) { 
     view = listAdapter.getView(i, view, listView); 
     if (i == 0) 
      view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT)); 

     view.measure(desiredWidth, MeasureSpec.UNSPECIFIED); 
     totalHeight += view.getMeasuredHeight(); 
    } 
    ViewGroup.LayoutParams params = listView.getLayoutParams(); 
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); 
    listView.setLayoutParams(params); 
} 

zur Anwendung dieses Verfahrens nur das Listview innerhalb dieser Methode übergeben:

ListView list = (ListView) view.findViewById(R.id.ls); 
setListViewHeightBasedOnChildren(list); 

Sag mir, wenn es für Sie funktioniert =).

0

es kann eine mögliche Lösung für das Problem sein ..

Was ich denke, Sie nur ganze Bildschirm scrollen möchten, die eine Listenansicht haben ..

Also, zunächst ein anderes Layout Namen wie header_listview.xml erstellen

in diesem Code abgesehen von dieser Listenansicht in ihr ..

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:layout_marginBottom="50dip" 
    > 

    <TextView 
     android:id="@+id/tvheading" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_marginBottom="10dp" 
     android:layout_marginTop="2dp" 
     android:gravity="center" 
     android:text="@string/title_AllSensorhead" 
     android:textStyle="bold" /> 

    <View 
     style="@style/Divider" 
     android:layout_above="@+id/listView" /> 

    <ProgressBar 
     android:id="@+id/progressBar" 
     style="?android:attr/progressBarStyleLarge" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:layout_gravity="center_horizontal" 
     android:indeterminateDrawable="@drawable/progress" 
     android:visibility="gone" /> 


    <com.github.mikephil.charting.charts.PieChart 
     android:id="@+id/chart" 
     android:layout_width="match_parent" 
     android:layout_height="200dp" 
     android:layout_gravity="center" /> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/colorPrimaryDark" 
     android:orientation="vertical" 
     android:padding="8dp"> 

     <TextView 
      android:id="@+id/textSensorName" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginLeft="2dp" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:textColor="@color/black" 
      android:textSize="20dp" 
      android:textStyle="bold" /> 


     <TextView 
      android:id="@+id/textSensorNo" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="2dp" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:textColor="@color/white" 
      android:textSize="15dp" 
      android:textStyle="bold" /> 

    </LinearLayout>  
</LinearLayout> 

Jetzt kann Ihr Listview-Layout sein einfach wie

<ListView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/listView" 
     android:layout_width="match_parent" 
     android:layout_height="1000dip" 
     android:layout_marginTop="5dip" 
     /> 

in der Java-Datei, wenn die Instanz von Liste fatching .. zwei Zeilen hinzufügen

View headerView= LayoutInflater.from(this).inflate(R.layout.header_listview,null,false); 
    listView.addHeaderView(headerView; 

Sie können diese Textviews & progressbar & Diagramm aus diesem headerview..like erhalten

TextView txt=(TextView) headerView.findViewById(R.id.tvheading); 

von diesem Ansatz wird die Verletzung von scrollview mit listivew nicht ankommen, Auch müssen Sie nicht 1000dip fix Höhe Listenansicht geben .. Bitte sagen, wenn mehr Hilfe benötigen .. Und zu schätzen, wenn es hilft ..

+0

So blicke ich jetzt auf. Kann ich es mit dem obigen Code verwenden? Ich habe es benutzt, aber es nicht zur Arbeit gebracht View rootView = inflater.inflate (R.layout.fragment_selected_sensor, container, false); – devloper2009

Verwandte Themen