0

Ich verwende RecyclerView mit StaggeredGridLayoutManager Tabellen von Kursen in meiner Anwendung zu zeigen, und das war in Ordnung, wie das Bild unten:Align Artikel von RecyclerView in jeder Zeile in android

enter image description here

Aber Wie kann ich alle Elemente der zweiten Reihe anpassen unten mit der gleichen Zeile wie das Bild gestartet werden: enter image description here

das ist mein onCreateView() -Methode in meinem Fragmente:


 

 
    @Override 
 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
 

 
     View rootView = inflater.inflate(R.layout.fragment_orders, container, false); 
 

 

 
     recyclerView = (RecyclerView) rootView.findViewById(R.id.recycleView); 
 
     adapter = new MyCustomAdapter(getActivity(), Data.getData()); 
 
     recyclerView.setAdapter(adapter); 
 
     // First param is number of columns and second param is orientation i.e Vertical or Horizontal 
 
     StaggeredGridLayoutManager gridLayoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL); 
 
     recyclerView.setLayoutManager(gridLayoutManager); 
 
     
 
     return rootView; 
 
    } 
 
}

Und dies kann Adapter MyCustomAdapter.java:

public class MyCustomAdapter extends RecyclerView.Adapter<MyCustomAdapter.MyViewHolder> { 
 

 
    private Context _context; 
 
    private ArrayList<Information> data; 
 
    private LayoutInflater inflater; 
 
    private int previousPosition = 0; 
 
    
 
    public MyCustomAdapter(Context context, ArrayList<Information> data) { 
 

 
     this._context = context; 
 
     this.data = data; 
 
     inflater = LayoutInflater.from(context); 
 
    } 
 

 
    @Override 
 
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int position) { 
 

 
     View view = inflater.inflate(R.layout.orders_row, parent, false); 
 

 
     MyViewHolder holder = new MyViewHolder(view); 
 
     return holder; 
 
    } 
 

 
    @Override 
 
    public void onBindViewHolder(MyViewHolder myViewHolder, final int position) { 
 

 
     if(position == 1){ 
 
      create_tablelayout(myViewHolder.itemView,position,3); 
 
     }else if(position == 3){ 
 
      create_tablelayout(myViewHolder.itemView,position,8); 
 
     }else if(position == 5){ 
 
      create_tablelayout(myViewHolder.itemView,position,1); 
 
     }else 
 
      create_tablelayout(myViewHolder.itemView,position,5); 
 

 
     myViewHolder.num_tab.setText("Table # " + data.get(position).id); 
 
     myViewHolder.server.setText(data.get(position).server); 
 
     
 

 
     final Information infoData = data.get(position); 
 

 
    } 
 

 
    @Override 
 
    public int getItemCount() { 
 
     return data.size(); 
 
    } 
 

 
    class MyViewHolder extends RecyclerView.ViewHolder{ 
 

 
     TextView num_tab; 
 
     TextView server; 
 

 
     public MyViewHolder(View itemView) { 
 
      super(itemView); 
 

 
      num_tab = (TextView) itemView.findViewById(R.id.num_table); 
 
      server = (TextView) itemView.findViewById(R.id.server); 
 

 
     } 
 
    } 
 

 
    
 
     /* Find Tablelayout defined in main.xml */ 
 
     TableLayout tablelayout = (TableLayout) convertView.findViewById(R.id.itemtablelayout); 
 
     tablelayout.removeAllViews(); 
 
     for(int i = 0;i<nbr_item; i++) { 
 
      // Creating a new TextView 
 
      TextView Order1 = new TextView(_context); 
 
      Order1.setText("TESSST"); 
 
      Order1.setTextColor(Color.parseColor("#FF9900")); 
 
      Order1.setTextSize(10); 
 
      Order1.setLayoutParams(new TableRow.LayoutParams 
 
        (LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
 
      //=================== 
 

 
      //Prepare TableRow to be inserted 
 
      TableRow row = new TableRow(_context); 
 
      row.addView(Order1); 
 
      row.setPadding(4,4,4,4); 
 
      row.setGravity(Gravity.CENTER); 
 
      row.setLayoutParams(new TableLayout.LayoutParams 
 
        (LinearLayout.LayoutParams.MATCH_PARENT, 
 
          LinearLayout.LayoutParams.WRAP_CONTENT)); 
 

 

 
      tablelayout.addView(row); 
 
     } 
 
    } 
 
}

und das ist der fragment_orders.xml

<?xml version="1.0" encoding="utf-8"?> 
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
 
    android:orientation="vertical" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" 
 
    android:background="@color/gray"> 
 

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

 
    <LinearLayout 
 
     android:layout_marginTop="10dp" 
 
     android:layout_width="match_parent" 
 
     android:background="@color/white" 
 
     android:layout_height="50dp"> 
 
    </LinearLayout> 
 

 
    <android.support.v7.widget.RecyclerView 
 
     android:id="@+id/recycleView" 
 
     android:paddingLeft="@dimen/space" 
 
     android:paddingRight="@dimen/space" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="match_parent"> 
 

 
    </android.support.v7.widget.RecyclerView> 
 

 
</LinearLayout> 
 

 
</LinearLayout>

Bitte helfen Sie mir, ich meine ganze Zeit mit diesem Thema verbracht haben, aber ich kann nichts Vergleichbares

Antwort

1

Von Ihrem beigefügten Screenshots erhalten, Ihren Fall sieht in der Notwendigkeit einer GridLayoutManager statt StaggeredGridLayoutManager zu sein .
die erforderliche Anzahl der Spalten in Ihrem Fall Finden Sie heraus (3/4, diese dynamisch entsprechend der Gerätebreite berechnet werden kann, wenn erforderlich) und stellen Sie dann Ihr Layout-Manager wie:

// Assuming there'll be 3 columns 
GridLayoutManager glm = new GridLayoutManager(getActivity(), 3); 
recyclerView.setLayoutManager(glm);