热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

android开发分享如何访问fragment的父Activity中的Fragment的子视图?

我有一个支持的片段活动,将加载diff片

我有一个支持的片段活动,将加载diff片段。 该片段有一些id = "score" findViewById ,我想得到它的句柄,但findViewById分数的textView返回null。 为什么这样?

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

    注意:

    直接访问片段外的片段是不是一个好主意。 你应该使用片段callback接口来处理这种情况,并避免错误。 以下方法可行,但不推荐,因为这不是一个好的做法。


    如果你想在其父Activity访问FragmentTextView ,那么你应该在你的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); } } 

    现在你可以在你的Activity使用这个:

     myFragment.setTextViewText("foo"); 

    这里myFragment是MyFragmenttypes的。

    如果你想访问整个TextView那么你可以在MyFragment.java定义一个像这样的方法:

     public TextView getTextView1(){ return mTextView; } 

    通过这个你可以访问TextView本身。

    希望这可以帮助。 ?

    可以用以下方法:

    请继续参照片段中的膨胀视图:

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

    在Activity中创build一个函数,如下所示:

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

    其目的是保持当前片段的参考。 无论何时你想切换片段,你都可以调用上面的函数,像这样(来自片段):

     activity.switchContent( new MyFragment_2()); 

    现在你有最新的片段参考。 所以你可以像这样直接访问Activity中的Fragment视图: this.fragment.view

    你不需要引用片段视图来获取它在Activity中的组件。 因为您可以直接访问父活动中 Fragment的布局组件。

    只要你可以通过这个访问任何组件

     findViewById(R.id.child_of_fragment_layout); 

    您可以使用Fragment类的getView方法访问。

    例如,您的MyFragment中有一个TextView,其ID为“text_view”
    在你的活动中做一个你的片段:

     MyFragment myFragment = new MyFragment(); 

    而当你需要一个孩子只需调用getView然后find你的childView。

     View view = myFragment.getView(); if (view !=null) { view.findViewById(R.id.text_view).setText("Child Accessed :D"); } 

    如果你的TextView放在Fragment中,那么你不能在你的Fragment Parent Activity中访问TextView,你可以设置Fragment和Activity之间的相互通信接口,当你点击TextView或者其他想要发生的事情时发送数据

    只有这样做:

     ((Your_Activity) this.getActivity()).YouyActivityElements; 

    只是放在片段而不是放在活动中:

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

    得分textView是在片段的布局,它不是在MyActivity的布局,即R.layout.activity_home。 所以一旦你膨胀相应的布局文件,你可以在该片段中find分数textview。

    它返回null因为TextViewFragment一个元素,而不是Activity

    请注意,使用Fragment的想法是将一个模块封装在Fragment ,这意味着Activity不应直接访问它的属性。 考虑移动你的逻辑,在Fragment获得TextView引用

    您不能访问父Activity Fragment元素,但可以按照以下方式将值传递给您的Fragment

    onNavigationDrawerItemSelected方法中执行以下操作

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

    然后在MyFragment类中创build一个名为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; } 

    并在MyFragmentonCreateView()方法

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

    这就是全部,我希望这会帮助你。 如果不是,请让我知道。

    只需将TextView声明为公共片段,通过片段的onCreateView()中的findViewById()对其进行初始化。 现在通过使用活动中添加的Fragment对象,您可以访问TextView。

    您需要从片段视图调用方法findViewById。

     protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); mNavigatiOnDrawerFragment= (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_drawer); scoreBoardTextView = (TextView) mNavigationDrawerFragment.getView().findViewById(R.id.score); } 

    这种方式适合我。

    我build议你将textview作为你的活动布局的一部分。 或者,您可以将textview作为分隔符片段。 看看我的问题在这里。 它与你的类似,但方向相反。 这里是我在我的项目中使用的代码的简化版本。 解释是沿着代码。

    活动类

     public class MainActivity extends ActionBarActivity { PlaceFragment fragment; TextView fragmentsTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); Bundle bundle = new Bundle(); bundle.putString("score", "1000"); fragment = PlaceFragment.newInstance(bundle); FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.replace(R.id.container, fragment); ft.addToBackStack(null); ft.commit(); // method 1 // fragment is added some ways to access views // get the reference of fragment's textview if (fragment.getTextView() != null) { fragmentsTextView = fragment.getTextView(); } // method 2 // using static method dont use in production code // PlaceFragment.textViewInFragment.setText("2000"); // method 3 // let the fragment handle update its own text this is the recommended // way wait until fragment transaction is complete before calling //fragment.updateText("2000"); } } 

    片段类:

     public class PlaceFragment extends Fragment { public TextView textViewInFragment;// to access via object.field same to // string.length // public static TextView textViewInFragment;//to access via // PlaceFragment.textView dont try this in production code public PlaceFragment() { } public static PlaceFragment newInstance(Bundle bundle) { PlaceFragment fragment = new PlaceFragment(); fragment.setArguments(bundle); return fragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view = inflater.inflate(R.layout.fragment_place, container, false); textViewInFragment = (TextView) view .findViewById(R.id.textViewInFragment); return view; } @Override public void onStart() { // TODO Auto-generated method stub super.onStart(); if (getArguments() != null) { textViewInFragment.setText(getArguments().getString("score")); } } public TextView getTextView() { if (textViewInFragment != null) { return textViewInFragment;// returns instance of inflated textview } return null;// return null and check null } public void updateText(String text) { textViewInFragment.setText(text);// this is recommended way to alter // view property of fragment in // activity } } 

    从活动到片段的沟通非常简单。 这是因为活动包含片段。 保留片段对象并通过setter和getter或其内部的公共字段来访问它的属性。 但是从片段到活动的通信需要一个接口。

    为什么你不直接从你的FragmentPagerAdapter访问它,

     SubAccountFragment subAccountFragment = (SubAccountFragment) mSectionsPagerAdapter.getItem(1); subAccountFragment.requestConnectPressed(view); 

    这里是完整的例子:

     import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import java.util.Locale; public class TabsActivity extends ActionBarActivity implements ActionBar.TabListener { /** * The {@link android.support.v4.view.PagerAdapter} that will provide * fragments for each of the sections. We use a * {@link FragmentPagerAdapter} derivative, which will keep every * loaded fragment in memory. If this becomes too memory intensive, it * may be best to switch to a * {@link android.support.v4.app.FragmentStatePagerAdapter}. */ SectionsPagerAdapter mSectionsPagerAdapter; /** * The {@link ViewPager} that will host the section contents. */ ViewPager mViewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_tabs); // Set up the action bar. final ActionBar actiOnBar= getSupportActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); // Create the adapter that will return a fragment for each of the three // primary sections of the activity. mSectiOnsPagerAdapter= new SectionsPagerAdapter(getSupportFragmentManager()); // Set up the ViewPager with the sections adapter. mViewPager = (ViewPager) findViewById(R.id.pager); mViewPager.setAdapter(mSectionsPagerAdapter); // When swiping between different sections, select the corresponding // tab. We can also use ActionBar.Tab#select() to do this if we have // a reference to the Tab. mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageSelected(int position) { actionBar.setSelectedNavigationItem(position); } }); // For each of the sections in the app, add a tab to the action bar. for (int i = 0; i  

    如果视图已经在屏幕上膨胀(例如可见),那么你可以在活动中使用findViewById(R.id.yourTextView),正常情况下它将把句柄返回到文本视图,如果没有find视图,则返回null。

      以上就是android开发分享如何访问fragment的父Activity中的Fragment的子视图?相关内容,想了解更多android开发(异常处理)及android游戏开发关注(编程笔记)。


      推荐阅读
      • Spring特性实现接口多类的动态调用详解
        本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
      • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
      • Android开发实现的计时器功能示例
        本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
      • IjustinheritedsomewebpageswhichusesMooTools.IneverusedMooTools.NowIneedtoaddsomef ... [详细]
      • 本文介绍了一款名为TimeSelector的Android日期时间选择器,采用了Material Design风格,可以在Android Studio中通过gradle添加依赖来使用,也可以在Eclipse中下载源码使用。文章详细介绍了TimeSelector的构造方法和参数说明,以及如何使用回调函数来处理选取时间后的操作。同时还提供了示例代码和可选的起始时间和结束时间设置。 ... [详细]
      • Introduction(简介)Forbeingapowerfulobject-orientedprogramminglanguage,Cisuseda ... [详细]
      • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
      • Linux重启网络命令实例及关机和重启示例教程
        本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
      • XML介绍与使用的概述及标签规则
        本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
      • FeatureRequestIsyourfeaturerequestrelatedtoaproblem?Please ... [详细]
      • 在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板
        本文介绍了在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板的方法和步骤,包括将ResourceDictionary添加到页面中以及在ResourceDictionary中实现模板的构建。通过本文的阅读,读者可以了解到在Xamarin XAML语言中构建控件模板的具体操作步骤和语法形式。 ... [详细]
      • 带添加按钮的GridView,item的删除事件
        先上图片效果;gridView无数据时显示添加按钮,有数据时,第一格显示添加按钮,后面显示数据:布局文件:addr_manage.xml<?xmlve ... [详细]
      • 1简介本文结合数字信号处理课程和Matlab程序设计课程的相关知识,给出了基于Matlab的音乐播放器的总体设计方案,介绍了播放器主要模块的功能,设计与实现方法.我们将该设 ... [详细]
      • 今天就跟大家聊聊有关怎么在Android应用中实现一个换肤功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根 ... [详细]
      • 本博文基于《Amalgamationofproteinsequence,structureandtextualinformationforimprovingprote ... [详细]
      author-avatar
      ckx1989
      这个家伙很懒,什么也没留下!
      PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
      Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有