如何制作单行水平ListView / List?

时间:2015-05-04 22:45:34

标签: xamarin xamarin.android

我想要做的是创建单行列表视图/带有图像和文本的列表这就是我现在所拥有的

我已将numColumns更改为1,但如何更改它以使其为水平和可滚动?图像应该是一个顶部,文本应该在它下面?

这是布局

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:numColumns="1"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
/>

单网格项目

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:scaleType="centerCrop"
        android:id="@+id/my_image_vieww" />
    <TextView
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/my_text_view" />
</LinearLayout>

主要活动

var gridview = FindViewById<GridView> (Resource.Id.gridview);
            gridview.Adapter = new ImageAdapter (this);

            gridview.ItemClick += delegate (object sender, AdapterView.ItemClickEventArgs args) {
                Toast.MakeText (this, args.Position.ToString (), ToastLength.Short).Show ();
            };



public class ImageAdapter : BaseAdapter
    {
        Context context;

        public ImageAdapter (Context c)
        {
            context = c;
        }

        public override int Count {
            get { return thumbIds.Length; }
        }

        public override Java.Lang.Object GetItem (int position)
        {
            return null;
        }

        public override long GetItemId (int position)
        {
            return 0;
        }

        // create a new ImageView for each item referenced by the Adapter
        public override View GetView (int position, View convertView, ViewGroup parent)
        {
            ImageView imageView;
            View view;
            if (convertView == null) {  // if it's not recycled, initialize some attributes
                view = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.my_grid_row, parent, false);
            } else {
                view = convertView;
            }

            var imageView2 = view.FindViewById<ImageView>(Resource.Id.my_image_vieww);
            imageView2.SetImageResource (thumbIds[position]);

            var textView = view.FindViewById<TextView>(Resource.Id.my_text_view);
            textView.Text = "Text to Display";
            return view;
        }


    }

1 个答案:

答案 0 :(得分:0)

请不要用户水平列表视图。 有预定义的android控件来实现这一点。 查看寻呼机

示例代码:

var _viewPager = view.FindViewById<ViewPager>(Resource.Id.viewPager);
            var _viewPageIndicatorCircle = view.FindViewById<CirclePageIndicator>(Resource.Id.viewPageIndicator);

            _viewPager.Adapter = new TestFragmentAdapter(fm);
            _viewPageIndicatorCircle.SetViewPager(_viewPager);


public class TestFragmentAdapter : FragmentPagerAdapter
    {

        public TestFragmentAdapter(Android.Support.V4.App.FragmentManager fm)
            : base(fm)
        {

        }

        public override Android.Support.V4.App.Fragment GetItem(int position)
        {
            return new TestFragment();
        }

        public override int Count
        {
            get
            {
                return 5;
            }
        }

    }

class TestFragment : Android.Support.V4.App.Fragment
    {

        public TestFragment()
        {
        }


        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            var view = inflater.Inflate(Resource.Layout.MyViewPager , container, false);
            return view;
        }


    }

Viewpager.xml

<android.support.v4.view.ViewPager
                        android:id="@+id/viewPager"
                        android:layout_width="fill_parent"
                        android:layout_height="209.6dp" />

如果您有任何疑问,请随时问我