2016-09-27 54 views
1

我试图设置一个DialogFragment布局,我可以在ListView的ItemLongClick上显示。 DialogFragment必须与列表视图中的每个项目的布局相同。此外,我正在使用Xamarin和C#在ItemLongClick列表上重新使用DialogFragment

正在调用对话框片段的代码块。此代码位于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

那是给我电子线RROR是

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; 
      } 
     } 
    } 

预先感谢您的帮助!

+0

Nongthonbam,我给这个问题增加了一些信息。 –

+0

发布你的片段类 –

+0

好吧我知道它不是最好的,但我一直没有与c#的verylong –

回答

0

嘿大家这个我有一个MapFragment里面的片段xml,当dialogFragment重新打开时抛出一个错误,因为已经有一个MapFragment与片段内存在相同的ID。所以我从我的片段中删除了MapFragment。所以现在它的固定。

相关问题