package com.qingguow.news.fragment;import androID.content.Context; androID.os.Bundle; androID.support.annotation.Nullable; androID.support.v4.app.Fragment; androID.vIEw.LayoutInflater; androID.vIEw.VIEw; androID.vIEw.VIEwGroup;/** * 基础类 * @author taoshihan * */public abstract class BaseFragment extends Fragment { public Context context; @OverrIDe voID onActivityCreated(@Nullable Bundle savedInstanceState) { // Todo auto-generated method stub super.onActivityCreated(savedInstanceState); initData(savedInstanceState); } @OverrIDe onCreate(@Nullable Bundle savedInstanceState) { .onCreate(savedInstanceState); context=getActivity(); } @OverrIDe @Nullable VIEw onCreateVIEw(LayoutInflater inflater,@Nullable VIEwGroup container,@Nullable Bundle savedInstanceState) { VIEw vIEw=initVIEw(inflater); return vIEw; } * 初始化VIEw对象 * @param inflater * @return */ abstract VIEw initVIEw(LayoutInflater inflater); * 初始化数据 * Bundle * initData(Bundle savedInstanceState);}@H_419_0@ @H_419_0@主Fragment里实现Banner轮播@H_419_0@在fragment_index.xml布局文件中添加节点<androID.support.v4.vIEw.VIEwPager />@H_419_0@获取VIEwPager对象,通过findVIEwByID()方法@H_419_0@调用VIEwPager对象的setAdapter()方法,设置适配器,参数:PagerAdapter对象@H_419_0@使用匿名内部类的方式实现抽象方法,getCount(),isVIEwFromObject(),instantiateItem(),destroyItem()@H_419_0@重写getCount()方法,返回轮播的个数@H_419_0@重写isVIEwFromObject()方法,返回布尔值,@H_419_0@重写instantiateItem()方法,将当前vIEw对象添加到VIEwGroup对象,返回当前对象@H_419_0@重写destroyItem()方法,从当前container中删除指定位置(position)的VIEw
java.util.ArrayList; java.util.List; androID.support.v4.vIEw.PagerAdapter; androID.support.v4.vIEw.VIEwPager; androID.Widget.TextVIEw; com.qingguow.news.R; * 首页 * class IndexFragment BaseFragment { private List<VIEw> banners; @OverrIDe VIEw initVIEw(LayoutInflater inflater) { VIEw vIEw=inflater.inflate(R.layout.fragment_index,null); banner部分 banners=new ArrayList<>(); TextVIEw tv=new TextVIEw(context); tv.setText("测试"); banners.add(tv); TextVIEw tv2= TextVIEw(context); tv2.setText("测试2"); banners.add(tv2); VIEwPager vp=(VIEwPager) vIEw.findVIEwByID(R.ID.vp_banner); vp.setAdapter( PagerAdapter(){ @OverrIDe voID destroyItem(VIEwGroup container,1)">int position,Object object) { container.removeVIEw(banners.get(position)); } @OverrIDe public Object instantiateItem(VIEwGroup container,1)"> position) { container.addVIEw(banners.get(position)); banners.get(position); } @OverrIDe getCount() { banners.size(); } @OverrIDe boolean isVIEwFromObject(VIEw arg0,Object arg1) { return arg0==arg1; } }); vIEw; } @OverrIDe initData(Bundle savedInstanceState) { Todo auto-generated method stub }}@H_419_0@ 总结
以上是内存溢出为你收集整理的[android] 新闻客户端主界面部分全部内容,希望文章能够帮你解决[android] 新闻客户端主界面部分所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)