Fragment与Activity之间的通讯
提示:本博客案例的创建部分在我的上一篇博客中
https://blog.csdn.net/weixin_38423829/article/details/80603782
1.从Fragment向Activity中传递数据
- 1.首先我们在我们在activity_main.xml中添加一个TextView控件放置在顶部
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请点击下方的文字"
android:id="@+id/tv_top"
android:gravity="center"
android:textSize="20sp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
>
<TextView
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="聊天"
android:gravity="center"
android:id="@+id/lay_charTop"
/>
<TextView
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="主页"
android:gravity="center"
android:id="@+id/lay_contentTop"
/>
<TextView
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="朋友圈"
android:gravity="center"
android:id="@+id/lay_friendsTop"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="5px"
>
<View
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/chars_bom"
/>
<View
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/content_bom"
/>
<View
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/frident_bom"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="7"
>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lay_vp"
></android.support.v4.view.ViewPager>
</LinearLayout>
</LinearLayout>
- 2.接着我们修改fone.java里的代码(在这之前,记得给fragment_one.xml中添加id)
package com.example.xiao.fragmentdemo2;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* A simple {@link Fragment} subclass.
*/
public class Fone extends Fragment {
public Fone() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//修改部分从这里开始
//这里的view是为了实例化TextView作准备
View view = inflater.inflate(R.layout.fragment_fone, container, false);
TextView fone = view.findViewById(R.id.fone); //记得给fragment_fone.xml中的文字添加一个id值哦
fone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity mainActivity = (MainActivity) getActivity(); //实例化主类
mainActivity.setToptv("这里是聊天窗口"); //调用主类里新建的setToptv方法
}
});
return view;
}
}
- 3.我们发现上面的代码中有一个setToptv方法我们没有创建过,那么我们就在主类里创建把,它的功能是点击fragment文字的时候,替换activity中的文字
MainActivity.java的代码如下:
package com.example.xiao.fragmentdemo2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private ViewPager viewPager;
List<Fragment> fragmentList;
Fone fragmentOne;
Ftwo fargmentTwo;
Fthree fragmentThree;
TextView chars, contents, friends,top_tv;
View chars_bom, content_bom, friend_bom;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
//实例化fragment对象并将加到fragmentlist对象里面
fragmentList = new ArrayList<>();
fragmentOne = new Fone();
fargmentTwo = new Ftwo();
fragmentThree=new Fthree();
fragmentList.add(fragmentOne);
fragmentList.add(fargmentTwo);
fragmentList.add(fragmentThree);
chars_bom.setBackgroundColor(Color.BLUE);//给聊天文字下的view标签添加下划线
Adapter adapter = new Adapter(getSupportFragmentManager(), fragmentList); //实例化适配器对象,将fragmentlist传入
viewPager.setAdapter(adapter); //将适配器对象设置到viewPager
//设置viewpage的监听事件
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
//滑动后产生的变化
@Override
public void onPageSelected(int position) {
chars_bom.setBackgroundColor(Color.GRAY);
content_bom.setBackgroundColor(Color.GRAY);
friend_bom.setBackgroundColor(Color.GRAY);
switch (position) {
case 0:
chars_bom.setBackgroundColor(Color.BLUE);
break;
case 1:
content_bom.setBackgroundColor(Color.BLUE);
break;
case 2:
friend_bom.setBackgroundColor(Color.BLUE);
break;
default:
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
private void init() {
//初始化数据
viewPager = findViewById(R.id.lay_vp);
chars = findViewById(R.id.lay_charTop);
friends = findViewById(R.id.lay_friendsTop);
contents = findViewById(R.id.lay_contentTop);
top_tv=findViewById(R.id.tv_top);
chars_bom = findViewById(R.id.chars_bom);
content_bom = findViewById(R.id.content_bom);
friend_bom = findViewById(R.id.frident_bom);
chars.setOnClickListener(this);
friends.setOnClickListener(this);
contents.setOnClickListener(this);
}
//点击上方的按钮也会实现跳转功能
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.lay_charTop:
viewPager.setCurrentItem(0);
break;
case R.id.lay_contentTop:
viewPager.setCurrentItem(1);
break;
case R.id.lay_friendsTop:
viewPager.setCurrentItem(2);
break;
default:
break;
}
}
//用于替换top_tv里的文字
public void setToptv(String s){
top_tv.setText(s);
}
}
2. 从Activity向Fragment中传递数据
- 1.首先我们在activity中设置用于传送到Fragment当中的数据
在MainActivity中书写的代码如下
Bundle bundle = new Bundle(); //创建打包对象
bundle.putString("name","我是从Activity中来的"); //放入数据
fargmentTwo.setArguments(bundle); //将数据发送到Ftwo
- 2.接着我们在Ftwo.java里面接收数据并通过点击设置
代码如下
public class Ftwo extends Fragment {
public Ftwo() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_ftwo, container, false);
final TextView ftwo = view.findViewById(R.id.ftwo); //记得给响应的textview添加id值
ftwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle bundle = getArguments();
String name = bundle.getString("name");
ftwo.setText(name);
}
});
return view;
}
}
效果