在ItemLongClick listveiw上重用DialogFragment

时间:2016-09-27 22:34:38

标签: c# android android-fragments xamarin xamarin.android

我正在尝试设置一个能够在ListView的ItemLongClick上显示的DialogFragment Layout。对于listview中的每个项,DialogFragment必须是相同的布局。我也在使用Xamarin和C#

调用对话框片段的代码块。此代码位于listview_ItemLongClick(对象发件人,AdapterView.ItemLongClickEventArgs e)内。

到目前为止,我已经尝试过:

Android.App.FragmentTransaction transaction_prev = FragmentManager.BeginTransaction().AddToBackStack(null);
        dialog_preview_busn previewBusn = new dialog_preview_busn(gName, gAddress, gZip, gNumber, gEmail, gWebsite, gHours, gImage);
        previewBusn.Show(transaction_prev, "Dialog Preview Business");

此外,我还尝试使用Xamarin文档来设置片段

当我退出DialogFragment并尝试重新打开它时(在相同的项目上和不同的项目上),这也是我得到的错误

Android.Views.InflateException: Binary XML file line #1: Error inflating class fragment

给我错误的行是

var view = inflater.Inflate(Resource.Layout.dialog_preview_busn, container, false);

这是XML文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/dialogLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:minWidth="300dp"
android:minHeight="200dp"
android:weightSum="100"
android:gravity="center">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="15"
android:id="@+id/relativeLayout2"
android:minWidth="25px"
android:minHeight="25px">
<ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_marginBottom="5dp"
    android:indeterminate="true"
    android:background="@drawable/progressBarDesign"
    android:id="@+id/fragProgress"
    android:visibility="invisible" />
</RelativeLayout>
<TextView
    android:text=""
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="10"
    android:gravity="center"
    android:textSize="20sp"
    android:id="@+id/txtName" />
<LinearLayout
    android:background="@drawable/dialogLayout"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="5">
    <LinearLayout
        android:background="@drawable/dialogLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="100">
        <TextView
            android:text="Address:  "
            android:layout_width="0dp"
            android:layout_weight="50"
            android:layout_height="fill_parent"
            android:gravity="right"
            android:textSize="15sp"
            android:id="@+id/labAddess" />
        <TextView
            android:text=""
            android:layout_width="0dp"
            android:layout_weight="50"
            android:layout_height="fill_parent"
            android:gravity="left"
            android:textSize="15sp"
            android:id="@+id/txtAddress"
            android:layout_below="@id/txtName" />
    </LinearLayout>
</LinearLayout>
<LinearLayout
    android:background="@drawable/dialogLayout"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="5">
    <LinearLayout
        android:background="@drawable/dialogLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="100">
        <TextView
            android:text="Number:  "
            android:layout_width="0dp"
            android:layout_weight="50"
            android:layout_height="fill_parent"
            android:gravity="right"
            android:textSize="15sp"
            android:id="@+id/labNumber" />
        <TextView
            android:text=""
            android:layout_width="0dp"
            android:layout_weight="50"
            android:layout_height="fill_parent"
            android:gravity="left"
            android:textSize="15sp"
            android:id="@+id/txtNumber"
            android:layout_below="@id/txtAddress" />
    </LinearLayout>
</LinearLayout>
<LinearLayout
    android:background="@drawable/dialogLayout"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="5">
    <LinearLayout
        android:background="@drawable/dialogLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="100">
        <TextView
            android:text="Email:  "
            android:layout_width="0dp"
            android:layout_weight="50"
            android:layout_height="fill_parent"
            android:gravity="right"
            android:textSize="15sp"
            android:id="@+id/labEmail" />
        <TextView
            android:text=""
            android:layout_width="0dp"
            android:layout_weight="50"
            android:layout_height="fill_parent"
            android:gravity="left"
            android:textSize="15sp"
            android:id="@+id/txtEmail"
            android:layout_below="@id/txtNumber" />
    </LinearLayout>
</LinearLayout>
<LinearLayout
    android:background="@drawable/dialogLayout"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="5">
    <LinearLayout
        android:background="@drawable/dialogLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="100">
        <TextView
            android:text="Website:  "
            android:layout_width="0dp"
            android:layout_weight="50"
            android:layout_height="fill_parent"
            android:gravity="right"
            android:textSize="15sp"
            android:id="@+id/labEmail" />
        <TextView
            android:text=""
            android:layout_width="0dp"
            android:layout_weight="50"
            android:layout_height="fill_parent"
            android:gravity="left"
            android:textSize="15sp"
            android:id="@+id/txtWebsite"
            android:layout_below="@id/txtEmail" />
    </LinearLayout>
</LinearLayout>
<TextView
    android:text="Hours"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="5"
    android:gravity="center"
    android:textSize="15sp"
    android:id="@+id/labHours"
    android:layout_below="@id/txtWebsite" />
<TextView
    android:text=""
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="5"
    android:gravity="center"
    android:textSize="15sp"
    android:id="@+id/txtHours"
    android:layout_below="@id/labEmail" />
<ImageView
    android:layout_width="30sp"
    android:layout_height="30sp"
    android:paddingTop="6sp"
    android:layout_gravity="center"
    android:background="#4A115C"
    android:src="@drawable/menu_down_arrow"
    android:id="@+id/downArrow"/>
<LinearLayout
    android:background="@drawable/dialogLayout"
    android:id="@+id/mapLayout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="5">
