片段xamarin中的自定义列表视图(如何从片段调用自定义列表视图适配器)

时间:2016-10-19 05:18:21

标签: c# xamarin xamarin.android xamarin.forms

(如何从片段调用自定义列表视图适配器)

以任何方式调用自定义列表视图适配器并显示列表。

我有“Home.axml”和Home.cs(列表片段)

我需要从Home.cs(List片段)调用一个自定义列表视图适配器并在Home.axml中显示它

下面是我的代码。

片段

namespace AndroidApp2.Fragments
{
    public class Home : ListFragment
    {
         public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
          {  
        var adapter = new ArrayAdapter<String>(Activity, Android.Resource.Layout.SimpleListItemChecked, allheading);
        ListAdapter = adapter; 
        return base.OnCreateView(inflater, container, savedInstanceState);
          }    
    }
}

以上代码正在使用,它对我来说很好。但是如何创建一个能够从片段调用的服装列表视图适配器?

Home.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"
    android:minWidth="25px"
    android:minHeight="25px">
    <fragment
        android:id="@+id/heading_fragment"
        android:layout_weight="1"
        android:layout_width="0px"
        android:layout_height="match_parent" />
    <FrameLayout
        android:id="@+id/details"
        android:layout_weight="1"
        android:layout_width="0px"
        android:layout_height="match_parent" />
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

Take a look on this code. I manged to call it from fragment, i only had to set the context properly otherwise i got this error "Window.getLayoutInflater()' on a null object reference".



        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            var view = inflater.Inflate(Resource.Layout.TagDialog, container, false);
            Button confirmBtn = view.FindViewById<Button>(Resource.Id.ConfirmBtn);
            Button cancelBtn = view.FindViewById<Button>(Resource.Id.CancelBtn);

            AbsenceService service = new AbsenceService();
            ListViewTags = view.FindViewById<ListView>(Resource.Id.TagListView);
            TagItems = service.GetTags(1);
            ListViewTags.Adapter = new Cards_TagListAdapter(context, TagItems);

            // "Cancel" click
            cancelBtn.Click += delegate {
                Dismiss();
            };

            confirmBtn.Click += ConfirmBtn_Click;
            return view;
        }