在自定义视图中访问片段引用

时间:2013-11-30 12:48:52

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

我有一个场景,其中Fragment在其布局中具有自定义视图(通过扩展View类)。自定义视图必须访问片段的某些方法。但是我无法在自定义视图中获得片段引用。此外,我无法以任何方式从自定义视图(片段的子节点)访问片段

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

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

编辑:

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

MyFragment .java

公共类MyFragment扩展了Fragment {

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扩展了View {

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

}

}

5 个答案:

答案 0 :(得分:8)

行。用你自己的话说:

  

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

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

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

HTH。

答案 1 :(得分:4)

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

或者,如果您使用Kotlin的片段库androidx.fragment:fragment-ktx,则可以在视图内部使用扩展功能View.findFragment()

它定义为:

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

从文档中:

此方法将找到与此视图关联的片段。将自动为Fragment.onCreateView及其子级返回的View填充。

答案 2 :(得分:2)

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

在Custom视图的构造函数中获取上下文的引用并将其强制转换为Activity Class

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

在方法中你要调用Fragment方法

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

答案 3 :(得分:0)

使用视图的构造函数传递片段本身而不是其活动。它是一个深层复制,因此您可以从视图中访问您的片段。

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

}

不要担心它是相同的片段 - 如果您不使用新的...() -

答案 4 :(得分:0)

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

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