0

Ich muss ein kompliziertes Layout erstellen (eine Quittungsansicht) mit vielen Daten. Da es x Gebühren oder Zahlungen geben kann, muss diese Ansicht programmgesteuert erfolgen.Layouts programmatisch erstellen und zur Ansicht hinzufügen

enter image description here

Ich versuche, wiederverwendbare Layouts zu erstellen, die ich auf verschiedene textview s oder edittext s einhängen können und haben sie auf die Linie.

Gegliedert es so etwas wie folgt aussieht: enter image description here

Welche auf 2 Grundlayout kommt:

  1. Eine volle Breite (wo der Text zentriert ist)
  2. A Split Spalte

a) wo eine Spalte 3/4s die Ansicht auf der linken oder rechten Seite aufnimmt.

b) und die zweite Spalte nimmt 1/4 der Ansicht.

Hier ist mein Versuch. Aber ich scheint es nicht richtig zu machen. Hier ist mein Fehler:

The specified child already has a parent. You must call removeView() on the child's parent first.

Kann jemand helfen?

private ScrollView mScrollView; 
private LinearLayout mLinearLayout; 
private ProgressBar mProgressBar; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_payment, container, false); 
    mScrollView = (ScrollView) view.findViewById(R.id.svPayment); 
    mScrollView.setVisibility(View.GONE); 
    mProgressBar = (ProgressBar) view.findViewById(R.id.pbPayment); 
    mLinearLayout = (LinearLayout) view.findViewById(R.id.llPayment); 
    return view; 
} 


public void setupGUI() { 


     //RESUABLE LAYOUT 
     LinearLayout.LayoutParams paramsFull = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
     paramsFull.setLayoutDirection(LinearLayout.HORIZONTAL); 

     LinearLayout.LayoutParams paramSmall = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
     paramSmall.setLayoutDirection(LinearLayout.HORIZONTAL); 
     paramSmall.weight = 0.2f; 

     LinearLayout.LayoutParams paramLarge = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
     paramLarge.setLayoutDirection(LinearLayout.HORIZONTAL); 
     paramLarge.weight = 0.8f; 


     // HOLDS THE LEFT AND RIGHT COLUMNS 
     LinearLayout columnShell = new LinearLayout(getContext()); 
     columnShell.setLayoutParams(paramsFull); 

    //Small column 
     LinearLayout columnSmall = new LinearLayout(getContext()); 
     columnSmall.setLayoutParams(paramSmall); 

    //Large column 
     LinearLayout columnLarge = new LinearLayout(getContext()); 
     columnLarge.setLayoutParams(paramLarge); 



     //First get rid of all the views, incase of refresh 
     mLinearLayout.removeAllViews(); 


     TextView textView = new TextView(getContext()); 

     //CUSTOMER 
     textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); 
     textView.setLayoutParams(fullwidth); 
     textView.setText("Mary Jane"); 
     columnShell.addView(textView); 

     mLinearLayout.addView(columnShell); 


     LinearLayout columnRight = new LinearLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
     columnRight.setLayoutDirection(LinearLayout.HORIZONTAL); 

     //LEFT SIDE (1/4) 
     textView = new TextView(getContext()); 
     textView.setLayoutParams(fullwidth); 
     textView.setText("PO: "); 
     columnSmall.addView(textView); 
     columnShell.addView(columnSmall); 

     //RIGHT SIDE (3/4) 
     textView = new TextView(getContext()); 
     textView.setLayoutParams(fullwidth); 
     textView.setText("4465465456"); 

     columnLarge.addView(textView); 
     columnShell.addView(columnLarge); 

     mLinearLayout.addView(columnShell);  
} 
} 

fragment_payment.xml

<LinearLayout 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:orientation="vertical" 
       tools:context="com.mycompany.myapp.Views.MasterDetails.PaymentFragment"> 

    <ProgressBar 
     android:id="@+id/pbPayment" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 

    <ScrollView 
     android:id="@+id/svPayment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/COLOR_LIGHT_GREY"> 

     <LinearLayout 
      android:id="@+id/llPayment" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"></LinearLayout> 
    </ScrollView> 

</LinearLayout> 
+3

Wenn Sie nicht "richtig" bekommen, dann sagen Sie, was falsch ist ... es wird waaaay besser sein, 'ListView' oder' RecyclerView' (mit Adaptern) zu verwenden – snachmsm

+3

Sieht aus wie 'RecyclerView' könnte besser sein Ansatz –

+1

RecyclerView + Funktionalität, um den viewHolder basierend auf welcher "Ansicht" in der Liste angezeigt werden soll. Sehen Sie sich diesen Codepath an: https://guides.codepath.com/android/Heterogen-Layouts-inside-RecyclerView – MikeOscarEcho

Antwort

0

Warum Verwendung Scrollview mit Linearlayout, können Sie Listenansicht mit zwei Textview als chid verwenden müssen, und neue Daten hinzufügen einfach in der Liste hinzufügen und benachrichtigen zu adpter, Ihre neuen Daten automatisch zuletzt der Liste hinzugefügt.

Verwandte Themen