java翻译到mono C#实现系列(2) mono实现GridView 横向滚动

群里的朋友问GridView 横向滚动怎么实现,我就百度了,参考http://blog.csdn.net/lonely_fireworks/article/details/7841134写了个mono版的.

本人菜鸟,请各位多多指点,不足之处,请斧正.没啥技术含量,就权当丰富下mono for android的小代码.

目录结构

QQ图片20130804154215

activity_main.axml 代码:

<?xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent" >

        <HorizontalScrollView

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:scrollbars="none" >



            <LinearLayout

                android:layout_width="fill_parent"

                android:layout_height="wrap_content"

                android:orientation="horizontal" >

                

                <GridView

                    android:id="@+id/gridview"

                    android:layout_width="fill_parent"

                    android:layout_height="wrap_content"

                    android:layout_gravity="center"

                    android:columnWidth="90dp"

                    android:stretchMode="columnWidth"

                    >

                </GridView>

            </LinearLayout>

        </HorizontalScrollView>

    </LinearLayout>

gridview_item.axml 代码:

<?xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:orientation="vertical" >



        <ImageView

            android:layout_width="100dp"

            android:layout_height="100dp"

            android:background="#00ff00" />



        <TextView

            android:id="@+id/item_textview"

            android:layout_width="100dp"

            android:layout_height="20dp"

            android:gravity="center"

            android:text="TextView"

            />



    </LinearLayout>

Activity1.cs 代码:

using System;



using Android.App;

using Android.Content;

using Android.Runtime;

using Android.Views;

using Android.Widget;

using Android.OS;

using Android.Util;

using System.Collections.Generic;

using System.Collections;

//GridView 横向滚动

//参考http://blog.csdn.net/lonely_fireworks/article/details/7841134

namespace AndroidApplication1

{

    [Activity(Label = "AndroidApplication1", MainLauncher = true, Icon = "@drawable/icon")]

    public class Activity1 : Activity

    {

        private GridView gridView;

        private LayoutInflater inflater;

        private IList<string> dataList = new List<string>();



        protected override void OnCreate(Bundle savedInstanceState)

        {

            base.OnCreate(savedInstanceState);



            // Set our view from the "main" layout resource

            SetContentView(Resource.Layout.activity_main);



            gridView = FindViewById<GridView>(Resource.Id.gridview);

            for (int i = 0; i < 10; i++)

            {

                dataList.Add("测试" + i);

            }

            inflater = (LayoutInflater)this.GetSystemService(Context.LayoutInflaterService);

            GridViewAdapter adapter = new GridViewAdapter(this, dataList);

            gridView.Adapter = adapter;

            int size = dataList.Count;

            DisplayMetrics dm = new DisplayMetrics();

            WindowManager.DefaultDisplay.GetMetrics(dm);

            float density = dm.Density;

            int allWidth = (int)(110 * size * density);

            int itemWidth = (int)(100 * density);

            LinearLayout.LayoutParams par = new LinearLayout.LayoutParams(allWidth, LinearLayout.LayoutParams.FillParent);

            gridView.LayoutParameters = par;

            gridView.SetColumnWidth(itemWidth);

            gridView.SetHorizontalSpacing(10);

            gridView.SetNumColumns(size);

        }



        public override bool OnCreateOptionsMenu(IMenu menu)

        {

            MenuInflater.Inflate(Resource.Layout.activity_main, menu);

            return true;

        }

        public class GridViewAdapter : BaseAdapter

        {

            private Activity context = null;

            public IList<string> dataList = null;



            /// <summary>

            /// 默认构造器

            /// </summary>

            public GridViewAdapter()

                : base()

            {



            }



            /// <summary>

            /// 带参构造器

            /// </summary>

            /// <param name="context"></param>

            /// <param name="list"></param>

            public GridViewAdapter(Activity context, IList<string> dataList)

                : base()

            {

                this.context = context;

                this.dataList = dataList;

            }



            public override int Count

            {

                get

                {

                    return dataList.Count;

                }

            }



            public override long GetItemId(int position)

            {

                return position;

            }



            public override Java.Lang.Object GetItem(int position)

            {

                return position;

            }



            public override View GetView(int position, View convertView, ViewGroup parent)

            {

                convertView = context.LayoutInflater.Inflate(Resource.Layout.gridview_item, null);

                TextView textView = (TextView)convertView

                                    .FindViewById(Resource.Id.item_textview);

                String str = dataList[position];

                textView.SetText(dataList[position], TextView.BufferType.Normal);

                return convertView;

            }

        }

    }

}

 

原java版实现 代码:

public class MainActivity extends Activity

{



    private GridView gridView;

    private LayoutInflater inflater;

    private List<String> dataList = new ArrayList<String>();



    //    @Override

    public void onCreate(Bundle savedInstanceState)

    {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        gridView = (GridView) this.findViewById(R.id.gridview);

        for (int i = 0; i < 10; i++)

        {

            dataList.add("测试" + i);

        }

        inflater = (LayoutInflater) this

                   .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        GridViewAdapter adapter = new GridViewAdapter();

        gridView.setAdapter(adapter);

        int size = dataList.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);

        gridView.setColumnWidth(itemWidth);

        gridView.setHorizontalSpacing(10);

        gridView.setStretchMode(GridView.NO_STRETCH);

        gridView.setNumColumns(size);

    }



    @Override

    public boolean onCreateOptionsMenu(Menu menu)

    {

        getMenuInflater().inflate(R.menu.activity_main, menu);

        return true;

    }



    final class GridViewAdapter extends BaseAdapter

    {



        @Override

        public int getCount()

        {

            return dataList.size();

        }



        @Override

        public Object getItem(int position)

        {

            return dataList.get(position);

        }



        @Override

        public long getItemId(int position)

        {

            return position;

        }



        @Override

        public View getView(int position, View convertView, ViewGroup parent)

        {

            convertView = inflater.inflate(R.layout.gridview_item, null);

            TextView textView = (TextView) convertView

                                .findViewById(R.id.item_textview);

            String str = dataList.get(position);

            textView.setText(str);

            return convertView;

        }



    }

}

源码下载: http://www.400gb.com/file/26907440

你可能感兴趣的:(GridView)