Fragment与activity的两种通信方式
Fragment与activity之间有多种通信方式,今天我们主要说两种通信方式,直接在一个activity传值给Fragment的方法和Fragment直接调用Activity中的public方法
Fragment直接调用Activity中的public方法
activity传值给fragment,在这其中我们首先得使用Bundle方法,首先我们得创建fragment
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#123456"
tools:context="com.example.asus.mytest2.fragment.BenFragment">
<!-- TODO: Update blank fragment layout -->
<Button
android:layout_width="match_parent"
android:layout_height="70dp"
android:textSize="30sp"
android:gravity="center"
android:text="大笨妞"
android:id="@+id/ben"/>
</FrameLayout>
可以看到,我在fragment中加了一个按钮,接下来我将把按钮上的taxt取我在activity中传的值
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.example.asus.mytest2.ViewPagerActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/line"
android:layout_below="@+id/ll"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:id="@+id/tv_view"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:id="@+id/ll_viewpager">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="大笨妞"
android:id="@+id/btn1_viewpager"/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="大傻妞"
android:id="@+id/btn2_viewpager"/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="大蠢妞"
android:id="@+id/btn3_viewpager"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/ll">
<View
android:layout_width="0dp"
android:layout_height="2dp"
android:layout_weight="1"
android:background="#000000"
android:id="@+id/view1"/>
<View
android:layout_width="0dp"
android:layout_height="2dp"
android:layout_weight="1"
android:id="@+id/view2"/>
<View
android:layout_width="0dp"
android:layout_height="2dp"
android:layout_weight="1"
android:id="@+id/view3"/>
</LinearLayout>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewPager"
android:layout_below="@id/line">
</android.support.v4.view.ViewPager>
</RelativeLayout>
这是activity中的布局,因为接下来我还要使用到下一种方法的例子,下面是activity的java页面
package com.example.asus.mytest2;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.example.asus.mytest2.adapter.ViewAdapter;
import com.example.asus.mytest2.fragment.BenFragment;
import com.example.asus.mytest2.fragment.ChunFragment;
import com.example.asus.mytest2.fragment.ShaFragment;
import java.util.ArrayList;
import java.util.List;
public class ViewPagerActivity extends AppCompatActivity implements View.OnClickListener {
private ViewPager viewPager;
private BenFragment benFragment;
private ShaFragment shaFragment;
private ChunFragment chunFragment;
private Button btn1, btn2, btn3;
private View view1, view2, view3;
private TextView tv;
private List<Fragment> fragmentList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager);
viewPager = (ViewPager) findViewById(R.id.viewPager);
view1 = findViewById(R.id.view1);
view2 = findViewById(R.id.view2);
view3 = findViewById(R.id.view3);
btn1 = (Button) findViewById(R.id.btn1_viewpager);
btn2 = (Button) findViewById(R.id.btn2_viewpager);
btn3 = (Button) findViewById(R.id.btn3_viewpager);
tv= (TextView) findViewById(R.id.tv_view);
view1.setBackgroundColor(Color.BLACK);
benFragment = new BenFragment();
shaFragment = new ShaFragment();
chunFragment = new ChunFragment();
fragmentList.add(benFragment);
fragmentList.add(shaFragment);
fragmentList.add(chunFragment);
Bundle bundle=new Bundle();
bundle.putString("name","张三");
benFragment.setArguments(bundle);
ViewAdapter adapter = new ViewAdapter(getSupportFragmentManager(), fragmentList);
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
view1.setBackgroundColor(Color.WHITE);
view2.setBackgroundColor(Color.WHITE);
view3.setBackgroundColor(Color.WHITE);
switch (position) {
case 0:
view1.setBackgroundColor(Color.BLACK);
break;
case 1:
view2.setBackgroundColor(Color.BLACK);
break;
case 2:
view3.setBackgroundColor(Color.BLACK);
break;
default:
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1_viewpager:
viewPager.setCurrentItem(0);
break;
case R.id.btn2_viewpager:
viewPager.setCurrentItem(1);
break;
case R.id.btn3_viewpager:
viewPager.setCurrentItem(2);
break;
default:
break;
}
}
public void test(String title){
tv.setText(title);
}
}
可以看到,我们重写了一个public的test的有参方法,String类型的参数用来等下传入需要传入的值,接下来就是在fragment里面接收,要调用activity的test方法,具体方法如下:
View view=inflater.inflate(R.layout.fragment_ben, container, false);
就这个返回给view视图,接下来我们去button的单击事件中调用test方法
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ViewPagerActivity viewPagerActivity= (ViewPagerActivity) getActivity();
viewPagerActivity.test("大笨妞");
}
});
return view;
当然,不能直接调用,需要创立activity的对象调用,传入值,最后不要忘了返回view视图
activity传值给Fragment的方法
这种传值方法我们就需要引入Bundle方法,具体调用方法如下
Bundle bundle=new Bundle();
bundle.putString("name","张三");
benFragment.setArguments(bundle);
这样传入值后就直接去fragment去接收
Bundle bundle=getArguments();
String name=bundle.getString("name");
button.setText(name);
接收用getArguments(),可以获取到Bundle的值,通过bundle.getString(“name”)得到传入的值
具体就是这两种方法,全部代码在上面已经全部写出来了,有兴趣的话可以去看看