您的位置:首页 > 移动开发 > Android开发

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

2016-06-15 10:16 555 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: