android 开发框架 怎么使用,Android快速开发框架dyh详解(二)---控件层的使用

1、控件组合

1.1、ViewPager + Fragment,

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

1.1.1、先看看效果图

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

0818b9ca8b590ca3270a3433284dd417.png

1.1.2、xml布局

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

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

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:id="@+id/ti"

/>

android:id="@+id/vp"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="@android:color/white" />

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里只能包含一个RadioButton

ti.create(tabName, R.layout.tab_radio);

//生成一个Fragment集合

List 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。0818b9ca8b590ca3270a3433284dd417.png

1.2.2、xml布局

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

android:id="@+id/main_layout"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id="@+id/main_content"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1"

android:background="@android:color/black" />

android:id="@+id/main_radio_group"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="@android:color/white"

android:orientation="horizontal" >

android:id="@+id/main_rb_show"

style="@style/main_radio"

android:drawableTop="@drawable/show_selector"

android:text="买家秀" />

android:id="@+id/main_rb_home"

style="@style/main_radio"

android:drawableTop="@drawable/home_selector"

android:text="首页" />

android:id="@+id/main_rb_me"

style="@style/main_radio"

android:drawableTop="@drawable/me_selector"

android:text="我" />

1.1.3、代码实现

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

/**

* 获取Fragment容器的控件id

* @decs 用来放Fragment的控件

*/

@Override

public int getFragmentViewId() {

return R.id.main_content;

}

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

/**

* 依次加入Fragment

* @desc add(RadioButton的id,fragment的class)

*/

@Override

protected void initFragment() {

//需要传入RadioButton的id和对应的Fragment

add(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.2、FixedHeightGridView(com.dyh.view.FixedHeightGridView)

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

2.3、FixedHeightListView(com.dyh.view.FixedHeightListView)

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

2.4、TabIndicator(com.dyh.view.TabIndicator)

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

//tabName是一个String数组,即标题文字

//R.layout.tab_radio是RadioButton的布局文件,标题的样式就是它决定,并且xml里只能包含一个RadioButton

ti.create(tabName, R.layout.tab_radio);

1、控件组合

1.1、ViewPager + Fragment,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值