Android 如何在片段的父活动中访问片段的子视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24188050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access Fragment's child views inside fragment's parent Activity?
提问by user93796
I have a supported fragment activity which will load diff fragments. The fragment has some textView
with id = "score"
and I want to get its handle but findViewById
for score's textView
returns null. Why so?
我有一个支持的片段活动,它将加载差异片段。该片段有一些textView
with id = "score"
,我想得到它的句柄,但findViewById
对于 score 的textView
返回 null。为什么这样?
textView is placed in fragment
textView 放置在片段中
public class MyActivity extends extends ActionBarActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks{
private TextView scoreBoardTextView = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
scoreBoardTextView = (TextView) findViewById(R.id.score); //this returns null
}
@Override
public void onNavigationDrawerItemSelected(int position) {
//set fragment
}
}
回答by SMR
Note:
笔记:
Directly accessing fragment's views outside fragment is not a good idea. You should use fragment callback interfaces to handle such cases and avoid bugs. The following way works but it is not recommended as it is not a good practice.
直接访问片段之外的片段视图不是一个好主意。您应该使用片段回调接口来处理这种情况并避免错误。以下方式有效,但不建议这样做,因为这不是一个好的做法。
如果要访问其父级内部的
TextView
TextView
of ,则应在类中定义一个方法,如下所示:Fragment
Fragment
Activity
Activity
Fragment
Fragment
public class MyFragment extends Fragment {
TextView mTextView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main, container, false);
mTextView = (TextView) view.findViewById(R.id.textView1);
return view;
}
public void setTextViewText(String value){
mTextView.setText(value);
}
}
Now you can use this inside your Activity
like this:
现在你可以Activity
像这样在你的内部使用它:
myFragment.setTextViewText("foo");
here myFragment is of type MyFragment
.
这里 myFragment 是类型MyFragment
。
If you want to access the whole TextView
then you can define a method like this inside MyFragment.java
:
如果你想访问整个,TextView
那么你可以在里面定义一个这样的方法MyFragment.java
:
public TextView getTextView1(){
return mTextView;
}
By this you can access the TextView
itself.
通过这个,你可以访问它TextView
本身。
Hope this Helps. :)
希望这可以帮助。:)
回答by Arsalan Mehmood
It is possible with following way:
可以通过以下方式:
Keep reference of inflated view in the Fragment like this :
在 Fragment 中保持对膨胀视图的引用,如下所示:
public class MyFragment extends SherlockFragment{
MainMenuActivity activity;
public View view;
public MyFragment(){
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if ( getActivity() instanceof MainMenuActivity){
activity = (MainMenuActivity) getActivity();
}
view = inflater.inflate(R.layout.aboutus, container, false);
return view;
}
}
Create a function in the Activity, like this:
在Activity中创建一个函数,像这样:
public class MainMenuActivity extends SherlockFragmentActivity {
SherlockFragment fragment = null;
public void switchContent(SherlockFragment fragment) {
this.fragment = fragment;
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.mainmenu, fragment)
.commit();
invalidateOptionsMenu();
}
Its purpose is to keep reference of current fragment. Whenever you wanna switch fragment, you call above function, like this (from fragment):
其目的是保持对当前片段的引用。每当你想切换片段时,你调用上面的函数,像这样(来自片段):
activity.switchContent( new MyFragment_2());
Now you've current fragment reference. So you can directly access Fragment's views in Activity like this: this.fragment.view
现在你有当前的片段参考。所以你可以像这样直接在 Activity 中访问 Fragment 的视图:this.fragment.view
回答by Ali Imran
You have no need of reference of Fragmentview to get its components in Activity. As you can directly access layout components of a Fragmentin parent Activity.
您无需参考Fragment视图即可在Activity 中获取其组件。因为您可以直接访问父Activity 中Fragment 的布局组件。
Simply you can access any component by this
简单地你可以通过这个访问任何组件
findViewById(R.id.child_of_fragment_layout);
回答by F.A. Botic
In order to access the TextView or Button or whatever in your fragment you need to do the following:
为了访问 TextView 或 Button 或片段中的任何内容,您需要执行以下操作:
public class BlankFragment extends Fragment {
public View view;
public TextView textView;
public Button button;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view =inflater.inflate(R.layout.fragment_blank, container, false);
textView = (TextView)view.getRootView().findViewById(R.id.textView_fragment1);
return view;
}
public void changeTextOfFragment(String text){
textView.setText(text);
view.setBackgroundResource(R.color.colorPrimaryDark);
}
Once that is done in your MainActivity or any other where you want to access your TextView from your Fragment you should make sure to set up the fragment in your OnCreate() method other ways it will most likely throw nullPointer. So your activity where you want to change the TextView should look smth like this:
一旦在 MainActivity 或任何其他要从 Fragment 访问 TextView 的地方完成此操作,您应该确保在 OnCreate() 方法中以其他方式设置片段,它很可能会抛出 nullPointer。因此,您要更改 TextView 的活动应如下所示:
public class MainActivity extends AppCompatActivity {
private Button button1;
private FragmentManager fragmentManager;
private FragmentTransaction fragmentTransaction;
BlankFragment blankFragment = new BlankFragment();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button)findViewById(R.id.button1);
changeFragment();
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment1,blankFragment);
fragmentTransaction.commit();
}
private void changeFragment(){
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
blankFragment.changeTextOfFragment("Enter here the text which you want to be displayed on your Updated Fragment");
}
});
}
Hope this helps :)
希望这可以帮助 :)
回答by Saman Sattari
You can access with getView method of Fragment class.
您可以使用 Fragment 类的 getView 方法访问。
For example You have a TextView in Your MyFragment with id of "text_view"
In Your Activity make a Fragment of Yours:
例如,您的 MyFragment 中有一个 ID 为“text_view”的 TextView
在您的 Activity 中创建一个 Fragment of Yours:
MyFragment myFragment = new MyFragment();
And when You need a child just call getView and then find Your childView.
当您需要孩子时,只需调用 getView,然后找到您的 childView。
View view = myFragment.getView();
if (view !=null) {
view.findViewById(R.id.text_view).setText("Child Accessed :D");
}
Note:if you want the root view of your fragment, then myFragment.getView();
is simply enough.
注意:如果您想要片段的根视图,那么myFragment.getView();
就足够了。
回答by Lalit kumar
Only doing this:
只这样做:
((Your_Activity) this.getActivity()).YouyActivityElements;
回答by Ramkailash
If your TextView placed inside Fragment that case you cannot access TextView inside your Fragment Parent Activityyou can set the interface for intercommunication between Fragment and Activity and send Data when you click on TextView or anyother thing which you want to happend
如果您的 TextView 放置在 Fragment 中,那么您无法访问Fragment Parent Activity 中的TextView,您可以设置 Fragment 和 Activity 之间的互通接口,并在单击 TextView 或任何其他您想要发生的事情时发送数据
回答by Zoran
Just put in fragment instead of putting in activity:
只需放入片段而不是放入活动:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_new_work_order,
container, false);
TextView scoreBoardTextView = (TextView) rootView.findViewById(R.id.score);
return rootView;
}
回答by Gunaseelan
You can't access Fragment
element in Parent Activity
, But You can pass values to your Fragment
by following way.
您无法访问Fragment
Parent 中的元素Activity
,但您可以通过Fragment
以下方式将值传递给您。
in your onNavigationDrawerItemSelected
method of MyActivity
do the following
在您执行以下操作的onNavigationDrawerItemSelected
方法中MyActivity
int myScore = 100;
@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager
.beginTransaction()
.replace(R.id.container,
MyFragment.newInstance(myScore)).commit();
}
And in MyFragment
class create a method called newInstance
like following
并在MyFragment
课堂上创建一个名为newInstance
如下的方法
private static final String SCORE = "score";
public static MyFragment newInstance(int score) {
MyFragment fragment = new MyFragment();
Bundle args = new Bundle();
args.putInt(SCORE, score);
fragment.setArguments(args);
return fragment;
}
And in MyFragment
's onCreateView()
method
和 inMyFragment
的onCreateView()
方法
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
TextView textView = (TextView) rootView
.findViewById(R.id.score);
textView.setText(Integer.toString(getArguments().getInt(
SCORE)));
return rootView;
}
That's All, I hope this will help you. If not please let me know.
就这些,希望对你有帮助。如果没有,请告诉我。
回答by Himanshu Gupta
Simply declare TextView as public in fragment, initialize it by findViewById() in fragment's onCreateView(). Now by using the Fragment Object which you added in activity you can access TextView.
只需在片段中将 TextView 声明为 public,在片段的 onCreateView() 中通过 findViewById() 对其进行初始化。现在通过使用您在活动中添加的 Fragment 对象,您可以访问 TextView。