封装代码和UI的利器—Fragment(一)

来源:互联网 发布:iapp源码大全下载2017 编辑:程序博客网 时间:2024/05/29 09:38

      fragment 这个单词的意思是“碎片”的意思,Google推出的Fragment 这项技术取得名字还是很不错的。就像一块拼图(Activity)一样,上面的小块(Fragment)不仅可以在不同设备和屏幕之间共享UI和逻辑,众所周知,布局用来封装UI,类来封装代码。而Fragment 技术将这两者完美结合在一起,真的是美妙极了。

之前android3.0之前是不支持平板电脑的,比如一个联系人的程序,可能你看到的第一个界面是一些联系人的名字,电话号码,等你点击了其中一个的时候会跳转到另外一个界面才会显示那个人的详细信息。这是因为手机的屏幕本来就很小,看上去操作也不是特别别扭吧。但是android3.0 后android开始支持平板电脑,这时候就碰到了问题,平板电脑的屏幕是很大的,有7寸,10寸的等等,想想这么大的屏幕显示一个联系人程序是不是很傻。因此就有了屏幕分配的问题了,想想可以在平板左边有一个窗口来显示联系人,往右显示点击的联系人的详细信息,将这两者显示在一个窗口上,这是不错的选择。因此才有Fragment的诞生,不是说以前的技术达不到在一个Activity中布置更多的UI,但是同时适应手机和平板的程序是比较麻烦的。

    (一)按照Fragment设计原则编写程序

这是我从网上弄来的一张图,这张图很明显道出了Fragment和Activity之间的关系。

     (二)Fragment的基本使用方法

Fragment 和Activity是非常类似的,每一个Fragment对应一个类,该类必须继承Fragment或其他的子类继承。Fragment 和Activity一样,也有一个用于初始化Fragment的方法。Fragment的初始化方法 是 onCreateView 。不同的是他需要将显示在Fragment中的View通过该方法返回,而不是像Activity中用setContentView方法来设置。

如果需要在某一个窗口嵌入一个Fragment,只需要在相应的布局文件中使用<fragment>标签即可。这个标签有个非常重要的class属性,用于指定Fragment类的全名。

(三)实例FirstFragment

实现目标 1:显示名胜古迹的列表

2:如果是平板电脑,单击列表项会在右侧显示名胜的介绍。如果是手机,会显示DetailActivity中。

1)LeftFragment

package com.example.firstfragment_01;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import android.annotation.SuppressLint;import android.app.Fragment;import android.content.Intent;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.TextView;@SuppressLint("NewApi")public class LeftFragment extends Fragment implements OnItemClickListener{private String[] data = {"马踏飞燕","秦始皇陵兵马俑","长城"};private ListView listView = null;@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stub View view = inflater.inflate(R.layout.left_fragment, null); listView = (ListView)view.findViewById(R.id.listView); listView.setOnItemClickListener(this); ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(),  android.R.layout.simple_list_item_activated_1, data);listView.setAdapter(arrayAdapter);listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);return view;}@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {// TODO Auto-generated method stubTextView textView = (TextView)getActivity().findViewById(R.id.textView);try {InputStream inputStream = getActivity().getResources().getAssets().open("m"+arg2);InputStreamReader is = new InputStreamReader(inputStream);byte[] buffer = new byte[1024];int count =inputStream.read(buffer);String detail = new String(buffer, 0, count, "gbk");if(textView == null){Intent intent = new Intent(getActivity(), DetailActivity.class);intent.putExtra("detail", detail);startActivity(intent);}else{textView.setText(detail);}inputStream.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

RightFragment.java

package com.example.firstfragment_01;import android.annotation.SuppressLint;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;@SuppressLint("NewApi")public class RightFragment extends Fragment{@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stubView view = inflater.inflate(R.layout.right_fragment, null);return view;}}

FirstFragmentActivity.java

package com.example.firstfragment_01;import android.os.Bundle;import android.app.Activity;import android.view.Menu;public class FirstFragmentActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_first_fragment);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.first, menu);return true;}}

DetailActivity.java

package com.example.firstfragment_01;import org.apache.http.util.EncodingUtils;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;public class DetailActivity extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.detail);TextView textView = (TextView)findViewById(R.id.textView);textView.setText(getIntent().getExtras().getString("detail"));}}
left_frament.xml
<RelativeLayout 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"    >    <fragment         android:id="@+id/left_fragment"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        class="com.example.firstfragment_01.LeftFragment"        /></RelativeLayout>

Detail.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"     android:layout_width="fill_parent"     android:layout_height="fill_parent">     <TextView      android:id="@+id/textView"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     /></LinearLayout>
left_Fragment.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"     android:layout_width="fill_parent"     android:layout_height="fill_parent">        <ListView         android:id="@+id/listView"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        ></ListView> </LinearLayout>
right_fragment.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"     android:layout_width="fill_parent"     android:layout_height="fill_parent">     <TextView      android:id="@+id/textView"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     /></LinearLayout>


主要代码在LeftFragment中,其他代码大家可以参考写。哦,我在调试过程中出现了中文乱码,我查了一些资料,大家可以用GBK国标码,这样就可以解决了中文乱码。

这个比较适合初学者复习,希望可以帮到大家。

0 0
原创粉丝点击