Fragment相关知识点及使用(一)

一、Fragment的静态使用

1、根据需要创建Fragment

  • 1) 创建子类继承Fragment
  • 2) 重写onCreateView()方法,为Fragment设置xml文件,转换成view对象返回

2、在activity布局中,通过标签引入fragment

android:name=”fragment的包名.类名”

在activity布局文件中设置如下:

<?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"
    android:id="@+id/layout">

    <fragment
        android:id="@+id/fragment_title"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:name="com.example.android.staticfragment.com.example.android.fragment.TitleFragment"/>
    <fragment
        android:id="@+id/fragment_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/fragment_title"
        android:name="com.example.android.staticfragment.com.example.android.fragment.ContentFragment"/>
	<!-- android:name表示引入fragment的包名.类名 -->
</RelativeLayout>

注意点:

  1. 静态加载一旦添加就不能在运行时删除
  2. fragment一定要有一个没有参数的(默认)构造函数
    内存重启 自动恢复的时候只会调用无参的构造函数

二、Fragment的动态使用

1、创建Fragment的管理器对象
2、获取Fragment的 事务对象并开启事务
3、调用事务中相应的的动态操作Fragment的方法执行

  • add:表示fragment动态添加位置的资源id, 表示添加的fragment对象。
  • remove:需要移除的fragment对象。
  • replace:表示替换fragment位置的资源id, 表示替换fragment对象。

4、提交事务
比如:
(1)创建Fragment的管理对象

FragmentManager fragmentManager = getFragmentManager();

(2)获取Fragment的事务对象并开启事务

FragmentTransaction transaction = fragmentManager.beginTransaction();

(3)调用事务中相应的动态操作Fragment的方法执行

transaction.add(R.id.tv_title, new TitleFragment());
transaction.add(R.id.tv_content, new ContentFragment());

(4)提交事务

transaction.commit();

三、Activity向Fragment传值

步骤如下:
1、Activity中创建Fragment对象,调用setArguments(bundle)方法存储值

MyFragment1 myFragment1 = new MyFragment1();
Bundle bundle = new Bundle();
bundle.putString("info", info);
myFragment1.setArguments(bundle);
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.layoutcontent, myFragment1);
transaction.commit();

2、Fragment中调用getArguments()获取传递的Bundle对象解析获取具体值

Bundle bundle = getArguments();
String info = bundle.getString("info");

四、Fragment向Activity传值

步骤如下:

1、Fragment中定义传值的回调接口,在声明周期onAttach()/onCreate()/onCreateView()方法中获取接口的 方法

public class MyFragment2 extends Fragment {
    private MyListener2 myListener2;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		… …
        myListener2 = (MyListener2) getActivity();
        myListener2.sendMessage(message);
		… …
    }

    public interface MyListener2{
        void sendMessage(String str);
    }
}

2、Fragment需要传值的位置调用回调方法传值

public class MyActivity2 extends Activity implements MyFragment2.MyListener2 {
    @Override
    public void sendMessage(String str) {
        … …
    }
}

五、Fragment与Fragment之间的传值

同一个Activity中不同Fragment之间传值

方式1: 可以调用findFragmentById()方法根据id获取fragment的对象,调用framgent中的方法赋值

RightFragment rightFragment = (RightFragment) getFragmentManager().findFragmentById(R.id.layoutright);
 rightFragment.showTextView(info);

方式2:先调用getFragmentManager()获取fragmentManager对象,然后调用findFragmentById()方法获取右侧fragment, 再调用getview()获取右侧fragment的view,最后调用findViewById()获得赋值的控件

 TextView rigth_show = (TextView) getFragmentManager().findFragmentById(R.id.layoutright).getView().findViewById(R.id.right_show);
 rigth_show.setText(info);

方式3:先调用getActivity()方法获取所属activity的对象,然后调用findViewById()获取目标控件

 TextView rigth_show = getActivity().findViewById(R.id.right_show);
 rigth_show.setText(info);

方式4:先从一个Fragment中传值给Activity,然后调用setArguments和getArguments()来进行传值

六、ListFragment

需要主要两点:
1、在创建ListFragment布局文件时,ListView控件id必须为“@id/android:list”
比如:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>
</RelativeLayout>

2、绑定数据,采用setListAdapter()方法,绑定每项点击事件重写onListItemClick()方法

public class MyListFragment extends ListFragment {
    private List<String> list;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.listfragment, null);
        list = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            list.add("item " + i);
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list);
        setListAdapter(adapter);
        return view;
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Toast.makeText(getActivity(), list.get(position) + "被单击了", Toast.LENGTH_SHORT).show();
    }
}

七、DialogFlagment

1、创建子类继承DialogFragment
2、重写onCreateDialog()方法,返回当前需要展示的Dialog对象

public class MyDialogFragment extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("提示");  // 设置对话框标题
        builder.setIcon(R.drawable.ic_launcher_foreground);  // 设置对话框图标
        builder.setMessage("您确定要退出吗?"); // 设置对话框提示信息
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {  // 设置确定按钮点击事件
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.setNegativeButton("取消",null);  // 设置取消按钮点击事件
        return builder.create(); // 将构建的dialog对象返回
    }
}

3、创建DialogFragment的子类对象,并且调用show()方法显示

MyDialogFragment myDialogFragment = new MyDialogFragment();
myDialogFragment.show(getFragmentManager(), "dialog");

特别注意:
至少实现OnCreateView()或者onCreateDialog()方法
onCreateView():表示以xml布局文件的形式展示dialog
onCreateDialog():利用alertDialog或者dialog创建

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值