Android 中 TabHost与ViewPager结合实现首页导航效果

今天发的是TabHost结合ViewPager实现首页底部导航的效果,虽然说网上有很多这样的Demo,不过呢,我还是要把自己练习写的发出来,没错!就是这么任性;

先上效果图,如下:

代码里面有注释,就不过多解释了,说几点需要注意的问题

1:TabHost 、TabWidget、FrameLayout一定添加id这个属性,否则会报错

android:id=”@android:id/tabhost”

android:id=”@android:id/tabcontent”

android:id=”@android:id/tabs”

这个属性是固定的。

2:ViewPager的适配器要继承PagerAdapter,别整错咯;

布局文件xml:

<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.wgh.tabhostwithviewpager.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1.0"> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v4.view.ViewPager> </FrameLayout> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="0.0" android:visibility="gone" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#0ff0f0" /> <RadioGroup android:id="@+id/radioGroup" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/radioButton1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="5dp" android:layout_weight="1" android:background="@drawable/img_draw_money_fail" android:button="@null" android:paddingLeft="10dp" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="5dp" android:layout_weight="1" android:background="@drawable/img_draw_money_fail" android:button="@null" /> <RadioButton android:id="@+id/radioButton3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="5dp" android:layout_weight="1" android:background="@drawable/img_draw_money_fail" android:button="@null" /> <RadioButton android:id="@+id/radioButton4" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="5dp" android:layout_weight="1" android:background="@drawable/img_draw_money_fail" android:button="@null" /> </RadioGroup> </LinearLayout> </TabHost>

Activity:

package com.example.wgh.tabhostwithviewpager; import android.app.TabActivity; import android.os.Bundle; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TabHost; import java.util.ArrayList; public class MainActivity extends TabActivity { private TabHost tabHost = null; private ViewPager viewPager = null; private RadioGroup radioGroup = null; private ArrayList<View> list = null; private View view1 = null; private View view2 = null; private View view3 = null; private View view4 = null; private RadioButton radioButton1 = null; private RadioButton radioButton2 = null; private RadioButton radioButton3 = null; private RadioButton radioButton4 = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); //设置初始化默认选项 radioGroup.check(R.id.radioButton1); radioButton1.setBackgroundResource(R.drawable.img_draw_money_success); viewPager.setCurrentItem(0); tabHost.setCurrentTab(0); //getViewPager添加适配器 MyAdapter adapter = new MyAdapter(list); viewPager.setAdapter(adapter); /** * viewPager设置滑动监听,根据viewPager选中页的position,设置tabHost页卡选项的样式 */ viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { if (position == 0){ radioButton1.setBackgroundResource(R.drawable.img_draw_money_success); radioButton2.setBackgroundResource(R.drawable.img_draw_money_fail); radioButton3.setBackgroundResource(R.drawable.img_draw_money_fail); radioButton4.setBackgroundResource(R.drawable.img_draw_money_fail); }else if(position == 1){ radioButton1.setBackgroundResource(R.drawable.img_draw_money_fail); radioButton2.setBackgroundResource(R.drawable.img_draw_money_success); radioButton3.setBackgroundResource(R.drawable.img_draw_money_fail); radioButton4.setBackgroundResource(R.drawable.img_draw_money_fail); }else if(position == 2){ radioButton1.setBackgroundResource(R.drawable.img_draw_money_fail); radioButton2.setBackgroundResource(R.drawable.img_draw_money_fail); radioButton3.setBackgroundResource(R.drawable.img_draw_money_success); radioButton4.setBackgroundResource(R.drawable.img_draw_money_fail); }else if(position == 3){ radioButton1.setBackgroundResource(R.drawable.img_draw_money_fail); radioButton2.setBackgroundResource(R.drawable.img_draw_money_fail); radioButton3.setBackgroundResource(R.drawable.img_draw_money_fail); radioButton4.setBackgroundResource(R.drawable.img_draw_money_success); } } @Override public void onPageScrollStateChanged(int state) { } }); /** * 给radioGroup设置监听,以此来改变ViewPager的页面 */ radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, int checkedId) { switch (checkedId){ case R.id.radioButton1: viewPager.setCurrentItem(0); radioButton1.setBackgroundResource(R.drawable.img_draw_money_success); radioButton2.setBackgroundResource(R.drawable.img_draw_money_fail); radioButton3.setBackgroundResource(R.drawable.img_draw_money_fail); radioButton4.setBackgroundResource(R.drawable.img_draw_money_fail); break; case R.id.radioButton2: viewPager.setCurrentItem(1); radioButton1.setBackgroundResource(R.drawable.img_draw_money_fail); radioButton2.setBackgroundResource(R.drawable.img_draw_money_success); radioButton3.setBackgroundResource(R.drawable.img_draw_money_fail); radioButton4.setBackgroundResource(R.drawable.img_draw_money_fail); break; case R.id.radioButton3: viewPager.setCurrentItem(2); radioButton1.setBackgroundResource(R.drawable.img_draw_money_fail); radioButton2.setBackgroundResource(R.drawable.img_draw_money_fail); radioButton3.setBackgroundResource(R.drawable.img_draw_money_success); radioButton4.setBackgroundResource(R.drawable.img_draw_money_fail); break; case R.id.radioButton4: viewPager.setCurrentItem(3); radioButton1.setBackgroundResource(R.drawable.img_draw_money_fail); radioButton2.setBackgroundResource(R.drawable.img_draw_money_fail); radioButton3.setBackgroundResource(R.drawable.img_draw_money_fail); radioButton4.setBackgroundResource(R.drawable.img_draw_money_success); break; } } }); } /** * 初始化数据源 */ private void initData() { list = new ArrayList<View>(); list.add(view1); list.add(view2); list.add(view3); list.add(view4); } /** * 初始化控件 */ private void initView() { tabHost = getTabHost(); viewPager = (ViewPager) findViewById(R.id.viewPager); radioGroup = (RadioGroup) findViewById(R.id.radioGroup); radioButton1 = (RadioButton) findViewById(R.id.radioButton1); radioButton2 = (RadioButton) findViewById(R.id.radioButton2); radioButton3 = (RadioButton) findViewById(R.id.radioButton3); radioButton4 = (RadioButton) findViewById(R.id.radioButton4); /** * 将每页要展示的layout实例出来,添加到list里面,最后通过适配器return回来要展示的相应的layout */ LayoutInflater inflater = LayoutInflater.from(this); view1 = inflater.inflate(R.layout.layout_one,null); view2 = inflater.inflate(R.layout.layout_two,null); view3 = inflater.inflate(R.layout.layout_three,null); view4 = inflater.inflate(R.layout.layout_four,null); } }

