Android GridView load items vertically

后端 未结 7 1409
离开以前
离开以前 2021-02-05 07:16

Is it possible to load items in GridView with vertical orientation? I mean...

Normal situation - 3 columns

\

相关标签:
7条回答
  • 2021-02-05 07:40

    I don't think this is possible at all.

    Reason ::

    • There is technical glitch doing this since we want to scroll items vertically. And also the rendering items on the screen is also vertical direction. In this case we can not complete the calculation we need to do in the onLayout and onMeasure Method of the GridView.
    • If try another way i.e some of us has suggested that snuffle the datasource as [1,5,2,6,3,7,4,8, ...] but in this case what would be shuffle algorithm?
    • When user flings vertically then how can you calculate how many items we need to render vertically and when to switch to next column to render the other items. there is no such mathematical formula for it.

    I did some work on this and came to this conclusion.Hope this make unanswered question answered.

    0 讨论(0)
  • 2021-02-05 07:42

    If you are using GridLayoutManager with RecyclerView, easy way to fill vertically data first is :

    GridLayoutManager lLayout_artist = new GridLayoutManager(getActivity(),3,GridLayoutManager.HORIZONTAL, false);
    

    You can change orientation of GridLayoutManager as per your requirement.

    0 讨论(0)
  • 2021-02-05 07:50

    // Have you ever used GridLayout this, Try this one:

    File -> Import -> <sdk_folder>\extras\android\support\v7\gridlayout>
    

    // Add this project as library project

    // xml file - activity_main.xml

    <android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/gridLayout"
        tools:context="com.example.gridlayoutdemo.MainActivity" >
    
    </android.support.v7.widget.GridLayout>
    

    // Java code

    MainActivity.class

    import android.os.Bundle;
    import android.support.v7.app.ActionBarActivity;
    import android.support.v7.widget.GridLayout;
    import android.view.View;
    import android.view.ViewGroup.LayoutParams;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MainActivity extends ActionBarActivity {
    
        private GridLayout gridLayout;
        private TextView[] textViews;
        private int noOfItems;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            gridLayout = (GridLayout) findViewById(R.id.gridLayout);
            gridLayout.setOrientation(1);
            gridLayout.setColumnCount(3);
            gridLayout.setRowCount(3);
    
            textViews = new TextView[8];
    
            for (int i = 0; i < 8; i++) {
                textViews[i] = new TextView(MainActivity.this);
                textViews[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT));
                textViews[i].setText(String.valueOf(i));
                textViews[i].setTextSize(25);
                textViews[i].setPadding(50, 25, 10, 25);
                gridLayout.addView(textViews[i]);
            }
    
            setContentView(gridLayout);
    
            for (noOfItems = 0; noOfItems < 8; noOfItems++) {
                textViews[noOfItems].setOnClickListener(new View.OnClickListener() {
    
                    int pos = noOfItems;
    
                    public void onClick(View v) {
                        Toast.makeText(getBaseContext(), pos + " Clicked",
                                Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }
    
    }
    

    // If you want to display horizontally than do this gridLayout.setOrientation(0);

    0 讨论(0)
  • 2021-02-05 07:55

    Please clarify, say you have a Grid like this

    1  4  7
    2  5  8
    3  6  9
    

    now if you scroll it down vertically, it should basically be like this??

    2  5  8
    3  6  9
    4  7  10
    

    In this case,

    • items 4 and 7 are not recycled, in fact they are moved to a new position
    • item 1 is to be recycled
    • only item 10 is introduced

    Clearly, this is not how a normal ListView/GridView works where a row is recycled at a time, AFAIK you will need to customise GridView to get this functionality.

    0 讨论(0)
  • 2021-02-05 07:55

    Is it possible to change the sequence of data? In your example, change data to [1,3,5,2,4,6, ...] for horizontal orientation, [1,5,2,6,3,7,4,8, ...] for vertical orientation.

    0 讨论(0)
  • 2021-02-05 08:00

    I think you need https://github.com/ApmeM/android-flowlayout . and set child gravity to Gravity.FILL_HORIZONTAL

    they even have vertical or horizontal orientation

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