在android中的每一行中对齐RecyclerView的项目

时间:2016-07-03 02:09:12

标签: gridview android-recyclerview grid-layout tablelayout staggeredgridlayout

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

enter image description here

但是,如何调整第二行的所有项目以相同的行开始,如下图所示: enter image description here

这是我片段中的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>

请帮助我,我把所有时间花在了这个问题上,但我无法得到类似的东西

1 个答案:

答案 0 :(得分:1)

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

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