生活随笔
收集整理的這篇文章主要介紹了
ViewPage+Fragment的使用例子
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2013年9月6日Fragment學習
Fragment這個東西,我到現在才接觸到,之前沒有用到過,關于Fragment這個東西在官方文檔已經介紹了非常清楚了,我還特地轉載了官方API的中文翻譯,在這我就不浪費口舌了。簡單來說,Fragment就是為滿足大屏幕的而誕生的,手機屏幕小而平板屏幕就大了,屏幕一大就會產生更多的交互,這是很容易想到的,那么單純的Activity就不能滿足我們的需求了。
今天這個例子是自己做的一個小Demo,簡單使用了Fragment,是跟ViewPage結合一起使用的。我想實現的效果是,左右滑動可以從一個Fragment到另一個Fragment的過渡。
看看效果圖:
可以看到,兩個fragment都可有控件的點擊事件,如果是兩個Activity我不知道怎么實現,可能會稍微麻煩一點。
簡單的ViewPage布局:
/2013.09.05_ViewPage_Fragment_Demo/res/layout/activity_main.xml
[html]?view plaincopy
<?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"?>?? ????<android.support.v4.view.ViewPager?? ????????android:id="@+id/viewpager"?? ????????android:layout_width="wrap_content"?? ????????android:layout_height="wrap_content"?? ????????android:flipInterval="30"?? ????????android:persistentDrawingCache="animation"?/>?? </RelativeLayout>??
/2013.09.05_ViewPage_Fragment_Demo/res/layout/viewpager1.xml
[html]?view plaincopy
<?xml?version="1.0"?encoding="UTF-8"?>?? <RelativeLayout?xmlns:android="http://schemas.android.com/apk/res/android"?? ????android:layout_width="fill_parent"?? ????android:layout_height="fill_parent"?? ????android:background="@drawable/guide_1"?>?? ?? ????<Button?? ????????android:id="@+id/btn"?? ????????android:layout_width="wrap_content"?? ????????android:layout_height="wrap_content"?? ????????android:layout_alignParentBottom="true"?? ????????android:layout_centerHorizontal="true"?? ????????android:layout_marginBottom="54dp"?? ????????android:background="@drawable/butten"?? ????????/>?? ?? </RelativeLayout>??
/2013.09.05_ViewPage_Fragment_Demo/res/layout/viewpager2.xml
[html]?view plaincopy
<?xml?version="1.0"?encoding="UTF-8"?>?? <RelativeLayout?xmlns:android="http://schemas.android.com/apk/res/android"?? ????android:layout_width="fill_parent"?? ????android:layout_height="fill_parent"?? ????android:background="@drawable/guide_2"?>?? ?? ????<Button?? ????????android:id="@+id/btn"?? ????????android:layout_width="wrap_content"?? ????????android:layout_height="wrap_content"?? ????????android:layout_alignParentBottom="true"?? ????????android:layout_centerHorizontal="true"?? ????????android:layout_marginBottom="54dp"?? ????????android:background="@drawable/butten"?/>?? ?? </RelativeLayout>??
再來一個適配器:
/2013.09.05_ViewPage_Fragment_Demo/src/com/wwj/vf/adapter/FragAdapter.java
[java]?view plaincopy
package?com.wwj.vf.adapter;?? ?? import?java.util.List;?? ?? import?android.support.v4.app.Fragment;?? import?android.support.v4.app.FragmentManager;?? import?android.support.v4.app.FragmentPagerAdapter;?? ? ? ? ? ?? public?class?FragAdapter?extends?FragmentPagerAdapter{?? ?????? ????private?List<Fragment>?fragments;?? ?????? ?? ????public?FragAdapter(FragmentManager?fm)?{?? ????????super(fm);?? ????}?? ?????? ????public?FragAdapter(FragmentManager?fm,?List<Fragment>?fragments)?{?? ????????super(fm);?? ????????this.fragments?=?fragments;?? ????}?? ?? ????@Override?? ????public?Fragment?getItem(int?position)?{?? ????????return?fragments.get(position);?? ????}?? ?? ????@Override?? ????public?int?getCount()?{?? ????????return?fragments.size();?? ????}?? }??
兩個自定義的Fragment
這里很簡單,一張背景+一個按鈕
/2013.09.05_ViewPage_Fragment_Demo/src/com/wwj/vf/ui/MyFragment1.java
[java]?view plaincopy
package?com.wwj.vf.ui;?? ?? import?com.wwj.vf.R;?? ?? import?android.os.Bundle;?? import?android.support.v4.app.Fragment;?? import?android.view.LayoutInflater;?? import?android.view.View;?? import?android.view.View.OnClickListener;?? import?android.view.ViewGroup;?? import?android.widget.Button;?? import?android.widget.Toast;?? ? ? ? ? ? ?? public?class?MyFragment1?extends?Fragment?{?? ????private?Button?btn;?? ????@Override?? ????public?void?onCreate(Bundle?savedInstanceState)?{?? ????????super.onCreate(savedInstanceState);?? ?????????? ????}?? ?????? ?????? ????@Override?? ????public?View?onCreateView(LayoutInflater?inflater,?ViewGroup?container,?? ????????????Bundle?savedInstanceState)?{?? ????????View?view?=?inflater.inflate(R.layout.viewpager1,?container,?false);?? ????????btn?=?(Button)?view.findViewById(R.id.btn);?? ????????btn.setOnClickListener(new?OnClickListener()?{?? ?????????????? ????????????@Override?? ????????????public?void?onClick(View?v)?{?? ????????????????Toast.makeText(getActivity(),?"你點我啦,好壞!!!",?Toast.LENGTH_LONG).show();?? ????????????}?? ????????});?? ????????return?view;?? ????}?? ?????? ????@Override?? ????public?void?onActivityCreated(Bundle?savedInstanceState)?{?? ????????super.onActivityCreated(savedInstanceState);?? ????}?? ?????? ????@Override?? ????public?void?onPause()?{?? ????????super.onPause();?? ????}?? ?????? }??
/2013.09.05_ViewPage_Fragment_Demo/src/com/wwj/vf/ui/MyFragment2.java
[java]?view plaincopy
package?com.wwj.vf.ui;?? ?? import?android.os.Bundle;?? import?android.support.v4.app.Fragment;?? import?android.view.LayoutInflater;?? import?android.view.View;?? import?android.view.View.OnClickListener;?? import?android.view.ViewGroup;?? import?android.widget.Button;?? import?android.widget.Toast;?? ?? import?com.wwj.vf.R;?? ? ? ? ? ? ?? public?class?MyFragment2?extends?Fragment?{?? ?????? ????@Override?? ????public?void?onCreate(Bundle?savedInstanceState)?{?? ????????super.onCreate(savedInstanceState);?? ????}?? ?????? ?????? ????@Override?? ????public?View?onCreateView(LayoutInflater?inflater,?ViewGroup?container,?? ????????????Bundle?savedInstanceState)?{?? ????????View?view?=?inflater.inflate(R.layout.viewpager2,?container,?false);?? ????????view.findViewById(R.id.btn).setOnClickListener(new?OnClickListener()?{?? ?????????????? ????????????@Override?? ????????????public?void?onClick(View?v)?{?? ????????????????Toast.makeText(getActivity(),?"這個Fragment2哦,要注意了!!",?Toast.LENGTH_SHORT).show();?? ????????????}?? ????????});?? ????????return?view;?? ????}?? ?????? ????@Override?? ????public?void?onActivityCreated(Bundle?savedInstanceState)?{?? ????????super.onActivityCreated(savedInstanceState);?? ????}?? ?????? ????@Override?? ????public?void?onPause()?{?? ????????super.onPause();?? ????}?? ?????? }??
好啦,在Activity里添加兩個Fragment
[java]?view plaincopy
package?com.wwj.vf;?? ?? import?java.util.ArrayList;?? import?java.util.List;?? ?? import?android.os.Bundle;?? import?android.support.v4.app.Fragment;?? import?android.support.v4.app.FragmentActivity;?? import?android.support.v4.view.ViewPager;?? ?? import?com.wwj.vf.adapter.FragAdapter;?? import?com.wwj.vf.ui.MyFragment1;?? import?com.wwj.vf.ui.MyFragment2;?? ? ? ? ? ? ?? public?class?MainActivity?extends?FragmentActivity?{?? ?????? ????private?ViewPager?vp;?? ????private?FragAdapter?adapter;?? ????@Override?? ????protected?void?onCreate(Bundle?savedInstanceState)?{?? ????????super.onCreate(savedInstanceState);?? ????????setContentView(R.layout.activity_main);?? ????????vp?=?(ViewPager)?findViewById(R.id.viewpager);?? ?????????? ????????List<Fragment>?fragments?=?new?ArrayList<Fragment>();?? ????????fragments.add(new?MyFragment1());?? ????????fragments.add(new?MyFragment2());?? ?????????? ????????adapter?=?new?FragAdapter(getSupportFragmentManager(),?fragments);?? ????????vp.setAdapter(adapter);?? ????????vp.setCurrentItem(0);?? ????}?? ?? }??
簡單的ViewPage和Fragment的搭配使用就這樣了
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的ViewPage+Fragment的使用例子的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。