[英]Where can I find ID of a fragment. And how can I get access to the view of a fragment from MainActivity class?
I created a tabbed Activity
and I have to modify some views from the MainActivity
class. 我创建了一个选项卡式
Activity
,我必须修改MainActivity
类中的一些视图。 How can I get access to any of these three fragments created from that Activity? 如何访问从该Activity创建的这三个片段中的任何一个?
I know the method involving the use of getFragmentManager().findFragmentById(R.id.example_fragment);
我知道涉及使用
getFragmentManager().findFragmentById(R.id.example_fragment);
的方法getFragmentManager().findFragmentById(R.id.example_fragment);
, but I don't know where I can find fragment ID. ,但我不知道在哪里可以找到片段ID。
While doing the fragment transaction you might consider setting an id by yourself and later you can retrieve the Fragment
using that id as well. 在执行片段事务时,您可以考虑自己设置id,稍后您也可以使用该ID检索
Fragment
。
Here's how you set up and id/tag for your Fragment
which is being launched. 以下是为正在启动的
Fragment
设置和标识/标记的方法。
getFragmentManager().beginTransaction().add(R.id.fragment_container, new YourFragment(), "FRAGMENT_1").commit();
Now you can retrieve the Fragment
using the id (ie tag) like the following. 现在,您可以使用id(即标记)检索
Fragment
,如下所示。
getFragmentManager().findFragmentByTag("FRAGMENT_1");
Now to answer your other question, which is how you can access a view component of your Fragment
from your Activity
class, you can do this in multiple ways. 现在回答您的另一个问题,即如何从
Activity
类访问Fragment
的视图组件,您可以通过多种方式执行此操作。
One possible option of doing this is having a BroadcastReceiver
in your Fragment
which can be invoked to update views inside the Fragment
while the broadcast is received from your Activity
. 这样做的一种可能的选择是有一个
BroadcastReceiver
在您的Fragment
可以被调用来更新内部意见Fragment
,而广播是从您收到Activity
。 Here's a sample Fragment
with a BroadcastReceiver
to explain the idea behind. 这是一个带有
BroadcastReceiver
的示例Fragment
,用于解释背后的想法。
public class YourFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
// Start listening for the broadcast sent from the Activity
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mYourBroadcastReceiver, new IntentFilter("MESSAGE_FROM_ACTIVITY"));
return inflater.inflate(R.layout.your_fragment_layout, null, true);
}
@Override
public void onDestroyView() {
// The BroadcastReceiver must be destroyed when your Fragment is being destroyed
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mYourBroadcastReceiver);
}
private final BroadcastReceiver mYourBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// This will be invoked when a broadcast is received from your Activity
// Update your views here
}
};
}
Now send the broadcast from your Activity
like this when needed. 现在,在需要时从您的
Activity
发送广播。
Intent intent =new Intent("MESSAGE_FROM_ACTIVITY");
sendBroadcast(i);
Hope that helps! 希望有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.