横向ListView

布局:

xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ycjr.gridviewdemo.MainActivity">



    

        
    

代码:
package com.ycjr.gridviewdemo;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {
MyHandler handler;
GridView gridView;
private String[] itemList = new String[10];

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.activity_main);
    gridView = (GridView) findViewById(R.id.gridView);
    handler = new MyHandler();
    new Thread() {
        public void run() {
            for (int i = 0; i < 10; i++) {
                String item = "北极熊生存如履薄冰" + i + "如果我们从现在开始就采取措施降低气温" + i;
                itemList[i] = item;
            }
            Message msg = Message.obtain();
            msg.what = 1;
            handler.sendMessage(msg);
        }
    }.start();
}


public class MyHandler extends Handler {
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case 1:
                if ((itemList == null) || (itemList.length == 0)) {
                    return;
                } else {
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(itemList.length * (100 + 6), 500);
                    gridView.setLayoutParams(params);
                    gridView.setColumnWidth(100);
                    gridView.setHorizontalSpacing(6);
                    gridView.setStretchMode(GridView.NO_STRETCH);
                    gridView.setNumColumns(itemList.length);
                    gridView.setAdapter(new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, itemList));
                }
                break;
        }
    }
}

}

你可能感兴趣的:(横向ListView)