代码之家  ›  专栏  ›  技术社区  ›  Thomas

Xamarin在ListView中突出显示行

  •  0
  • Thomas  · 技术社区  · 8 年前

    我是Xamarin/Android新手。我有一个大约20行的列表视图(要查看最后几行,用户需要向下滚动)。我需要根据一些计算值自动突出显示随机行。我的问题是,一旦填充了listview(我有一个自定义适配器来填充listview),如何设置所选行的背景色或前景色(比如说第三行,高亮显示10秒,然后高亮显示第一行-行号是基于计算的)。

    下面是我的自定义适配器。

    public class PeopleListAdapter: BaseAdapter<Content>
        {
            List<Content> content;
            Activity context;
            public PeopleListAdapter(Activity context, List<Content> content) : base()
            {
                this.context = context;
                this.content = content;
            }
            public override long GetItemId(int position)
            {
                return position;
            }
            public override Content this[int position]
            {
                get { return content[position]; }
            }
            public override int Count
            {
                get { return content.Count; }
            }
            public override View GetView(int position, View convertView, ViewGroup parent)
            {
                View view = convertView; // re-use an existing view, if one is available
                if (view == null) // otherwise create a new one
                    view = context.LayoutInflater.Inflate(Resource.Layout.listlayout, null);
                view.FindViewById<TextView>(Resource.Id.txtValues).Text = content[position].Text;
                return view;
            }
        }
    

    下面列出的是我的listview布局。

    <?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="match_parent"
        android:minWidth="25px"
        android:minHeight="25px">
    
      <ListView
           android:minWidth="25px"
           android:minHeight="25px"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:id="@+id/list" >    
      </ListView>
    </LinearLayout>
    

    我让适配器填充下面的布局(listlayout.axml):

    <?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="match_parent">
        <TextView
            android:text="Text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/txtValues" />
    </LinearLayout>
    

    请建议我是否做得对。我是新来的。我还想知道,鉴于上述实现,如何突出显示我选择的行。

    1 回复  |  直到 8 年前
        1
  •  0
  •   Ashray P. Shetty    8 年前

    在getview方法中,您有一个为每一行充气的视图:

     View view = convertView; // re-use an existing view, if one is available
    
     if (view == null) // otherwise create a new one
     view = context.LayoutInflater.Inflate(Resource.Layout.listlayout, null);
    

    然后在相同的方法getview中,添加一个检查if(Integer.parserInt(view.getTag())==positionToHighlight),然后设置视图的背景色。例如:view.setBackground()

    推荐文章