关于接口的第二个demo

InterfaceListener

public interface InterfaceListener {

    void getData(Object data);
}
MainActivity
public class MainActivity extends AppCompatActivity implements  TabLayout.BaseOnTabSelectedListener {
    private LeftFragment mLeftFragment = new LeftFragment();
    private RightFragment mRightFragment = new RightFragment();
    private FragmentManager manager;

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

    private void initView() {
        TabLayout mTabLayout = findViewById(R.id.main_tl);
        mTabLayout.addTab(mTabLayout.newTab().setText("LeftFragment"));
        mTabLayout.addTab(mTabLayout.newTab().setText("RightFragment"));
        mTabLayout.addOnTabSelectedListener(this);

        manager = getSupportFragmentManager();
        manager.beginTransaction()
                .add(R.id.main_fl, mLeftFragment, "LeftFragment")
                .add(R.id.main_fl, mRightFragment, "RightFragment")
                .hide(mRightFragment)
                .show(mLeftFragment)
                .commit();
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        if (tab.getPosition() == 0) {
            manager.beginTransaction().hide(mRightFragment).show(mLeftFragment).commit();
        } else {
            manager.beginTransaction().hide(mLeftFragment).show(mRightFragment).commit();
        }
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
}

activity_main

<?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"
    tools:context=".MainActivity">
    <FrameLayout
        android:id="@+id/main_fl"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/main_tl"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

</LinearLayout>
LeftFragment
public class LeftFragment extends Fragment implements InterfaceListener, View.OnClickListener {

    private TextView mTextView;
    private InterfaceListener mLeftListener;

    public void setLeftListener(InterfaceListener mLeftListener) {
        this.mLeftListener = mLeftListener;
    }

    public LeftFragment() {
        // 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_left, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        Button mButton = view.findViewById(R.id.left_fragment_btn);
        mButton.setOnClickListener(this);
        mTextView = view.findViewById(R.id.left_fragment_tv);
        mTextView.setText("我是LeftFragment");
    }

    @Override
    public void onResume() {
        super.onResume();
        RightFragment mRightFragment = (RightFragment) getActivity().getSupportFragmentManager().findFragmentByTag("RightFragment");
        mRightFragment.setInterfaceListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left_fragment_btn:
                if (mLeftListener != null) {
                    mLeftListener.getData("31eqw56e4qw31e5qw4");
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void getData(Object data) {
        Toast.makeText(getActivity(), "LeftFragment改变成功", Toast.LENGTH_SHORT).show();
        mTextView.setText((CharSequence) data);
    }
}
fragment_left

<RelativeLayout 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"
    tools:context=".fragment.LeftFragment">

    <!-- TODO: Update blank fragment layout -->

    <Button
        android:id="@+id/left_fragment_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="点我改变RightFragment的值" />

    <TextView
        android:id="@+id/left_fragment_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

</RelativeLayout>
RightFragment
public class RightFragment extends Fragment implements View.OnClickListener, InterfaceListener {

    private TextView mTextView;
    private InterfaceListener mInterfaceListener;

    public void setInterfaceListener(InterfaceListener mInterfaceListener) {
        this.mInterfaceListener = mInterfaceListener;
    }

    public RightFragment() {
        // 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_right, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        mTextView = view.findViewById(R.id.right_fragment_tv);
        mTextView.setText("我是RightFragment");
        Button mButton = view.findViewById(R.id.right_fragment_btn);
        mButton.setOnClickListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        LeftFragment mLeftFragment = (LeftFragment) getActivity().getSupportFragmentManager().findFragmentByTag("LeftFragment");
        mLeftFragment.setLeftListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.right_fragment_btn:
                if (mInterfaceListener != null) {
                    mInterfaceListener.getData("我是RightFragment传过来的");
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void getData(Object data) {
        Toast.makeText(getActivity(), "RightFragment改变成功", Toast.LENGTH_SHORT).show();
        mTextView.setText((CharSequence) data);
    }
}

fragment_right

<RelativeLayout 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"
    tools:context=".fragment.LeftFragment">

    <!-- TODO: Update blank fragment layout -->

    <Button
        android:id="@+id/left_fragment_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="点我改变RightFragment的值" />

    <TextView
        android:id="@+id/left_fragment_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

</RelativeLayout>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值