Fragment使用(下),附带学习经验

本文详细介绍了Android中Fragment的使用,包括Fragment向Activity传值的实现方式,Fragment间通信的多种方法,以及ListFragment的应用场景。通过代码实例解析了各个步骤,并提供了总结要点。
摘要由CSDN通过智能技术生成
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout

xmlns:android=“http://schemas.android.com/apk/res/android”

android:id="@+id/rl_content"

android:layout_width=“match_parent”

android:layout_height=“match_parent”>

<TextView

android:id="@+id/tv_show"

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:layout_centerInParent=“true”

android:gravity=“center”

android:text=“TEXT”

android:textColor="@color/light_blue_2e"

android:textSize=“30sp”/>

1.1.2、总结

1.2、所属的Fragment向Activity传值


1.2.1、代码实例

Test6FragmentActivity

public class Test6FragmentActivity extends AppCompatActivity implements ResourceFragment.MyListener {

private TextView tvShow;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_test6_fragment);

tvShow = findViewById(R.id.tv_show);

}

@Override

public void sendMessage(String msg) {

if (!TextUtils.isEmpty(msg)){

tvShow.setText(msg);

}

}

}

布局文件:

<?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”

tools:context=".fragment.test6.Test6FragmentActivity">

<TextView

android:id="@+id/tv_show"

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:text=“Text”

android:layout_centerHorizontal=“true”

android:textSize=“24sp”

android:gravity=“center”

android:textColor="@color/light_blue_2e"

android:layout_margin=“10dp”

/>

<fragment

android:id="@+id/fragment_resource"

android:name=“com.gs.common3.fragment.test6.ResourceFragment”

android:layout_width=“match_parent”

android:layout_height=“300dp”

android:layout_below="@id/tv_show"

android:layout_marginTop=“20dp”/>

Reso

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值