![【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】怎么在ViewPage中使用Fragment模擬一個(gè)微信界面_第1頁(yè)](http://file4.renrendoc.com/view/660a24318ad4acd4781a15e032ff5d5c/660a24318ad4acd4781a15e032ff5d5c1.gif)
![【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】怎么在ViewPage中使用Fragment模擬一個(gè)微信界面_第2頁(yè)](http://file4.renrendoc.com/view/660a24318ad4acd4781a15e032ff5d5c/660a24318ad4acd4781a15e032ff5d5c2.gif)
![【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】怎么在ViewPage中使用Fragment模擬一個(gè)微信界面_第3頁(yè)](http://file4.renrendoc.com/view/660a24318ad4acd4781a15e032ff5d5c/660a24318ad4acd4781a15e032ff5d5c3.gif)
![【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】怎么在ViewPage中使用Fragment模擬一個(gè)微信界面_第4頁(yè)](http://file4.renrendoc.com/view/660a24318ad4acd4781a15e032ff5d5c/660a24318ad4acd4781a15e032ff5d5c4.gif)
![【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】怎么在ViewPage中使用Fragment模擬一個(gè)微信界面_第5頁(yè)](http://file4.renrendoc.com/view/660a24318ad4acd4781a15e032ff5d5c/660a24318ad4acd4781a15e032ff5d5c5.gif)
版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】怎么在ViewPage中使用Fragment模擬一個(gè)微信界面
這期內(nèi)容當(dāng)中在下將會(huì)給大家?guī)?lái)有關(guān)怎么在ViewPage中使用Fragment模擬一個(gè)微信界面,文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。界面與碎片:主界面:<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="/apk/res/android"
xmlns:app="/apk/res-auto"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.g160628_android10_viewpagerfragment_zuoye.MainActivity">
<!--設(shè)置ViewPager與單選組-->
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/vp_main_view"
android:layout_weight="1"
></android.support.v4.view.ViewPager>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/rg_main_group"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:background="@drawable/button_selector"
android:id="@+id/rb_main_bu1"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:background="@drawable/button2_selector"
android:id="@+id/rb_main_bu2"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:background="@drawable/button3_selector"
android:id="@+id/rb_main_bu3"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:background="@drawable/button4_selector"
android:id="@+id/rb_main_bu4"
/>
</RadioGroup>
</LinearLayout>在drawable中創(chuàng)建四個(gè)選擇器button_selector.xml<?xml
version="1.0"
encoding="utf-8"?>
<selector
xmlns:android="/apk/res/android">
<item
android:state_checked="false"
android:drawable="@drawable/small_weixin"></item>
<item
android:state_checked="true"
android:drawable="@drawable/small_weixin2"></item>
</selector>button2_selector.xml<?xml
version="1.0"
encoding="utf-8"?>
<selector
xmlns:android="/apk/res/android">
<item
android:state_checked="false"
android:drawable="@drawable/small_contact"></item>
<item
android:state_checked="true"
android:drawable="@drawable/small_contact2"></item>
</selector>button3_selector.xml<?xml
version="1.0"
encoding="utf-8"?>
<selector
xmlns:android="/apk/res/android">
<item
android:state_checked="false"
android:drawable="@drawable/small_find"></item>
<item
android:state_checked="true"
android:drawable="@drawable/small_find2"></item>
</selector>button4_selector.xml<?xml
version="1.0"
encoding="utf-8"?>
<selector
xmlns:android="/apk/res/android">
<item
android:state_checked="false"
android:drawable="@drawable/small_mine"></item>
<item
android:state_checked="true"
android:drawable="@drawable/small_mine2"></item>
</selector>四個(gè)碎片:fragment_weixin.xml<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="470dp"
android:layout_height="720dp"
android:src="@drawable/weixin"
/>
</LinearLayout>fragment_contact.xml<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="470dp"
android:layout_height="720dp"
android:src="@drawable/contact"
/>
</LinearLayout>fragment_find.xml<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="470dp"
android:layout_height="720dp"
android:src="@drawable/find"
/>
</LinearLayout>fragment_mine.xml<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="470dp"
android:layout_height="720dp"
android:src="@drawable/mine"
/>
</LinearLayout>Java代碼主Activity:package
com.example.g160628_android10_viewpagerfragment_zuoye;
import
android.content.res.Resources;
import
android.support.annotation.IdRes;
import
android.support.v4.app.Fragment;
import
android.support.v4.app.FragmentManager;
import
android.support.v4.app.FragmentPagerAdapter;
import
android.support.v4.view.ViewPager;
import
android.support.v7.app.AppCompatActivity;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.RadioButton;
import
android.widget.RadioGroup;
import
java.util.ArrayList;
import
java.util.List;
public
class
MainActivity
extends
AppCompatActivity
{
private
List<Fragment>
fragments=new
ArrayList<>();
private
ViewPager
vp_main_view;
private
RadioGroup
rg_main_group;
private
List<View>
views;
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//把碎片加入到碎片集合中
fragments.add(new
WeiXinFragment());
fragments.add(new
ContactFragment());
fragments.add(new
FindFragment());
fragments.add(new
MineFragment());
//找到自己的ViewPager
vp_main_view
=
(ViewPager)
findViewById(R.id.vp_main_view);
vp_main_view.setAdapter(new
MyAdapter(getSupportFragmentManager()));
//設(shè)置當(dāng)前的碎片為1
vp_main_view.setCurrentItem(1);
//獲得單選按鈕組
rg_main_group
=
(RadioGroup)
findViewById(R.id.rg_main_group);
//設(shè)置單選按鈕組的選擇事件
rg_main_group.setOnCheckedChangeListener(new
RadioGroup.OnCheckedChangeListener()
{
@Override
public
void
onCheckedChanged(RadioGroup
group,
@IdRes
int
checkedId)
{
Resources
res=MainActivity.this.getResources();
switch
(checkedId)
{
case
R.id.rb_main_bu1:
vp_main_view.setCurrentItem(0);
break;
case
R.id.rb_main_bu2:
vp_main_view.setCurrentItem(1);
break;
case
R.id.rb_main_bu3:
vp_main_view.setCurrentItem(2);
break;
case
R.id.rb_main_bu4:
vp_main_view.setCurrentItem(3);
break;
}
}
});
//獲得所有的單選框
views=rg_main_group.getTouchables();
//設(shè)置單選框事件
vp_main_view.setOnPageChangeListener(new
ViewPager.OnPageChangeListener()
{
@Override
public
void
onPageScrolled(int
position,
float
positionOffset,
int
positionOffsetPixels)
{
}
@Override
public
void
onPageSelected(int
position)
{
//設(shè)置選中
RadioButton
button=
(RadioButton)
views.get(position);
button.setChecked(true);
}
@Override
public
void
onPageScrollStateChanged(int
state)
{
}
});
}
//定義屬于自己的適配器
class
MyAdapter
extends
FragmentPagerAdapter{
public
MyAdapter(FragmentManager
fm)
{
super(fm);
}
//獲得碎片的所有
@Override
public
Fragment
getItem(int
position)
{
return
fragments.get(position);
}
//返回碎片的長(zhǎng)度
@Override
public
int
getCount()
{
return
fragments.size();
}
}
}四個(gè)碎片對(duì)應(yīng)的FragmentWeiXinFragmentpackage
com.example.g160628_android10_viewpagerfragment_zuoye;
import
android.os.Bundle;
import
android.support.annotation.Nullable;
import
android.support.v4.app.Fragment;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.view.ViewGroup;
/**
*
Created
by
Administrator
on
2017/6/15.
*/
public
class
WeiXinFragment
extends
Fragment
{
@Nullable
@Override
public
View
onCreateView(LayoutInflater
inflater,
@Nullable
ViewGroup
container,
@Nullable
Bundle
savedInstanceState)
{
//返回對(duì)應(yīng)的fragment_weixin
return
inflater.inflate(R.layout.fragment_weixin,null);
}
}ContactFragmentpackage
com.example.g160628_android10_viewpagerfragment_zuoye;
import
android.os.Bundle;
import
android.support.annotation.Nullable;
import
android.support.v4.app.Fragment;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.view.ViewGroup;
/**
*
Created
by
Administrator
on
2017/6/15.
*/
public
class
ContactFragment
extends
Fragment
{
@Nullable
@Override
public
View
onCreateView(LayoutInflater
inflater,
@Nullable
ViewGroup
container,
@Nullable
Bundle
savedInstanceState)
{
//返回對(duì)應(yīng)的fragment_contact
return
inflater.inflate(R.layout.fragment_contact,null);
}
}FindFragmentpackage
com.example.g160628_android10_viewpagerfragment_zuoye;
import
android.os.Bundle;
import
android.support.annotation.Nullable;
import
android.support.v4.app.Fragment;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.view.ViewGroup;
/**
*
Created
by
A
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 交通運(yùn)輸渣土運(yùn)輸協(xié)議
- 商鋪改造半包裝修合同模板
- 寵物運(yùn)輸合同模板-@-1
- 福建圍墻垂直綠化施工方案
- 主播轉(zhuǎn)讓合同范本
- 波紋管涵拆除施工方案
- 良心裝飾公司招商方案
- 可以變更勞動(dòng)合同范例
- 通風(fēng)與空調(diào)施工方案
- 魚(yú)塘招租競(jìng)標(biāo)方案
- 外科護(hù)理課程思政課程標(biāo)準(zhǔn)
- 船舶航行安全
- 道德經(jīng)全文完整版本
- 9.2溶解度(第1課時(shí)飽和溶液不飽和溶液)+教學(xué)設(shè)計(jì)-2024-2025學(xué)年九年級(jí)化學(xué)人教版(2024)下冊(cè)
- 2024年審計(jì)局公務(wù)員招錄事業(yè)單位招聘考試招錄139人完整版附答案【研優(yōu)卷】
- 濰坊市人民醫(yī)院招聘真題
- 銷(xiāo)售人員薪資提成及獎(jiǎng)勵(lì)制度
- 2017年江蘇南京中考滿(mǎn)分作文《無(wú)情歲月有味詩(shī)》5
- 2023年宏觀經(jīng)濟(jì)學(xué)考點(diǎn)難點(diǎn)
- 2024-2030年中國(guó)智慧水務(wù)行業(yè)應(yīng)用需求分析發(fā)展規(guī)劃研究報(bào)告
- 黑龍江申論真題2021年(鄉(xiāng)鎮(zhèn))
評(píng)論
0/150
提交評(píng)論