安卓基础回顾9:Fragment与activity的通信

Fragment与activity的通信

一.Frament控制Activity中的控件

1.在fragment中找到当前视图

这里写图片描述

2.修改成

这里写图片描述

3.在activity中添加改变text的方法

这里写图片描述

4.对FragmentOne中的TextView添加单击事件,使点击后改变activity中的Text

注意:下图红框非常重要请看代码注释

这里写图片描述

实现效果:
这里写图片描述
这里写图片描述

代码如下:

FragmentOne



package com.example.a22120.sildefragmentviewpage;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


/**
 * A simple {@link Fragment} subclass.
 */
public class FragmentOne extends Fragment {
        transient TextView textView;
    private static final String TAG ="FragmentOne" ;

    public FragmentOne() {
        // Required empty public constructor
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        Log.d(TAG, "onCreateView:1111111");

 /*    return inflater.inflate(R.layout.fragment_fragment_one, container, false);*/

     View view=inflater.inflate(R.layout.fragment_fragment_one, container, false); //为当前视图

     textView=view.findViewById(R.id.text1); //使用当前的Fragment视图绑定Fragment的控件id

      textView.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              MainActivity mainActivity= (MainActivity) getActivity(); //创建activity对象用get方法得到对象
              mainActivity.setToptv("我是第一个Fragment"); //调用activity中的setToptv()方法
          }
      });
     return view;
    }
}

Activity

package com.example.a22120.sildefragmentviewpage;

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.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private ViewPager viewPager;
    List<Fragment> fragmentList;
    FragmentOne fragmentOne;
    FargmentTwo fargmentTwo;
    FragmentThree 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);

        viewPager = findViewById(R.id.lay_vp);
        chars = findViewById(R.id.lay_charTop);
        friends = findViewById(R.id.lay_friendsTop);
        contents = findViewById(R.id.lay_contentTop);
        chars_bom = findViewById(R.id.chars_bom);
        content_bom = findViewById(R.id.content_bom);
        friend_bom = findViewById(R.id.frident_bom);
        top_tv=findViewById(R.id.top_tv);

        //设置按钮监听
        chars.setOnClickListener(this);
        friends.setOnClickListener(this);
        contents.setOnClickListener(this);

        fragmentList = new ArrayList<>();

        fragmentOne = new FragmentOne();
        fargmentTwo = new FargmentTwo();
        fragmentThree=new FragmentThree();
        fragmentList.add(fragmentOne);
        fragmentList.add(fargmentTwo);
        fragmentList.add(fragmentThree);

        chars_bom.setBackgroundColor(Color.BLUE);
        Adapter adapter = new Adapter(getSupportFragmentManager(), fragmentList);
        viewPager.setAdapter(adapter);

        //设置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) {

            }
        });
    }

    @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;
        }
    }
        public void setToptv(String s){
        top_tv.setText(s);
        }
}

注:该例子详细布局和代码在上一章 安卓基础回顾8:Fragment初识

链接: https://blog.csdn.net/qq_38845493/article/details/80582969

二.Activity对Fragment中的控件进行操作

1.为了实现将Activity的数据传递至Fragment中,并使用

这里写图片描述

2.在MainActivity中打包数据
这里写图片描述
代码:

  Bundle bundle = new Bundle();  //包裹
  bundle.putString("name", "小泽");  //打包
  fragmentTwo.setArguments(bundle); //发送

3.在Fragment中接收数据

这里写图片描述

  Bundle bundle=getArguments();
         String name=bundle.getString("name");
         textView.setText(name);

实现效果改变Fragment界面
这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_38845493/article/details/80591657