Android中利用Fragment同时支持屏幕和手机

1.设计横屏时屏幕左边的样式,定义一个类FragmentA继承自ListFragment
重写onAttach()方法,并且定义数据源。
2.在onAttach()方法中设置我们的适配器用于给FragmentA提供数据

String[] data={"计算机组成原理","操作系统原理","编译原理","算法","JAVAEE","JAVAWEB","Python",
            "PHP","C++"};
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        //由于我们继承的是ListFragment所以设置适配器的时候我们直接设置,适配器的类型是listAdatapter
        setListAdapter(new ArrayAdapter<String>(
                getActivity(),                      //第一个参数是上下文
                android.R.layout.simple_expandable_list_item_2, //设置样式,项的样式
                android.R.id.text1,                 //要设置数据源的项的id
                data    
                ));
    }

3.新建一个FragmentB继承自Fragment,在布局文件中定义fragment_b的布局文件并在FragmentB中解析: public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_b,null);
return view;
}
//找到布局文件中的控件,因此要重写以下的方法,必须调用view的findviewbyid的方法
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
textview=(TextView) view.findViewById(R.id.textView1);
}

4.这里我们想让FragmentA来控制FragmentB的内容更改,为了降低程序的耦合性,我们设置接口

public interface SelectedItemListener {
    public void selectedItem(String text);
}

5.当竖屏的时候,我们仅显示FragmentA,所以我们在activity_main中加入FragmentA:

   <fragment
        android:id="@+id/fragment1"
        android:name="com.example.androiddemo001.Fragment.FragmentA"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

此时我们要显示第二个界面,所以在layout文件夹下面添加布局文件activity_selected,添加FragmentB,用于显示FragmentB:

    <fragment
        android:id="@+id/fragment1"     android:name="com.example.androiddemo001.Fragment.FragmentB"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

6.此时我们的竖屏界面已经准备完毕。我们为了显示横屏呈现不同的效果,在res下新建文件夹:layout-land:然后将actvity_main复制粘贴到此文件夹下。
这时候,修改布局文件,放两个
Fragment不是放一个:

<LinearLayout 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"
    android:orientation="horizontal"
    tools:context="com.example.androiddemo001.MainActivity" >
    <LinearLayout 
        android:layout_width="0dp"
        android:layout_weight="30"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#FF0000"
        >
        <fragment
            android:id="@+id/fragment1"
            android:name="com.example.androiddemo001.Fragment.FragmentA"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>
    <LinearLayout 
        android:layout_width="0dp"
        android:layout_weight="70"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#00FF00"
        >
        <fragment
            android:id="@+id/fragment2"   android:name="com.example.androiddemo001.Fragment.FragmentB"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
</LinearLayout>

7.由于我们使用support.v4包下的Fragment所以MainActivity要继承于FragmentActivity

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值