繁体   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