2016-07-05 13 views
0

Ich habe eine Anwendung, die verschiedene Bilder innerhalb eines LinearLayout (das in einem ScrollView ist) zeigt.Wie bestelle ich Elemente in einem linearen Layout?

Ich möchte die Elemente im Layout neu anordnen, wenn der Benutzer eine Taste drückt. Zum Beispiel: Wenn ich | pic1 | pic2 | pic3 | im Layout gezeigt, aber ich möchte sie neu anordnen, so dass sie als | pic2 | pic1 | pic3 | erscheinen

Das ist mein "activity_main.xml" Datei, wo ich alle meine Bilder haben:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/map" 
    tools:context="dis2.widget.MainActivity"> 

    <HorizontalScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="80dp" 
     android:id="@+id/contactsScrollView" 
     android:fillViewport="false" 
     android:visibility="visible" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginTop="26dp"> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:visibility="visible"> 

      <ImageView 
       android:layout_width="100dp" 
       android:layout_height="wrap_content" 
       android:src="@drawable/pic1" 
       android:id="@+id/pic1ID" 
       android:visibility="gone" /> 

      <ImageView 
       android:layout_width="100dp" 
       android:layout_height="wrap_content" 
       android:src="@drawable/pic2" 
       android:id="@+id/pic2ID" 
       android:visibility="gone" /> 

      <ImageView 
       android:layout_width="100dp" 
       android:layout_height="wrap_content" 
       android:src="@drawable/pic3" 
       android:id="@+id/pic3ID" 
       android:visibility="visible" /> 

     </LinearLayout> 
    </HorizontalScrollView> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Reorder" 
     android:id="@+id/reorderB" 
     android:layout_marginBottom="114dp" 
     android:onClick="reorder" 
     android:layout_alignParentBottom="true" /> 


</RelativeLayout> 

Antwort

1

setzen eine ID zu Ihrem Linearlayout:

android:id="@+id/myLinearLayout 

und in Java:

public void reorder() { 

    LinearLayout myLinearLayout = (LinearLayout) findViewById(R.id.myLinearLayout); 
    // get number of children 
    int childcount = myLinearLayout.getChildCount(); 
    // create array 
    View[] children = new View[childcount]; 

    // get children of linearlayout 
    for (int i=0; i < childcount; i++){ 
     children[i] = myLinearLayout.getChildAt(i); 
    } 

    //now remove all children 
    myLinearLayout.removeAllViews(); 

    //and resort, first position 
    myLinearLayout.addView(children[2]); 
    //second position 
    myLinearLayout.addView(children[0]); 
    //etc. 
} 

betrachten Droid: How to reorder a linearlayouts contents programmatically? und Get all child views inside LinearLayout at once

Verwandte Themen