【问题标题】:Xamarin Highlight rows in ListViewXamarin 突出显示 ListView 中的行
【发布时间】:2019-02-14 11:07:22
【问题描述】:

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

我还可以自动向上/向下滚动以显示当前不在屏幕范围内的行吗?例如,如果我当前突出显示第 2 行,接下来我需要突出显示第 15 行,我希望应用程序自动向下滚动并突出显示第 15 行。有可能吗?

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

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;
        }
    }

下面列出的是我的列表视图布局。

<?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>

请建议我这样做是否正确。我是新来的。我也想知道在上述实现的情况下如何突出显示我选择的行。

【问题讨论】:

    标签: listview xamarin xamarin.forms xamarin.android


    【解决方案1】:

    在您的 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);
    

    在这里,为视图分配一个标签属性。即 view.setTag(position)。

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

    【讨论】:

      猜你喜欢
      • 2012-01-22
      • 2017-09-24
      • 1970-01-01
      • 1970-01-01
      • 2019-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多