【问题标题】:Creating an android gridview that spans multiple columns and rows based on position创建一个基于位置跨越多列和多行的android gridview
【发布时间】:2015-08-27 11:53:56
【问题描述】:

我想实现一个能够根据位置跨越多行和多列的网格布局。假设当跨度计数为 3 时。项目位置为零,取两行跨度和两列跨度,项目位置一取两列跨度和一行跨度项目位置二,取一列跨度和一行跨度项目位置三,取一列跨度和一行跨度

基本上,我希望视图根据其位置跨越不同的列和行。

我尝试使用 setSpanSizeLookUp 方法使用 Recycle 视图的 GridLayoutManager,但它水平或垂直跨越视图。

我发现 twowayview 可用于跨越多个列和行,但我不确定如何在我的项目中实现它,因为它是一个广泛的库(在 android studio 中设置它时遇到问题)。

谁能建议最好的方法?

【问题讨论】:

    标签: android gridview


    【解决方案1】:

    在研究您的问题时,我学到了一些新东西:我碰巧查看了 RecyclerView 的 GridLayoutManager,我注意到您可以设置自定义 SpanSizeLookup。现在,如果您垂直滚动,“跨度”只是一列,如果您水平滚动,则只是一行。因此 SpanSizeLookup 允许您指定例如,项目 0 占用 2 列,项目 1 占用 1 列,等等。

    事实证明,如果您将 RecyclerView 与 GridLayoutManager 一起使用,则解决方案很简单:

       @Override
       protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        // Create a grid layout with two columns
        GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
    
        // Create a custom SpanSizeLookup where the first item spans both 
     columns
        layoutManager.setSpanSizeLookup(new 
     GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return position == 0 ? 2 : 1;
            }
        });
    
        RecyclerView recyclerView = (RecyclerView)  
      findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(new MyGridAdapter());
       }
    

    关于适配器的说明:RecyclerView.Adapter 与 ListAdapter 不兼容。您需要从 RecyclerView.Adapter 扩展您的适配器并进行适当的更改。

    这是我为我的测试项目创建的适配器:

        public static class MyViewHolder extends RecyclerView.ViewHolder {
    
        ImageView mImageView;
        TextView mTextView;
    
        public MyViewHolder(View itemView) {
            super(itemView);
            mImageView = (ImageView) itemView.findViewById(R.id.imageView);
            mTextView = (TextView) itemView.findViewById(R.id.textView);
        }
       }
    
        public static class MyGridAdapter extends     
        RecyclerView.Adapter<MyViewHolder> {
    
        private int[] mDrawables;
    
        public MyGridAdapter() {
            this.mDrawables = new int[] {
                    R.drawable.images_01,
                    R.drawable.images_02,
                    .
                    .
                    .
            };
        }
    
        @Override
        public int getItemCount() {
            return mDrawables.length;
        }
    
          @Override
          public MyViewHolder onCreateViewHolder(ViewGroup parent, int   
            viewType) {
    
            LayoutInflater inflater = (LayoutInflater) 
       parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.layout_grid_item, parent,  
           false);
            MyViewHolder holder = new MyViewHolder(view);
            // set up any onClickListener you need on the view here
            return holder;
        }
    
        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
    
            holder.mImageView.setImageResource(mDrawables[position]);
            holder.mTextView.setText("Image " + position);
        }
       }
    
    
    
    
    
    
      Here xml of Activity
    
    
         <?xml version="1.0" encoding="utf-8"?>
     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/
       xmlns:tools="http://schemas.android.com/tools"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       tools:context="com.syneotek.gridlayoutmanagerdemo.Demo2">
    
    
     <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/recyclerview">
    
     </android.support.v7.widget.RecyclerView>
    
     </RelativeLayout>
    

    这里是 Grid item_list 的 xml

     <?xml version="1.0" encoding="utf-8"?>
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:orientation="vertical"
     >
    
    
      <ImageView
        android:id="@+id/grid_item_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher" >
      </ImageView>
    
      <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/text1"
        android:layout_gravity="bottom"
        android:gravity="center"
        android:textStyle="bold"
        android:textAppearance="?android:textAppearanceMedium"
        android:text="Munaf Patel"/>
    
     </LinearLayout>
    
    
        Best of Luck.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-05
      • 1970-01-01
      • 2013-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多