版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】使用ViewPager+Fragment实现选项卡切换效果
参考链接:/p/7bc9a1ff137e
/code/info/245.html
/articles/FfeiumV本人参考了三篇文章来写这个页面,但是运行这上面给出的源码,发现每个fragment的flag并不能实现切换。要实现的效果如下:在activity_main.xml中设置布局。xml内容如下:<LinearLayout
xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.fengzhengapp.MainActivity"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:orientation="horizontal"
>
<TextView
android:id="@+id/tv_hot"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:background="#ffEAEAEA"
android:gravity="center"
android:text="@string/tab_hot"
android:textSize="18sp"
/>
<TextView
android:id="@+id/tv_news"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:background="#ffEAEAEA"
android:gravity="center"
android:text="@string/tab_news"
android:textSize="18sp"
/>
<TextView
android:id="@+id/tv_fav"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:background="#ffEAEAEA"
android:gravity="center"
android:text="@string/tab_favorite"
android:textSize="18sp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="3dp"
android:orientation="horizontal">
<View
android:id="@+id/activity_order_flag_all"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_marginLeft="36dp"
android:layout_marginRight="36dp"
android:background="@color/app_style_red"
android:gravity="center"/>
<View
android:id="@+id/activity_order_flag_uncomplete"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_marginLeft="36dp"
android:layout_marginRight="36dp"
android:background="@color/app_style_red"
android:gravity="center"
android:visibility="invisible"/>
<View
android:id="@+id/activity_order_flag_complete"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_marginLeft="36dp"
android:layout_marginRight="36dp"
android:background="@color/app_style_red"
android:gravity="center"
android:visibility="invisible"/>
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/myViewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:flipInterval="30"
>
</android.support.v4.view.ViewPager>
</LinearLayout>接下来,增加3个Fragment布局页,分别在里面填充简单的内容第一个
:<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/txtHot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="this
is
the
hot
tab"
>
</TextView>
</LinearLayout>第二个
:<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/txtNews"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:text="frag_1"
android:textSize="30dp"
android:textStyle="bold"
android:textColor="#000000"
/>
</LinearLayout>第三个
:<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/txtFav"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="this
is
the
Fav
tab"
>
</TextView>
</LinearLayout>以上3个Fragment的布局文件已创建完毕,每个文件中只显示简单的文本内容,用做演示。加载3个Fragment到Activity中。首先实现3个Fragment对应的后台类import
android.os.Bundle;
import
android.support.v4.app.Fragment;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.view.ViewGroup;
import
erfacepractice.R;
public
class
FragmentHot
extends
Fragment
{
@Override
public
View
onCreateView(LayoutInflater
inflater,
ViewGroup
container,Bundle
savedInstanceState)
{
View
view
=
inflater.inflate(R.layout.fragmenthot,
container,
false);
return
view;
}
}news布局页对应的类:import
android.os.Bundle;
import
android.support.v4.app.Fragment;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.view.ViewGroup;
import
erfacepractice.R;
public
class
FragmentNews
extends
Fragment
{
@Override
public
View
onCreateView(LayoutInflater
inflater,
ViewGroup
container,Bundle
savedInstanceState)
{
View
view
=
inflater.inflate(R.layout.fragmentnews,
container,
false);
return
view;
}
}收藏布局页对应的类:import
android.os.Bundle;
import
android.support.v4.app.Fragment;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.view.ViewGroup;
import
erfacepractice.R;
public
class
FragmentFavorite
extends
Fragment
{
@Override
public
View
onCreateView(LayoutInflater
inflater,
ViewGroup
container,Bundle
savedInstanceState)
{
View
view
=
inflater.inflate(R.layout.fragmentfav,
container,
false);
return
view;
}
}之后再activity中初始化这3个Fragment注意要点:MainActivity继承自FragmentActivity要实现一个FragmentPagerAdapter,内容如下:import
android.support.v4.app.Fragment;
import
android.support.v4.app.FragmentManager;
import
android.support.v4.app.FragmentPagerAdapter;
import
java.util.ArrayList;
public
class
MyFragmentAdapter
extends
FragmentPagerAdapter
{
ArrayList<Fragment>
list;
public
MyFragmentAdapter(FragmentManager
fm,ArrayList<Fragment>
list){
super(fm);
this.list
=
list;
}
@Override
public
Fragment
getItem(int
arg0)
{
return
list.get(arg0);
}
@Override
public
int
getCount()
{
return
list.size();
}
}然后在MainActivity中实现切换和动画效果,代码如下:import
android.os.Bundle;
import
android.support.v4.app.Fragment;
import
android.support.v4.app.FragmentActivity;
import
android.support.v4.view.ViewPager;
import
android.view.MenuItem;
import
android.view.View;
import
android.widget.TextView;
import
java.util.ArrayList;
import
adapter.MyFragmentAdapter;
import
fragment.FragmentFavorite;
import
fragment.FragmentHot;
import
fragment.FragmentNews;
public
class
MainActivity
extends
FragmentActivity
implements
View.OnClickListener{
private
ViewPager
mViewPager;
private
ArrayList
fragments;
private
TextView
view1,
view2,
view3;
private
int
mCurrentOption
=
0;
private
View
flag0,
flag1,
flag2;
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViewPager();
initView();
}
@Override
public
boolean
onOptionsItemSelected(MenuItem
item)
{
int
id
=
item.getItemId();
if
(id
==
R.id.action_settings)
{
return
true;
}
return
super.onOptionsItemSelected(item);
}
private
void
initView(){
view1
=
(TextView)
findViewById(R.id.tv_hot);
view2
=
(TextView)
findViewById(R.id.tv_news);
view3
=
(TextView)
findViewById(R.id.tv_fav);
flag0
=
findViewById(R.id.activity_order_flag_all);
flag1
=
findViewById(R.id.activity_order_flag_uncomplete);
flag2
=
findViewById(R.id.activity_order_flag_complete);
view1.setOnClickListener(this);
view2.setOnClickListener(this);
view3.setOnClickListener(this);
}
@Override
public
void
onClick(View
v)
{
switch
(v.getId()){
case
R.id.tv_hot:
if(mCurrentOption
==
0)
return;
mCurrentOption
=
0;
flag0.setVisibility(View.VISIBLE);
flag1.setVisibility(View.INVISIBLE);
flag2.setVisibility(View.INVISIBLE);
mViewPager.setCurrentItem(mCurrentOption);
break;
case
R.id.tv_news:
if(mCurrentOption
==
1)
return;
mCurrentOption
=
1;
flag0.setVisibility(View.INVISIBLE);
flag1.setVisibility(View.VISIBLE);
flag2.setVisibility(View.INVISIBLE);
mViewPager.setCurrentItem(mCurrentOption);
break;
case
R.id.tv_fav:
if(mCurrentOption
==
2)
return;
mCurrentOption
=
2;
flag0.setVisibility(View.INVISIBLE);
flag1.setVisibility(View.INVISIBLE);
flag2.setVisibility(View.VISIBLE);
mViewPager.setCurrentItem(mCurrentOption);
break;
default:
return;
}
}
private
void
initViewPager(){
mViewPager
=
(ViewPager)findViewById(R.id.myViewPager);
fragments
=
new
ArrayList<Fragment>();
Fragment
fragmentHot
=
new
FragmentHot();
Fragment
fragmentNews
=
new
FragmentNews();
Fragment
fragmentFav
=
new
FragmentFavorite();
fragments.add(fragmentHot);
fragments.add(fragmentNews);
fragments.add(fragmentFav);
mViewPager.setAdapter(new
MyFragmentAdapter(getSupportFragmentManager(),
fragments));
mViewPager.setCurrentItem(0);
mViewPager.setOnPageChangeListener(new
ViewPager.OnPageChangeListener()
{
@Override
public
void
onPageScrolled(int
i,
float
v,
int
i2)
{
}
@Override
public
void
onPageSelected(int
i)
{
switch
(i){
case
0:
flag0.setVisibility(View.VISIBLE);
flag1.setVisibility(View.INVISIBLE);
flag2.setVisibility(View.INVISIBLE);
break;
case
1:
flag0.setVisibility(View.INVISIBLE);
flag1.setVisibility(View.VISIBLE);
flag2.setVisibility(View.INVISIBLE);
break;
case
2:
flag0.setVisibility(View.INVISIBLE);
flag1.setVisibility(View.INVISIBLE);
flag2.setVisibility(View.VISIBLE);
break;
default:
return;
}
}
@Override
public
void
onPageScrollStateChanged(int
i)
{
}
});
}
}package
com.ganinfo.collect.activity;
import
java.util.ArrayList;
import
com.ganinfo.collect.R;
import
com.ganinfo.collect.fragment.GzMyFragment;
import
com.ganinfo.collect.fragment.GzTaskFragment;
import
com.ganinfo.collect.utils.GzLog;
import
com.ganinfo.collect.utils.GzNetwork;
import
com.ganinfo.collect.widget.MyFragmentAdapter;
import
android.annotation.SuppressLint;
import
android.app.AlertDialog;
import
android.os.Bundle;
import
android.support.v4.view.ViewPager;
import
android.view.MenuItem;
import
android.view.View;
import
android.widget.ImageView;
import
android.widget.TextView;
@SuppressLint("ResourceAsColor")
public
class
GzMainActivity
extends
GzBaseActivity
{
private
String
TAG
=
"GzMainActivity";
private
long
lastTime
=
0;
//再按一次退出的时间标志
private
ArrayList<android.support.v4.app.Fragment>
fragments;
private
int
mCurrentOption
=
0;
private
ViewPager
mViewPager;
private
ImageView
mTaskIV,mTaskMyIV;
private
TextView
mTaskTV,mTaskMyTV;
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GzLog.d(TAG,
"onCreate");
initview();
initViewPager();
CheckNetwork();
}
private
void
initview()
{
mTaskIV
=
(ImageView)
findViewById(R.id.main_iv_shop);
mTaskMyIV
=
(ImageView)
findViewById(R.id.main_iv_my);
mTaskTV
=
(TextView)
findViewById(R.id.main_tv_shop);
mTaskMyTV
=
(TextView)
findViewById(R.id.main_tv_my);
}
@Override
public
boolean
onOptionsItemSelected(MenuItem
item)
{
int
id
=
item.getItemId();
if
(id
==
R.id.action_settings)
{
return
true;
}
return
super.onOptionsItemSelected(item);
}
private
void
CheckNetwork()
{
if(!GzNetwork.isNetworkAvailable()){
new
AlertDialog.Builder(this).
setMessage(R.work_not_avail).
setPositiveButton(R.string.ok,
null).
create().show();
}
}
private
void
initViewPager(){
mViewPager
=
(ViewPager)findViewById(R.id.main_viewpager);
fragments
=
new
ArrayList<android.support.v4.app.Fragment>();
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年及未来5年市场数据中国流通股质押贷款业务行业市场全景评估及投资战略数据分析研究报告
- 政策与法律法规精讲
- 2026年及未来5年市场数据中国休闲餐饮行业运行态势及市场发展潜力预测报告
- 2026年山西农业大学《大学计算机基础》期末试卷及答案
- 智慧农业:技术赋能与未来发展
- 2025 高中信息技术数据与计算之数据可视化的靶心图设计课件
- 2026年汽车行业零碳工厂建设指南与脱碳路径
- 2026年深海光缆作业机器人海底电缆施工关键技术应用
- 2026年漂浮式风电从示范向商业化应用转型指南
- 2026年孤儿电站问题应对企业倒闭后电站运维责任机制
- 招商公司运营薪酬制度
- 2025届贵州省高三学业水平选择性考试适应性测试生物试题(解析版)
- 2026年苏州工业职业技术学院高职单招职业适应性测试备考题库含答案解析
- 英语教学反思案例及改进策略
- 炎德·英才大联考湖南师大附中2026届高三月考试卷(五)英语试题(含答案详解)
- 2026年江西生物科技职业学院单招职业技能测试题库含答案详解
- 2018沪G504 钢筋混凝土锚杆静压桩和钢管锚杆静压桩
- 国家事业单位招聘2024中国农业科学院农业环境与可持续发展研究所招聘笔试历年参考题库典型考点附带答案详解(3卷合一)
- 2025年大理州州级机关统一公开遴选公务员参考试题(32人)附答案解析
- 2025+RCOG指南:妊娠甲状腺疾病管理解读课件
- 三国演义三英战吕布课件
评论
0/150
提交评论