<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="200sp"
    android:layout_marginLeft="10sp"
    android:layout_marginRight="10sp"
    android:layout_marginTop="15sp"
    android:layout_marginBottom="10sp"
    android:name="com.google.android.gms.maps.MapFragment" />
</LinearLayout>
</LinearLayout>

对于dialog_prev_busn.cs:

    using Android.App;
    using Android.OS;
    using Android.Views;
    using Android.Widget;
    using Android.Graphics;
    using Android.Gms.Maps;
    using System;
    using Android.Gms.Maps.Model;

    namespace My_Town_App.Droid
    {
        class dialog_preview_busn : DialogFragment
        {
            public string gName, gAddress, gNumber, gEmail, gWebsite, gHours, gImage;
            public int gZip;
            private GoogleMap gMap;

            public dialog_preview_busn(string Name, string Address, int Zip, string Number, string Email, string Website, string Hours, string Image)
            {
                gName = Name;
                gAddress = Address;
                gZip = Zip;
                gNumber = Number;
                gEmail = Email;
                gWebsite = Website;
                gHours = Hours;
                gImage = Image;
                System.Diagnostics.Debug.Write("-----------------");
                System.Diagnostics.Debug.Write("dialog_preview_busn");
                System.Diagnostics.Debug.Write("-----------------");
                System.Diagnostics.Debug.Write("Name: " + Name + "   Address: " + Address + "    Zip: " + Zip + "    Number: " + Number);
                System.Diagnostics.Debug.Write("Email: " + Email + "    Website: " + Website + "    Hours: " + Hours + "    Image: " + Image);
                //gOnLogInComplete.Invoke(this, new OnLogInEventArgs(gtxtEmail.Text, gtxtPassword.Text));
                //PassedParams.Invoke(this, new PassedParams(user, pass));
            }

            //----------------------------------------
            // OnCreate Activity
            //----------------------------------------
            public override void OnActivityCreated(Bundle savedInstanceState)
            {
                Dialog.Window.RequestFeature(WindowFeatures.NoTitle); //Set the Title Bar to invisable
                base.OnActivityCreated(savedInstanceState);

                TextView txtName = View.FindViewById<TextView>(Resource.Id.txtName);
                TextView txtAdress = View.FindViewById<TextView>(Resource.Id.txtAddress);
                TextView txtNumber = View.FindViewById<TextView>(Resource.Id.txtNumber);
                TextView txtEmail = View.FindViewById<TextView>(Resource.Id.txtEmail);
                TextView txtWebsite = View.FindViewById<TextView>(Resource.Id.txtWebsite);
                TextView txtHours = View.FindViewById<TextView>(Resource.Id.txtHours);
                TextView labHours = View.FindViewById<TextView>(Resource.Id.labHours);
                ImageView downArrow = View.FindViewById<ImageView>(Resource.Id.downArrow);
                LinearLayout mapLayout = View.FindViewById<LinearLayout>(Resource.Id.mapLayout);

                mapLayout.Visibility = ViewStates.Gone;
                SetUpMap();
                downArrow.Click += delegate
                {
                    System.Diagnostics.Debug.Write("down arrow is working");
                    if (mapLayout.Visibility == ViewStates.Visible)
                    {
                        downArrow.SetImageResource(Resource.Drawable.menu_down_arrow);
                        mapLayout.Visibility = ViewStates.Gone;
                    }
                    else if (mapLayout.Visibility == ViewStates.Gone)
                    {
                        downArrow.SetImageResource(Resource.Drawable.menu_up_arrow);
                        mapLayout.Visibility = ViewStates.Visible;
                    }

                };

                txtName.Text = gName;
                txtName.PaintFlags = PaintFlags.UnderlineText;

                txtAdress.Text = (gAddress + " " + gZip).ToString();
                txtNumber.Text = gNumber;
                txtEmail.Text = gEmail;
                txtWebsite.Text = gWebsite;
                string[] aryHours = gHours.Split(',');
                foreach (string s in aryHours)
                {
                    txtHours.Text += (s + " ").ToString();
                }
                labHours.PaintFlags = PaintFlags.UnderlineText;

                Dialog.Window.Attributes.WindowAnimations = Resource.Style.dialog_animation; //set the animation
            }

            //----------------------------------------
            // OnCreate View
            //----------------------------------------

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

                var view = inflater.Inflate(Resource.Layout.dialog_preview_busn, container, false);
                return view;

            }

            private void SetUpMap()
            {
                if (gMap == null)
                {
                    FragmentManager.FindFragmentById<MapFragment>(Resource.Id.map);
                }


                LatLng location = new LatLng(50.897778, 3.013333);
                CameraPosition.Builder builder = CameraPosition.InvokeBuilder();
                builder.Target(location);
                builder.Zoom(18);
                builder.Bearing(155);
                builder.Tilt(65);
                CameraPosition cameraPosition = builder.Build();
                CameraUpdate cameraUpdate = CameraUpdateFactory.NewCameraPosition(cameraPosition);
                MapFragment mapFrag = (MapFragment)FragmentManager.FindFragmentById<MapFragment>(Resource.Id.map);
                GoogleMap map = mapFrag.Map;

                if (map != null)
                {
                    map.MoveCamera(cameraUpdate);
                }
            }        

            public void OnMapReady(GoogleMap googleMap)
            {
                gMap = googleMap;
            }
        }
    }

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

嘿大家为此我在Fragments xml中有一个MapFragment,当重新打开dialogFragment时会抛出一个错误,因为Fragment中已经存在一个具有相同ID的MapFragment。所以我从Fragment中删除了MapFragment。所以现在它固定了。