ListView - 在整个 ListView 高度上拉伸单个项目

ListView - stretch single item across the whole ListView height

如果我在 ListView 上的 ArrayAdapter 中有一个 single 项目,我会喜欢在整个 ListView height stretch that one item(所以我有一个的文本屏幕中央的项目)。

像这样:

这真的很容易用 RecyclerView 完成(上图),但在我的情况下,我现在负担不起在 RecyclerView 上重建 ListView 屏幕。我尝试将布局项的高度设置为屏幕高度 "match_parent" 或在代码中覆盖 80dp 值。它对我的情况没有任何影响,结果如下所示:

这就是我创建 ArrayAdapter 的方式:

string[] noResultsItem = { Resources.GetString(Resource.String.no_network) };
playlistListView.Adapter = new ArrayAdapter<string>(this, Resource.Layout.no_result_row, Resource.Id.textTop, noResultsItem);

这是 ArrayAdapter 项目的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:id="@+id/noResultsRow">
    <TextView
        android:id="@+id/textTop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textSize="18dip"
        android:textColor="#ffffff"
        android:text=""
        android:gravity="center_horizontal" />
</RelativeLayout>

我可以想象一个隐藏和显示 ListView 并将其替换为居中的 TextView 的解决方案,但我不太喜欢该解决方案。

使用相对布局高度作为

 android:layout_height="80dp" 

上替换它

android:layout_height="match_parent" 并且重力应该是中心或者你可以采用 LinearLayout

使用listview的item显示没有数据是不好的做法。使用而不是 Textview 或任何表示 FrameLayout 中的列表视图没有数据的 Imageiew,并使用

等视图的可见性
if(data.size > 0){
 listview.setVisibilty(View.VISIBLE);
 textview.setVisibility(View.GONE);
}else{
 listview.setVisibilty(View.GONE);
 textview.setVisibility(View.VISIBLE);
}

通过为一项创建我自己的简单适配器解决:

public class SingleItemAdapter : BaseAdapter
{
    private readonly Context context;
    private readonly ListView listView;
    private readonly int textViewResourceId;
    private readonly int resource;
    private readonly int textResourceId;

    public SingleItemAdapter(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer) { }

    public SingleItemAdapter(Context context, ListView listView, int resource, int textViewResourceId, int textResourceId)
    {
        this.context = context;
        this.textViewResourceId = textViewResourceId;
        this.resource = resource;
        this.textResourceId = textResourceId;
        this.listView = listView;

        listView.SetSelector(Android.Resource.Color.Transparent);
        listView.CacheColorHint = Android.Graphics.Color.Transparent;
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        if (convertView == null)
        {
            var inflater = LayoutInflater.From(context);
            var view = inflater.Inflate(resource, parent, false);
            return CustomizeView(view);
        }
        else
        {
            return CustomizeView(convertView);
        }
    }

    private View CustomizeView(View view)
    {
        view.LayoutParameters.Height = listView.Height;
        var textTop = view.FindViewById<TextView>(textViewResourceId);
        textTop.Text = context.GetString(textResourceId);
        return view;
    }

    public override Object GetItem(int position)
    {
        return context.GetString(textResourceId);
    }

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

    public override int Count => 1;
}

这是适配器中包含的项目的更新布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:gravity="center_horizontal|center_vertical"
    android:id="@+id/noResultsRow"
    android:orientation="vertical">
    <TextView
        android:id="@+id/textTop"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="18dip"
        android:textColor="#ffffff"
        android:text=""
        android:gravity="center" />
</LinearLayout>