Android中Activity调用Fragment事件,通过回调实现。

来源:互联网 发布:财务会计软件目的 编辑:程序博客网 时间:2024/04/30 12:57

效果图如下:


整个是一个Activity,下面部分为Fragment

我们通过点击Fragment区域在Activity中实现Fragment中的方法。

拓展:我们可以通过点击Fragment中的按钮在Activity中实现Fragment的替换。

public class InterfaceActivity extends AppCompatActivity {    FragmentManager fm;    InterfaceFragment iff;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_interface);        if (null==iff){            iff = new InterfaceFragment();        }        if (null==fm){            fm = getSupportFragmentManager();        }        fm.beginTransaction().add(R.id.id_interface_ll,iff).show(iff).addToBackStack(null).commit();        iff.setTestInterface(new InterfaceFragment.TestInterface() {            @Override            public void showText() {                Toast.makeText(InterfaceActivity.this,"你点击了Fragment",Toast.LENGTH_SHORT).show();            }        });    }}

布局文件 activity_interface.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">   <TextView       android:text="下面是Fragment"       android:layout_width="wrap_content"       android:layout_height="wrap_content" />    <LinearLayout        android:orientation="vertical"        android:id="@+id/id_interface_ll"        android:layout_width="match_parent"        android:layout_height="match_parent"/></LinearLayout>
布局文件 fragment_interface.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#b9be71b9"    android:orientation="vertical">    <TextView        android:textSize="20sp"        android:gravity="center"        android:layout_gravity="center|center_vertical"        android:id="@+id/id_interface_btn"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="@null"        android:text="这是Fragment" /></LinearLayout>
代码 InterfaceFragment

public class InterfaceFragment extends Fragment {    TestInterface testInterface;    private TextView tv;    /**     * 回调     * @param testInterface     */    public void setTestInterface(TestInterface testInterface) {        this.testInterface = testInterface;    }    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.fragment_interface,container,false);        tv = (TextView) view.findViewById(R.id.id_interface_btn);        view.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                //方法回调,在Activity中实现                testInterface.showText();                tv.setText("你点击了Fragment");            }        });        return view;    }    /**     * 定义回调接口     */    public interface TestInterface{        void showText();    }}





0 0