带表的可扩展ListView

时间:2017-08-17 08:24:36

标签: xamarin xamarin.android expandablelistview

Expandable ListView应具备以下功能

对于第一步,我需要知道如何用这三种能力创建这个可扩展视图,小,中,大(带表)

主:

public class En : AppCompatActivity
{

    public List<Ml> list;
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        list = new List<Mdel>();
        for (int i = 0; i < 30; i++)
        {
            if (i % 5 == 0)
                list.Add(new Mdel
                {

                });
            else
            {
                list.Add(new Mdel
                {
                    Na = "N " + i,
                    Dels = "Dels " + i,
                    Ln = "Loon " + i,
                    Te = "Te " + i,
                    isDone = false
                });
            }
        }

        ListView lv = (ListView)FindViewById(Resource.Id.oi);
        oi.Adapter = new Myter(this, list);
        oi.Itck += Miew_ItemClick;

}
}

1 个答案:

答案 0 :(得分:1)

  

我的问题是,如何使用此3个步骤创建此视图。小行,med行和带有表格的较大行

我认为这里不需要ExpandableListView,只是从这个问题的描述来看,它仍然是每个项目的一对一关系,我认为ExpandableListView更适合这个案例其中项目之间有root-child关系。

您需要的是一个带有项目视图自定义适配器的自定义适配器。

例如,首先创建数据模型:

public class MyDataModel
{
    public string Name { get; set; }
    public string Time { get; set; }
    public string Location { get; set; }
    public string Details { get; set; }

    public bool isDone { get; set; }
}

使用如下项目布局创建自定义适配器:

public class MyLVAdapter : BaseAdapter<MyDataModel>
{
    private List<MyDataModel> items = new List<MyDataModel>();
    private Activity context;

    public MyLVAdapter(Activity context, List<MyDataModel> items) : base()
    {
        this.context = context;
        this.items = items;
    }

    public override MyDataModel this[int position]
    {
        get { return items[position]; }
    }

    public override int Count
    {
        get { return items.Count; }
    }

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

    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.mycell, null);
        }

        TextView nametv = view.FindViewById<TextView>(Resource.Id.name);
        TextView taltv = view.FindViewById<TextView>(Resource.Id.timeandlocation);
        GridLayout layout = view.FindViewById<GridLayout>(Resource.Id.details);
        TextView detailtime = view.FindViewById<TextView>(Resource.Id.detailtime);
        TextView detailtv = view.FindViewById<TextView>(Resource.Id.detailtv);

        nametv.Text = items[position].Name;
        taltv.Text = "Time: " + items[position].Time + "Location: " + items[position].Location;

        detailtime.Text = items[position].Time;
        detailtv.Text = items[position].Details;

        layout.Visibility = ViewStates.Gone;

        if (items[position].isDone)
        {
            taltv.Visibility = ViewStates.Gone;
        }

        return view;
    }
}

项目视图是这样的,您可以用自己的视图替换它:

<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:id="@+id/name"
            android:layout_height="wrap_content"
            android:layout_width="match_parent" />

  <TextView android:id="@+id/timeandlocation"
      android:layout_height="wrap_content"
      android:layout_width="match_parent" />

  <GridLayout android:id="@+id/details"
                android:layout_height="100dp"
                android:layout_width="match_parent"
              android:rowCount="1"
              android:columnCount="2">

    <TextView android:id="@+id/detailtime"
              android:layout_height="wrap_content"
                android:layout_width="wrap_content" />

    <TextView android:id="@+id/detailtv"
              android:layout_height="wrap_content"
                  android:layout_width="wrap_content" />
  </GridLayout>
</LinearLayout>

我为活动中的测试做了一些假数据:

public List<MyDataModel> list;

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    SetContentView(Resource.Layout.Main);

    list = new List<MyDataModel>();
    for (int i = 0; i < 30; i++)
    {
        if (i % 5 == 0)
            list.Add(new MyDataModel
            {
                Name = "Name " + i,
                Details = "Details " + i,
                Location = "Location " + i,
                Time = "Time " + i,
                isDone = true
            });
        else
        {
            list.Add(new MyDataModel
            {
                Name = "Name " + i,
                Details = "Details " + i,
                Location = "Location " + i,
                Time = "Time " + i,
                isDone = false
            });
        }
    }

    ListView lv = (ListView)FindViewById(Resource.Id.lv);
    lv.Adapter = new MyLVAdapter(this, list);
    lv.ItemClick += MListView_ItemClick;
}

在项目点击事件中,更改视图的可见性:

private void MListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
    var view1 = e.View.FindViewById<TextView>(Resource.Id.timeandlocation);
    var view2 = e.View.FindViewById<GridLayout>(Resource.Id.details);
    if (view2.Visibility == ViewStates.Gone)
    {
        view1.Visibility = ViewStates.Gone;
        view2.Visibility = ViewStates.Visible;
    }
    else
    {
        if (!list[e.Position].isDone)
            view1.Visibility = ViewStates.Visible;
        view2.Visibility = ViewStates.Gone;
    }
}

enter image description here