在android系统xamarin ListView和评价吧 [英] listview and rating bar in android xamarin

查看:164
本文介绍了在android系统xamarin ListView和评价吧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图中,每个项目都有一个文本视图和评价吧。
项目布局是:

I have a list view , each item has a text view and a rating bar. the Item layout is :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="50dp"
    android:orientation="horizontal">
  <TextView
      android:id="@+id/name"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:lineSpacingExtra="3dp"
      android:paddingRight="10dp"
      android:paddingTop="5dp"
      android:textColor="#ffffff"
      android:textStyle="bold"
       android:textSize="15sp"
      android:typeface="sans"
       android:layout_alignParentRight="true"/>
  <RatingBar android:id="@+id/ratingbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/name"

        android:numStars="5"
        android:stepSize="1.0"
             android:layout_alignParentLeft="true" />

</RelativeLayout>

这是我ListAdapter的GetView方法:

this is GetView method of my ListAdapter:

public override View GetView(int position, View convertView, ViewGroup parent)
        {
            View view = convertView;

            RatingBar rate; 

            if (view == null)
                view = context.LayoutInflater.Inflate(Resource.Layout.QuestionListViewItemLayout, parent, false);
               // view = context.LayoutInflater.Inflate(Android.Resource.Layout.SimpleListItemActivated1, parent, false);

            question item = this[position];
            view.FindViewById<TextView>(Resource.Id.name).Text = item.text; 
            view.FindViewById<RatingBar>(Resource.Id.ratingbar).Rating = item.userrate;
            RatingBar rb = view.FindViewById<RatingBar>(Resource.Id.ratingbar);

            rb.RatingBarChange += (o, e) =>
            {
                item.userrate = System.Convert.ToInt32( e.Rating);
            };
            //view.FindViewById<TextView>(Resource.Id.SanadNumber).Text = item.sanadNumber.ToString();


            return view;
        }

在该行:rb.RatingBarChange当我改变了ratingbars之一在我的列表视图中的一些其他项目的变化。该委托执行铁道部超过一次,每次我设置的评级我的评价之一吧。

in the line : "rb.RatingBarChange " when I change one of the ratingbars in my list view some other items are change. that delegate execute mor than one time each time I set rating for one of my rating bar.

所以,当我读到它通过评价栏主编的item.userrate,没有正确的答案。

so when I read " item.userrate" which edited by rating bar , there is not correct answer.

推荐答案

我想在我的列表视图有问题(S)的列表。
的问题类是这样的:

I want to have a list of Question(s) in my list view. the question class is like this:

public  class question
    {
        public int id { get; set; }
        public string text { get; set; }
        public int questionorder { get; set; }
        public int userrate { get; set; }

        public question()
        {
            userrate = 0;

        }

    }

现在我需要一个QuestionListViewItemLayout.axml文件我的样式列表视图:

now I need a "QuestionListViewItemLayout.axml" file for styling my list view:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="50dp"
    android:orientation="horizontal">
  <TextView
      android:id="@+id/name"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:lineSpacingExtra="3dp"
      android:paddingRight="10dp"
      android:paddingTop="5dp"
      android:textColor="#ffffff"
      android:textStyle="bold"
       android:textSize="15sp"
      android:typeface="sans"now 
       android:layout_alignParentRight="true"/>
  <RatingBar android:id="@+id/ratingbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/name"

        android:numStars="5"
        android:stepSize="1.0"
             android:layout_alignParentLeft="true" />

</RelativeLayout>

现在我需要一个ListAdapter来管理我的列表视图项,但首先我必须创建一个包装类保存视图:

now I need a ListAdapter to manage Items in my List view, but first I have to create a Wrapper class to save views :

 public class ViewWrapper : Java.Lang.Object
    {
        View base1;
        RatingBar rate = null;
        TextView label = null;

      public  ViewWrapper(View base1)
        {
            this.base1 = base1;
        }

      public  RatingBar getRatingBar()
        {
            if (rate == null)
            {
                rate = base1.FindViewById<RatingBar>(Resource.Id.ratingbar);
            }

            return (rate);
        }

       public TextView getLabel()
        {
            if (label == null)
            {
                label = base1.FindViewById<TextView>(Resource.Id.name);
            }

            return (label);
        }
    }

本类创建只是我的问题类,也许你需要创建您自己的包装相似,上面的代码

this class created just for my question class and maybe you need to create your own wrapper similar to above code

是时候创建列表适配器类:

it's time to create List adapter class :

public class QuestionListAdapter : BaseAdapter<question>
    {
        Activity context;
        List<question> list;
        public QuestionListAdapter(Activity _context, List<question> _list)
            : base()
        {
            this.context = _context;
            this.list = _list;
        }

        public override int Count
        {
            get { return list.Count; }
        }
        public override long GetItemId(int position)
        {
            return position;
        }
        public List<question> GetList()
        {
            return list;
        }
        public override question this[int index]
        {
            get { return list[index]; }
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            View view = convertView;

            ViewWrapper wrapper;
            RatingBar rate;    

            question item = this[position];
            if (view == null)
            {
                view = context.LayoutInflater.Inflate(Resource.Layout.QuestionListViewItemLayout, parent, false);
                wrapper = new ViewWrapper(view);
                view.SetTag(Resource.Id.holder, wrapper);
                rate = wrapper.getRatingBar();

                rate.RatingBarChange += (o, e) =>
                {
                    RatingBar ratingBar = o as RatingBar;
                    int myPosition = (int)ratingBar.GetTag(Resource.Id.holder);
                    question model = list[myPosition];
                    model.userrate = System.Convert.ToInt32(e.Rating);
                };
            }
            else
            {

                wrapper = (ViewWrapper)view.GetTag(Resource.Id.holder);
                rate = wrapper.getRatingBar();

            }

            question model1 = list[position];

            wrapper.getLabel().Text = model1.text;
            rate.SetTag(Resource.Id.holder, position);
            rate.Rating = model1.userrate;

            return view;
        }



每一个视图中创建时间,我为它创建一个包装,放入视图标签
节:[IF(查看== NULL)
,并为评级酒吧利率变化事件

each time a view created I create a wrapper for it and put it into view's tag Section : [if (view == null)] and set an event for rating bar rate changes.

其他时间查看负荷。我得到的标签,并设置等级栏的评级表吧。

other time the view loads I get the tag and set the rating of rating bar form it.

这篇关于在android系统xamarin ListView和评价吧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