Android Fragment事件透傳

概述

近期使用Fragment,需要將事件透傳到下層(可能是Activity,也可能是其他Fragment),於是作此文記錄之。

原理其實很簡單,就是讓Fragment佈局中ViewGroup都會將觸摸事件傳遞到下層。主要是通過重寫dispatchTouchEvent方法來實現。

需要注意的是,如果Fragment佈局中嵌套較多,那麼每一層都需要重寫dispatchTouchEvent這個方法。

demo

在這裏插入圖片描述

代碼

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private TestFragment fragment;
    private Button btn;

    public static int count = 0;

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

        //設置fragment
        fragment = new TestFragment();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.add(android.R.id.content, fragment, "TestFragment");
        ft.commit();

        //設置button
        btn = findViewById(R.id.btn_main);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count ++;
                fragment.setTextTwo("MainActivity.btn點擊次數:"+count);
            }
        });
    }

}

activity_main.xml

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <Button
        android:id="@+id/btn_main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="add count" />

</LinearLayout>

TestFragment.java

public class TestFragment extends Fragment {

    //ui
    private LinearLayout linearLayout;
    private TextView mTextViewOne;
    private TextView mTextViewTwo;

    //data
    private int count = 0;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //將事件傳遞給Activity中的btn
        linearLayout = new LinearLayout(getContext()){
            @Override
            public boolean dispatchTouchEvent(MotionEvent ev) {
                super.dispatchTouchEvent(ev);
                return false;
            }
        };
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.setBackgroundColor(getResources().getColor(R.color.mBlack));

        mTextViewOne = new TextView(getContext());
        mTextViewOne.setTextSize(50);
        mTextViewOne.setTextColor(Color.WHITE);
        linearLayout.addView(mTextViewOne);

        mTextViewTwo = new TextView(getContext());
        mTextViewTwo.setTextSize(50);
        mTextViewTwo.setTextColor(Color.WHITE);
        linearLayout.addView(mTextViewTwo);


        mTextViewTwo.setText("MainActivity.btn點擊次數:"+count);
        mTextViewOne.setText("fragment點擊次數:"+count);
        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count++;
                mTextViewOne.setText("fragment點擊次數:"+count);
            }
        });

        return linearLayout;
    }

    public void setTextTwo(String text) {
        if (mTextViewTwo != null) {
            mTextViewTwo.setText(text);
        }
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章