• fragment之间的通信


    Fragment有一个公共的桥梁 Activity

    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            //[1]获取Fragment的管理者
            FragmentManager fragmentManager = getFragmentManager();
            //[2]开启事物 
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            //[3]动态替换
            transaction.replace(R.id.ll1, new Fragment1(),"f1");
            transaction.replace(R.id.ll2, new Fragment2(),"f2");
            
            //[4]最后一步 记得commit
            transaction.commit();
            
            
        }
    
    
    }
    public class Fragment1 extends Fragment {
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment1, null);
            //[1]找到按钮设置点击事件 
            view.findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
    
                    Toast.makeText(getActivity(), "jagjajgl", 1).show();
                    //[2]修改Fragment2里面textview的值 
                    Fragment2 f2 = (Fragment2) getActivity().getFragmentManager().findFragmentByTag("f2");
                    f2.setText("haahha");
                    
                }
            });
            
            return view;
        }
    }
    public class Fragment2 extends Fragment {
    
        private TextView tView;
    
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment2, null);
            
            tView = (TextView) view.findViewById(R.id.tv);
            
            return view;
        }
        
        
        //修改textview值的方法
        public void setText(String content){
            tView.setText(content);
        }
    }
  • 相关阅读:
    转:struts2的s:iterator 标签 详解
    转载:jquery 获取和设置 select下拉框的值
    转载:Linux下Tomcat配置
    document、element、node方法
    .NET平台下Web测试工具横向比较
    PhotoShop中让索引图片解锁使用
    改变FileUpLoader 样式 (FileUpLoader js)
    【收藏下】ashx+jQuery,一个轻量级的asp.net ajax解决方案
    TopCoder入门教程
    调用API
  • 原文地址:https://www.cnblogs.com/xufengyuan/p/6123718.html
Copyright © 2020-2023  润新知