ViewPager适配器MyAdapter:

public class MyAdapter extends PagerAdapter { private ArrayList<View> list = null; public MyAdapter(ArrayList<View> list) { this.list = list; } @Override public int getCount() { return list.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(list.get(position)); return list.get(position); } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(list.get(position)); } }

以上所述是小编给大家介绍的Android 中 TabHost与ViewPager结合实现首页导航效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

时间: 2024-08-02 19:34:08

Android 中 TabHost与ViewPager结合实现首页导航效果的相关文章

Android 中 TabHost与ViewPager结合实现首页导航效果_Android

今天发的是TabHost结合ViewPager实现首页底部导航的效果,虽然说网上有很多这样的Demo,不过呢,我还是要把自己练习写的发出来,没错!就是这么任性: 先上效果图,如下: 代码里面有注释,就不过多解释了,说几点需要注意的问题 1:TabHost .TabWidget.FrameLayout一定添加id这个属性,否则会报错 android:id="@android:id/tabhost" android:id="@android:id/tabcontent"

android中如何实现类似于微博个人首页的设计整个界面可以下拉

问题描述 android中如何实现类似于微博个人首页的设计整个界面可以下拉 android中如何实现类似于微博个人首页的设计整个界面可以下拉,但不是刷新数据 解决方案 SwipeRefreshLayout 解决方案二: 百度搜弹性scrollview 解决方案三: 官方的下拉控件不能拉动整个界面,gitHub上有很多自定义的下拉控件可以实现..不刷新数据的话,只需要在回调方法中不做任何数据更新操作就行: 当然,你也可以自定义下拉控件...主要思路是给listView 添加一个头部!!希望有用 解

Android使用TabLayou+fragment+viewpager实现滑动切换页面效果

TabLayou 主要实现的是标题头的 滑动 这个 控件 类似于 ScrollView XML中的布局 <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <android.support.design.widget.TabLayout a

android 中如何去掉listView自带的回弹效果

问题描述 android 中如何去掉listView自带的回弹效果 android 中做listView的下拉刷新和上拉加载更多!魅族,还有其它的产商的手机下啦与自带的回弹 效果怎么去掉,下啦涮新怪怪的 解决方案 listview有属性可以设置的:listView.setOverScrollMode(View.OVER_SCROLL_NEVER); 解决方案二: 在xml中,listView的一个属性 android:overScrollMode="never" 在代码中 mListV

Android中ListView + CheckBox实现单选、多选效果

还是先来看看是不是你想要的效果: 不废话,直接上代码,很简单,代码里都有注释 1 单选 public class SingleActivity extends AppCompatActivity { private ListView listView; private ArrayList<String> groups; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInsta

Android 中从屏幕左下角弹出Dialog动画效果的实现代码

MainActivity代码: import android.app.Dialog; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.Window; import androi

Android实现腾讯新闻的新闻类别导航效果

效果图如下所示: 1.在Adapter中加入如下代码 <pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'OCR A Std';font-size:10.2pt;"><span style="color:#cc7832;">private int </span><span style="color:#9876aa;">cli

Android 利用ViewPager+GridView实现首页导航栏布局分页效果_Android

最近我尝试使用ViewPager+GridView实现的,看起来一切正常,废话不多说,具体代码如下: 如图是效果图   首先分析下思路 1.首先是怎么布局:整体是一个ViewPager将GridView作为一个View添加到ViewPager的adapter中,下方是圆点 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas

Android 利用ViewPager+GridView实现首页导航栏布局分页效果

最近我尝试使用ViewPager+GridView实现的,看起来一切正常,废话不多说,具体代码如下: 如图是效果图 首先分析下思路 1.首先是怎么布局:整体是一个ViewPager将GridView作为一个View添加到ViewPager的adapter中,下方是圆点 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.a