簡體   English   中英

在ItemLongClick listveiw上重用DialogFragment

[英]Re-using a DialogFragment on ItemLongClick listveiw

我正在嘗試設置一個DialogFragment Layout,我可以在ListView的ItemLongClick上顯示。 對於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;
            }
        }
    }

提前感謝您的幫助!

大家為此我在Fragments xml中有一個MapFragment,當重新打開dialogFragment時會拋出一個錯誤,因為已經存在一個在Fragment內部存在相同ID的MapFragment。 所以我從Fragment中刪除了MapFragment。 所以現在它固定了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM