新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Fragment-向Activity中添加Fragment,并相互交互-创新互联

一、activity

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名申请、虚拟空间、营销软件、网站建设、浮山网站维护、网站推广。

一般书写代码时在oncreate方法中引用initview()、initdata()、initload()进行初始化

package com.ping.myfragment;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {//当activity中引入fragment时应该继承--FragmentActivity

 private Myfragment myfragment;
 private TextView activityTV;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  initview();
  initdata();
 }

 private void initdata() {
  FragmentManager fm = getSupportFragmentManager();//获得fragment的管理对象
  FragmentTransaction ft = fm.beginTransaction();
  myfragment = new Myfragment();//Fragment每次添加都要重新创建,否则因为状态不同会导致问题

 ft.add(R.id.container, myfragment);
  ft.commit();
 }

 private void initview() {
  activityTV = (TextView) findViewById(R.id.tv_activity);
  findViewById(R.id.bt_activity).setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    myfragment.settext();
   }
  });
 }

 public void settext() {
  activityTV.setText("activity修改成功");
 }
}

二、fragment

package com.ping.myfragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

public class Myfragment extends Fragment {

 private TextView fragmentTV;

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  View inflate = inflater.inflate(R.layout.layout_myfragment, null);
  initview(inflate);
  return inflate;
 }

 private void initview(View inflate) {
  final MainActivity activity = (MainActivity) getActivity();
  fragmentTV = (TextView) inflate.findViewById(R.id.tv_fragment);
  inflate.findViewById(R.id.bt_fragment).setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    activity.settext();
   }
  });
 }

 public void settext() {
  fragmentTV.setText("fragment修改成功");
 }
}

三activity.xml

   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity" >

          android:id="@+id/container"
       android:layout_width="match_parent"
       android:layout_height="150dp"
       android:layout_alignParentTop="true"
       android:layout_centerHorizontal="true" >
   

          android:id="@+id/tv_activity"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@+id/container"
       android:layout_centerHorizontal="true"
       android:layout_marginTop="30dp"
       android:text="activity中的textview" />

          android:id="@+id/bt_activity"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@+id/tv_activity"
       android:layout_centerHorizontal="true"
       android:layout_marginTop="17dp"
       android:text="改变fragment中的textview" />

四、fragmen.xml


   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="#eeeeee">

          android:id="@+id/tv_fragment"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentTop="true"
       android:layout_centerHorizontal="true"
       android:layout_marginTop="20dp"
       android:text="fragment中的textview" />

          android:id="@+id/bt_fragment"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@+id/tv_fragment"
       android:layout_centerHorizontal="true"
       android:layout_marginTop="15dp"
       android:text="改变activity中的textview" />

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前文章:Fragment-向Activity中添加Fragment,并相互交互-创新互联
文章网址:http://www.wjwzjz.com/article/csgocd.html
在线咨询
服务热线
服务热线:028-86922220
TOP