0

我正在使用 RecyclerView 和 StaggeredGridLayoutManager 在我的应用程序中显示课程表,这没关系,如下图所示:

在此处输入图像描述

但是,如何将第二行的所有项目调整为以同一行开头,如下图所示: 在此处输入图像描述

这是我片段中的 onCreateView() 方法:

   

    @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;
    }
}

这可能是适配器 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);
        }
    }
}

还有这个 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>

请帮助我,我已经把所有时间都花在了这个问题上,但我找不到类似的东西

4

1 回答 1

2

从您附加的屏幕截图中,您的案例看起来需要GridLayoutManager而不是StaggeredGridLayoutManager
找出您的情况所需的列数(3/4,如果需要,可以根据设备宽度动态计算),然后将您的布局管理器设置为:

// Assuming there'll be 3 columns
GridLayoutManager glm = new GridLayoutManager(getActivity(), 3);
recyclerView.setLayoutManager(glm);
于 2016-10-10T14:37:37.793 回答