Android GridView load items vertically

后端 未结 7 1415
离开以前
离开以前 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:50

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

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

    // Add this project as library project

    // xml file - activity_main.xml

    
    
    
    

    // 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);

提交回复
热议问题