How to set different columns for rows in android gridview

后端 未结 10 793
旧时难觅i
旧时难觅i 2020-12-01 07:17

I want to have a gridview similar to this

\"enter

Every odd numbered row will

相关标签:
10条回答
  • 2020-12-01 08:14

    I have something similar and i solved with the new RecyclerView.

    I created a Fragment with an a RecyclerView. RecyclerView on xml:

    <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/filter_subtypes" android:layout_width="match_parent" android:layout_height="match_parent" />
    

    On your Fragment/Activity (OnViewCreated in my fragment case). I find the RecyclerView and set an Adapter- a normal Adapter class inherit from RecyclerView.Adapter< YOUR VIEW HOLDER class >- And then i create a GridLayoutManager

    final GridLayoutManager mng_layout = new GridLayoutManager(this.getActivity(), TOTAL_CELLS_PER_ROW/*In your case 4*/);
    


    Then i override this method to set a dynamic numbers of columns (cells)

    mng_layout.setSpanSizeLookup( new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    switch( adapterSubtype.getItemViewType(position) ) {
                        case FilterSubtypesAdapter.TYPE_LOW:
                            return TOTAL_CELLS_PER_ROW;
                        case FilterSubtypesAdapter.TYPE_HIGH:
                            return 2;
                        default:
                            return -1;
                    }
                }
            });
    myRecyclerView.setLayoutManager(mng_layout);
    

    With this you will get dynamic numbers of cell on your rows.

    EXTRA: Then if you are using the same view/type view on your adapter, you will get the same w & h view. You will need to create 2 xml views for TYPE_HIGH and other view for TYPE_LOW.

    So, in your adapter, you need to have 2 kind of data (1 for high images and 1 for low images). You must override this methods

    @Override
    public SubtypeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType==TYPE_HIGH) {
            view = inflater.inflate(R.layout.item_image_high, parent, false);
        } else {
            view = inflater.inflate(R.layout.item_image_low, parent, false);
        }
        return new SubtypeViewHolder(view, viewType);
    }
    
     @Override
     public int getItemViewType(int position) {
         return (list.get(position).getType()==Subtype_type.HIGH) ? TYPE_HIGH : TYPE_LOW;
     }
    

    I hope i was clear, any problem tell me.

    0 讨论(0)
  • 2020-12-01 08:20

    I guess the best way to do is using recycler view.. Also it is preferred over list/grid for performance

    May be below links can help a lot - All are related to Two way View by Lucas

    https://github.com/lucasr/twoway-view

    https://plus.google.com/+LucasRocha/posts/WBaryNqAHiy

    http://lucasr.org/2014/07/31/the-new-twowayview/

    0 讨论(0)
  • 2020-12-01 08:20

    Create two different XML files and inflate based on position in your Adapter like below.

    if(position%2==0){
    //Inflate Even number layout with 4 images
    }else{
    //Inflate ODD number layout with 2 images
    }
    
    0 讨论(0)
  • 2020-12-01 08:22

    If you are using RecyclerView for GridView, then there is solution that should work for you:

    GridLayoutManager layoutManager = new GridLayoutManager(this, 4);
    layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
             int mod = position % 6;    
    
             if(position == 0 || position == 1)
                  return 2;
             else if(position < 6)
                  return 1;
             else if(mod == 0 || mod == 1)
                  return 2;
             else
                  return 1;
        }
    });
    
    recyclerView.setLayoutManager(layoutManager);
    

    Hope this work for you!

    0 讨论(0)
提交回复
热议问题