详解地址:http://www.dyhdyh.com

最新版下载地址:http://www.dyhdyh.com/download

交流QQ群:146261923


转载请注明出处:http://blog.csdn.net/aa464971/article/details/43039085


1、控件组合

1.1、ViewPager + Fragment

这两个控件的组合在项目里也是比较常用的,效果同ViewPager,但由于是用Fragment所以可以使代码操作起来更方便;


1.1.1、先看看效果图

左右滑动切换页面,标题也会随着ViewPager切换而改变标题的选中状态。

1.1.2、xml布局

这里用了dyh里的一个自定义控件,TabIndicator,也就是viewpager标题下面那个下划线,具有位移动画,标题文字也是属于该控件。

如果不需要标题,布局只需要写一个ViewPager就好。

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <com.dyh.view.TabIndicator         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:id="@+id/ti"        />    <android.support.v4.view.ViewPager        android:id="@+id/vp"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:background="@android:color/white" /></LinearLayout>

1.1.3、代码实现

首先我们需要一个Adapter来给ViewPager,而这个Adapter,已经自带在框架里面了,可以直接使用DyhBaseFragmentAdapter(该类继承于FragmentPagerAdapter)。如果需要自定义Adapter,可以继承DyhBaseFragmentAdapter进行扩展,也可以继承FragmentPagerAdapter来重写。

框架里还有一个DyhFragmentPagerActivity,这个Activity自带了ViewPager标题切换的效果,如果需要自定义,可选择继承其它Activity。

在oncreate调用以下方法,就可以完成ViewPager+Fragment的效果


//tabName是一个String数组,即标题文字//R.layout.tab_radio是RadioButton的布局文件,标题的样式就是它决定,并且xml里只能包含一个RadioButtonti.create(tabName, R.layout.tab_radio);//生成一个Fragment集合List<Fragment> fragments = FragmentFactory.createList(AllFragment_.class,GraphicFragment_.class,SoundFragment_.class,VideoFragment_.class);//创建一个DyhBaseFragmentAdapter,将FragmentManager和Fragment集合传入FragmentPagerAdapter adapter=new DyhBaseFragmentAdapter(getSupportFragmentManager(), fragments);vp.setAdapter(adapter);//如果使用自带的标题切换效果,需继承于DyhFragmentPagerActivity//下面两个监听,就是标题切换vp.setOnPageChangeListener(new OnPageChangeListener(ti));ti.setOnCheckedChangeListener(new OnRadioChangeListener(vp));

1.2、Fragment + RadioGroup

这两个控件的组合几乎所有的项目都会用到,一般用于应用的首页,RadioGroup作导航,Fragment作内容容器;

1.2.1、效果图

最下面的三个按钮就是导航栏RadioGroup,内容页面是Fragment,点击按钮会切换对应的Fragment。


1.2.2、xml布局

首先需要一个FrameLayout来作为Fragment的容器,也就是说,所有的Fragment都是放在这个FrameLayout上面;下面再放一组RadioGroup,里面的RadioButton就是单个选项卡的按钮。

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/main_layout"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <FrameLayout        android:id="@+id/main_content"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:layout_weight="1"        android:background="@android:color/black" />    <RadioGroup        android:id="@+id/main_radio_group"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:background="@android:color/white"        android:orientation="horizontal" >        <RadioButton            android:id="@+id/main_rb_show"            style="@style/main_radio"            android:drawableTop="@drawable/show_selector"            android:text="买家秀" />        <RadioButton            android:id="@+id/main_rb_home"            style="@style/main_radio"            android:drawableTop="@drawable/home_selector"            android:text="首页" />        <RadioButton            android:id="@+id/main_rb_me"            style="@style/main_radio"            android:drawableTop="@drawable/me_selector"            android:text="我" />    </RadioGroup></LinearLayout>

1.1.3、代码实现

(1)需要继承于DyhFragmentRadioActivity,然后会要求重写getFragmentViewId()方法,这里返回FrameLayout的id就好;

/** * 获取Fragment容器的控件id * @decs 用来放Fragment的控件 */@Overridepublic int getFragmentViewId() {return R.id.main_content;}

(2)还需要重写initFragment()方法,在里面添加你的Fragment即可

/** * 依次加入Fragment * @desc add(RadioButton的id,fragment的class) */@Overrideprotected void initFragment() {//需要传入RadioButton的id和对应的Fragmentadd(R.id.main_rb_show, ShowFragment.class);add(R.id.main_rb_home, HomeFragment.class);add(R.id.main_rb_me, MeFragment.class);}

(3)在OnCreate里面设置RadioGroup的监听,再设置默认选择的按钮,就大功告成了!

//如果需要传入自定义的OnCheckedChangeListener,需在方法里调用addFragmentStack(fragments.get(checkedId));rg.setOnCheckedChangeListener(this);//默认选中索引为1的按钮setRadioTrue(rg,1);



2、自定义控件

2.1、BaseWebView(com.dyh.view.BaseWebView)

使用方式同WebView,默认设置了支持JavaScript、在应用内打开网页,所以无需再设置。


2.2FixedHeightGridViewcom.dyh.view.FixedHeightGridView

使用方式同GridView,固定高度的Gridview,解决了ScrollView与GridView共存的滑动问题。


2.3、FixedHeightListViewcom.dyh.view.FixedHeightListView

使用方式同GridView,固定高度的Gridview,解决了ScrollView与GridView共存的滑动问题。


2.4、TabIndicatorcom.dyh.view.TabIndicator

在布局里添加一个TabIndicator,再在OnCreate调用创建的方法,可参考ViewPager + Fragment

//tabName是一个String数组,即标题文字//R.layout.tab_radio是RadioButton的布局文件,标题的样式就是它决定,并且xml里只能包含一个RadioButtonti.create(tabName, R.layout.tab_radio);



1、控件组合

1.1、ViewPager + Fragment,

更多相关文章

  1. 第一行代码Android笔记精华版
  2. android 基础控件(EditView、SeekBar等)的属性及使用方法
  3. Android中RelativeLayout的使用-----------转载下----以免忘记.
  4. Android(安卓)Studio用法之ListView(图文并茂的listview界面)
  5. Android实现淘宝倒计时功能
  6. Android学习笔记(三)UI布局
  7. Android(安卓)循环滚动控件ViewFlipper,可实现跑马灯或轮播图效果
  8. android launcher总体分析 .
  9. 一种巧妙获取Android状态栏高度的办法

随机推荐

  1. android蓝牙BLE(三) —— 广播
  2. Android直播开发之旅(8):Android硬编解码
  3. :Android模拟器的基本操作
  4. 简述Android触摸屏手势识别
  5. Android输入系统解析及Native层模拟按键
  6. android Layout XML属性
  7. 解析 ViewTreeObserver 源码,体会观察者模
  8. Android中读取电话本Contacts联系人的所
  9. Android(安卓)内功心法(1.3)——android
  10. Android单张图片查看、单指移动、双指缩