【问题标题】:Get Fragment by Id in ActivityActivity 中按 Id 获取 Fragment
【发布时间】:2016-02-21 15:11:21
【问题描述】:

我有以下布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment
        class="com.xamarin.recipes.filepicker.FileListFragment"
        android:id="@+id/FileListFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</FrameLayout>

之后我尝试在 Activity 中访问 FileListFragment:

var fileListFragment = SupportFragmentManager.FindFragmentById(Resource.Id.FileListFragment);

但我在 Watches for fileListFragment 中得到“未知标识符:fileListFragment”

更新。

活动代码:

using Android.Support.V4.App;
using Android.OS;

namespace com.xamarin.recipes.filepicker
{
    [Android.App.Activity(Label = "Choose file", MainLauncher = true, Icon = "@drawable/icon")]
    public class FilePickerActivity : FragmentActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.ChooseAudio);

            var fileListFragment = (FileListFragment)SupportFragmentManager.FindFragmentById(Resource.Id.FileListFragment);
        }
    }
}

【问题讨论】:

标签: c# android xamarin


【解决方案1】:

使用这个:

 getSupportFragmentManager()
                        .findFragmentById(R.id.FileListFragment))

【讨论】:

  • 问题是关于 Xamarin
【解决方案2】:

我目前没有使用 Xamarin,但是我取消了我的答案,因为我认为我已经接近解决方案,这可以帮助你。我找到了几个使用 Xamarin 的教程或 GitHub 项目。这些搜索没有给我正确的解决方案,但我认为它们为我提供了您问题的线索。试试这个代码:

using Android.App;
using Android.OS;
using Android.Support.V4.App;
// using Fragment = Android.Support.V4.App.Fragment;

namespace com.xamarin.recipes.filepicker
{
    [Activity(Label = "Choose file", MainLauncher = true, Icon = "@drawable/icon")]
    public class FilePickerActivity : FragmentActivity
    {
        // use Android.Support.V4.App.Fragment below
        private FileListFragment mFileListFragment; 

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.ChooseAudio);

            mFileListFragment = (FileListFragment)SupportFragmentManager.FindFragmentById(Resource.Id.fileListFragment);
            // Avoid any confusion: change its id in your layout by "fileListFragment"
        }
    }
}   

我使用片段类 FileListFragment 更改了 var 似乎未定义的 Android.Support.V4.App.Fragment,因为错误显示:未知标识符

注意:请确保FileListFragment 扩展Android.Support.V4.App.Fragment 如下:

public class FileListFragment : Android.Support.V4.App.Fragment
{
  ...
}  

要使用var,我猜你需要通过as Android.Support.V4.App.Fragment(一个比较器)来转换它,像这样:

var fileListFragment = SupportFragmentManager
                       .FindFragmentById(Resource.Id.fileListFragment)
                       as FileListFragment; // you need this line

【讨论】:

    【解决方案3】:

    试试这个代码。我不确定,但我认为它应该有效。

    var fileListFragment = SupportFragmentManager.FindFragmentById<FileListFragment>(Resource.Id.FileListFragment);
    

    【讨论】:

    • FindFragmentById 中没有类型化参数
    • @Idsa 'FragmentManager' documentation 中有类型参数。我认为“SupportFragmentManager”具有相同的方法。
    • 文档好像已经过时了
    【解决方案4】:

    你有没有尝试过这样的片段

     Fragment fileListFragment = SupportFragmentManager.findFragmentById(R.id.FileListFragment);
    

    【讨论】:

      【解决方案5】:

      你已经知道你尝试过的方法是行不通的,所以我不会再说一遍。 请注意,无法像这样访问 Fragment 的视图。 尝试使用 ADT 创建一个带有选项卡导航的项目,然后查看代码,它将阐明您的概念。 该向导还将为您生成一个虚拟选项卡片段类,如下所示

      public static class FragHomeUI extends Fragment
      

      片段视图将在类的以下成员中创建和访问:

          public View onCreateView(LayoutInflater inflater, ViewGroup container,
                  Bundle savedInstanceState)
      {
                  View rootView = inflater.inflate(R.layout.fragment_home, container,
                          false);
      

      再说一遍,通过ADT向导创建几个示例项目,仔细观察代码。

      【讨论】:

      • 你在回答之前读过问题和cmets吗?这个问题是关于Xamarin的!这不是使用这种跨平台开发创建片段的正确方法。
      【解决方案6】:

      这可能会有所帮助。

      FragmentManager fm = getSupportFragmentManager();
      Fragment fragment_byID = fm.findFragmentById(R.id.fragment_id);
      //OR
      Fragment fragment_byTag = fm.findFragmentByTag("fragment_tag");
      

      【讨论】:

      • 和其他人一样:你在回答之前读过问题和cmets吗?这个问题是关于Xamarin!这不是使用这种跨平台开发的正确方法。
      • Activity 也可以使用 FragmentManager 来查找 Fragment Like : var emailList = FragmentManager.FindFragmentById(Resource.Id.email_list_fragment);
      • 究竟什么是你做不到的?
      【解决方案7】:

      请在 Xamarin.Android 4.12.02001 中尝试此代码:

      FileListFragment fileListFragment = Android.Runtime.Extensions.JavaCast<FileListFragment>(SupportFragmentManager.FindFragmentById(Resource.Id.id_file_list_fragment));

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-07-19
        • 1970-01-01
        • 2013-08-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多