簡體   English   中英

將片段添加到以編程方式生成的FrameLayout

[英]Add fragment to programmatically generated FrameLayout

我繼承了一些代碼,需要改變它的工作方式。 原始方式現在沒有靈活性。

應用程序是表單生成器,因此必須按需創建UI。 這是Xamarin原生,而不是Xamarin形式。

正在以編程方式創建每個表單問題的FrameLayout,添加到視圖中,然后將一個片段添加到此FrameLayout。 一旦加載了UI以顯示進度循環,所有這一切都發生在OnCreateView之后。

經過一系列例外后,我已經陷入了異常

Java.Lang.IllegalArgumentException: No view found for id 0x50 (unknown) for fragment UploadFragment{a31e878 #7 id=0x50 upload_80}

我的猜測是,當試圖顯示片段時,FrameLayout不存在。

OnCreateView()完成后運行OnCreate()方法后發生異常。

我無法找到以編程方式使用Fragments添加FrameLayouts的任何代碼先例。

代碼片段

frame = new FrameLayout(this.Context);
frame.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
upload = new Widgets.UploadFragment(control, binding, Inflater, a, xFormInstance);
MainFormLayout.AddView(frame);
frame.Id = control.id;
fragmentTx.Add(frame.Id, upload, $"upload_{control.id}");    
fragmentTx.Commit();

任何建議將不勝感激。 謝謝。

擴展說明

放入它所做的一切可能會有點多,但會嘗試盡可能多地投入。 頁面的層次結構是

Activity -> FormFragment -> UploadFragment

所以UploadFragment的父級也是一個片段,而不是Activity。

上傳片段

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
    <RelativeLayout>
        <LinearLayout>
            <TextView/>
            <ImageButton/>
        </LinearLayout>
        <ImageView/>
    </RelativeLayout>
</LinearLayout>

 public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
        }

 public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            // Use this to return your custom view for this Fragment
            _inflater = inflater;
            v = _inflater.Inflate(Resource.Layout.BindImageInput, container, false);            
            SetUpload();
            return v;
            //return base.OnCreateView(inflater, container, savedInstanceState);
        }

SetUpload()將標簽的值,按鈕的事件和圖像(如果存在)設置為imageview。 它還處理一些與表單事件處理有關的額外事件。 停止運行SetUpload()仍會發生異常。

FormFragment

<RelativeLayout>
    <TextView />
    <View />
    <ScrollView>
        <LinearLayout />
    </ScrollView>
</RelativeLayout>

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            ShowLoading();
            View v = inflater.Inflate(Resource.Layout.Form2, container, false);    
            MainFormLayout = v.FindViewById<LinearLayout>(Resource.Id.mainFormView);
            MainScrollView = v.FindViewById<ScrollView>(Resource.Id.mainScrollView);

            formBuilderWorker = new BackgroundWorker();
            return v;
        }

OnResume()調用formBuilderWorker.DoWork()存在的方法

formBuilderWorker.DoWork += delegate
{
    Form.LoadForm(null, this, FormInstance);
}

LoadForm()使用接口告訴FormFragment顯示控件。 其中一個是UploadFragment。

public void AddControl(Controls control, int? sectionID)
        {
///CODE REMOVED FOR OTHER CONTROL TYPES (they still use old codebase)
            Bindings binding = XForm.GetBindingForControl(control, FormInstance);
            try
            {
                // Create a new fragment and a transaction.
                FragmentTransaction fragmentTx = this.FragmentManager.BeginTransaction();    

                FrameLayout frame = null;    
                Widgets.UploadFragment upload = null;
                frame = new FrameLayout(this.Context);
                frame.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
                frame.Id = control.id;
                upload = new Widgets.UploadFragment(control, binding, Inflater, a, xFormInstance);                    
                MainFormLayout.AddView(frame);
                ControlViews.Add(frame);             
                fragmentTx.Replace(frame.Id, upload, $"upload_{control.id}");
                //fragmentTx.Show(upload);
                fragmentTx.Commit();


            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

這是清理代碼,以盡可能多地刪除不相關的代碼。 顯示的代碼是有問題的代碼通過的路徑。

我發現了這個問題。 我從上面的代碼中取出的部分內容是Activity.RunOnUiThread()調用,它將框架添加到主視圖中。 問題是由線程計時引起的。 UI線程需要很長時間才能將幀添加到視圖中,當FragmentTransaction嘗試提交更改時,框架仍然不存在。

暫無
暫無

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

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