Android GridView实现横向滚动


1:Activity布局,data_test.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="match_parent"
    android:orientation="vertical">

    <HorizontalScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_margin="10dp" >

            <GridView
                android:id="@+id/listView"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:gravity="center"
                android:numColumns="auto_fit"
                android:stretchMode="spacingWidthUniform" >
            GridView>
        LinearLayout>
    HorizontalScrollView>

LinearLayout>
----------------------------------------------------------------------
2:适配器布局,view1:
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="match_parent"
    android:orientation="vertical">


    <com.android.volley.toolbox.NetworkImageView
        android:layout_width="160dp"
        android:layout_height="120dp"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal"
        android:id="@+id/imageView" />
    <TextView
        android:id="@+id/coursename"
        android:textSize="14dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal"
        android:text="@string/item_name" />


LinearLayout>
---------------------------------------------------------------------
3:主Activity:
public class Second extends Activity {
    private GridView gridView;
    private ArrayList list = new ArrayList();
    private String url = "http://www.jcpeixun.com/app_client_api/course_list.aspx";
    private RequestQueue requestQueue;
    //数据类
    class Item {
        String coursename;//name
        String courseimg;//url
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.data_test);
        gridView = (GridView) findViewById(R.id.listView);
        requestQueue = Volley.newRequestQueue(this);
        getData(url);
    }

    //获得通过Volley框架获得Json数据
    public void getData(String url) {
        JsonArrayRequest req = new JsonArrayRequest(url, new Response.Listener() {
            @Override
            public void onResponse(JSONArray response) {
                for (int i = 0; i < response.length(); i++) {
                    try {
                        JSONObject obj = response.getJSONObject(i);
                        Item item = new Item();
                        item.coursename = obj.getString("coursename");
                        item.courseimg = obj.getString("courseimg");
                        list.add(item);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                horizontal_layout();//设置gridview为横向布局
                gridView.setAdapter(new Adapter());//绑定适配器
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.e("Error: ", error.getMessage());
            }
        });
        requestQueue.add(req);
    }
    //gridview横向布局方法
    public void horizontal_layout(){
        int size = list.size();
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        float density = dm.density;
        int allWidth = (int) (110 * size * density);
        int itemWidth = (int) (100 * density);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                allWidth, LinearLayout.LayoutParams.FILL_PARENT);
        gridView.setLayoutParams(params);// 设置GirdView布局参数
        gridView.setColumnWidth(itemWidth);// 列表项宽
        gridView.setHorizontalSpacing(10);// 列表项水平间距
        gridView.setStretchMode(GridView.NO_STRETCH);
        gridView.setNumColumns(size);//总长度
    }
    //适配器
    class Adapter extends BaseAdapter {
        ImageLoader imageLoader = new ImageLoader(requestQueue, new BitmapCache());

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        class ViewHolder {
            TextView coursename;
            NetworkImageView imageView;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = getLayoutInflater().inflate(R.layout.view1, null);
                viewHolder.coursename = (TextView) convertView.findViewById(R.id.coursename);
                viewHolder.imageView = (NetworkImageView) convertView.findViewById(R.id.imageView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            viewHolder.coursename.setText(list.get(position).coursename);
            viewHolder.imageView.setImageUrl(list.get(position).courseimg, imageLoader);

            return convertView;
        }
    }

}

你可能感兴趣的:(Android控件)