【问题标题】:Accessing fragment reference inside a custom view在自定义视图中访问片段引用
【发布时间】:2013-12-16 12:24:42
【问题描述】:

我有一个场景,片段在其布局中有一个自定义视图(通过扩展视图类)。自定义视图必须访问片段的一些方法。但我无法在自定义视图中获取片段引用。此外,我无法从自定义视图(片段的子视图)访问片段

根据 android 片段:我们在视图的构造函数中获取上下文。此上下文是活动的实例。但是无法获取托管 customview 的片段的引用。

请告诉我如何访问自定义视图中的片段。

编辑:

添加代码以便更好地理解我的问题:

MyFragment .java

公共类 MyFragment 扩展片段 {

int selectedOptionIndex;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
//get value of selectedOptionIndex from bundle and save it to this.selectedOptionIndex;

}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
this.activity = activity;

}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.myFragmentview, container, false);
    return view;

//i can access views from fragment, but not vice versa. }

}

myFragmentview.xml

    <com.test.CustomView
                android:id="@+id/myCustomView" android:layout_width="fill_parent"
                android:layout_height="40dip"  layout_weight="1"
                  />

公共类 CustomView 扩展视图 {

private MyActivity context; 

/** Default Constructor */
public EasCalWeekViewHeader1(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
        this.context = (MyActivity) context;

// Now using context, we can access activity insatnce inside this customView and can call any method of activity instance. //But we cannot do this for fragments. There is not way to access parent fragment at this point. I cannot access selectedOptionIndex and use it to set in this view. //if view can access the parent activty, then it should also have an access to parent fragment.
}

/** Default Constructor */
public EasCalWeekViewHeader1(Context context, AttributeSet attrs) {
    super(context,attrs);
            this.context = (MyActivity) context;
        }

}

}

【问题讨论】:

    标签: android android-fragments android-custom-view


    【解决方案1】:

    好的。用你自己的话来说:

    根据android片段:我们在构造函数中获取上下文 观点。此上下文是活动的实例。但是没有 获取托管自定义视图的片段的引用的方法。

    所以,上下文就是活动的实例,对吧?现在从这个活动上下文中,我相信如果您分别使用findFragmentByTag(String tag)findFragmentById(int id) 为您的片段提供了一个字符串标签名称或一个ID,那么您可以获得对您的片段的引用。

    context.getFragmentManager().findFragmentByTag("tag_of_frag_you_are_trying_to_access");
    

    HTH。

    【讨论】:

    • 同意,这是在任何视图中访问片段的更好方法。
    【解决方案2】:

    致电FragmentManager.findFragment(this),其中this 是您的自定义视图。

    或者如果你使用 Kotlin 的 Fragment 库 androidx.fragment:fragment-ktx,你可以在你的视图中使用扩展函数 View.findFragment()

    定义为:

    fun <F : Fragment> View.findFragment(): F = FragmentManager.findFragment(this)
    

    来自文档:

    此方法将定位与此视图关联的 Fragment。这是为 Fragment.onCreateView 及其子项返回的 View 自动填充的。

    【讨论】:

    • 当您没有为片段指定 id 或标签时(例如在开发自定义视图时),我认为这是最好的调用方法。我正在使用这种方法来获取托管我的自定义视图的片段或活动,如下所示。 val parent = try { // 如果我们在一个片段中获取片段 FragmentManager.findFragment(widget) } catch (exception: IllegalStateException) { // 否则获取活动上下文 } 但是你需要添加实现 'androidx.fragment: gradle 文件中的 fragment-ktx:1.2.4'
    【解决方案3】:

    我找到了解决方案并和我一起尝试

    在自定义视图的构造函数中获取上下文的引用并将其转换为活动类

    private Activity activity ;
    public EasCalWeekViewHeader1(Context context){
        activity = (Activity) context ; 
    }
    

    并且在方法中要调用Fragment方法

    yourFragment a= activity.getFragmentManager().findFragmentById(R.id.fragment1);
    a.YourMethod();
    

    【讨论】:

    • 是的,我们可以使用 getFragmentManager() 访问片段。哈里克里斯和你的答案都是可以接受的。但由于哈里克里斯的回答排在第一位,我将其标记为已接受
    【解决方案4】:

    使用视图的构造函数传递片段本身而不传递其活动。这是一个深层副本,因此您将能够从视图中访问您的片段。

    public class YourView extends View
    {
         Fragment yourfragmentinside;
         public YourView(Fragment inputyourfragment)
         {
            this.yourfragmentinside = inputyourfragment
         }
    

    }

    别担心它是同一个片段 - 如果你不使用 new...()-

    【讨论】:

      【解决方案5】:

      我正在尝试使用findFragmentByTag 来查找片段,但是在我的项目中添加标签并不容易,并且片段是一个自定义片段类。所以我迭代片段来找到它。代码sn-p如下:

      MyFragment myFrag = null;
      List<Fragment> fragments = getContext().getSupportFragmentManager().getFragments();
      for (Fragment frag : fragments) {
          if (frag instanceof MyFragment) {
              myFrag = (MyFragment) frag;
              break;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-03
        • 2023-02-02
        • 1970-01-01
        • 2016-02-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多