当我们使用activity加fragment的时候,每个界面都要建立一个fragment,每个fragment里面都要重写onCreate(),onCreateView(),onActivityCreated(),方法,我们新建一个基类BaseFragment来重写这些方法
BaseFragment需要定义一个抽象方法initView(),用来初始化View对象,需要子类去实现,我们父类把LayoutInflater对象传进去
BaseFragment需要定义一个抽象方法initData(),用来初始化数据,需要子类去实现
package com.qingguow.news.fragment; import android.content.Context; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * 基础类 * @author taoshihan * */ public abstract class BaseFragment extends Fragment { public Context context; @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { // TODO Auto-generated method stub super.onActivityCreated(savedInstanceState); initData(savedInstanceState); } @Override public void onCreate(@Nullable Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); context=getActivity(); } @Override @Nullable public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view=initView(inflater); return view; } /** * 初始化View对象 * @param inflater * @return */ public abstract View initView(LayoutInflater inflater); /** * 初始化数据 * @param Bundle * @return */ public abstract void initData(Bundle savedInstanceState); }
主Fragment里实现Banner轮播
在fragment_index.xml布局文件中添加节点<android.support.v4.view.ViewPager />
获取ViewPager对象,通过findViewById()方法
调用ViewPager对象的setAdapter()方法,设置适配器,参数:PagerAdapter对象
使用匿名内部类的方式实现抽象方法,getCount(),isViewFromObject(),instantiateItem(),destroyItem()
重写getCount()方法,返回轮播的个数
重写isViewFromObject()方法,返回布尔值,
重写instantiateItem()方法,将当前view对象添加到ViewGroup对象,返回当前对象
重写destroyItem()方法,从当前container中删除指定位置(position)的View
package com.qingguow.news.fragment; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.qingguow.news.R; /** * 首页 * @author taoshihan * */ public class IndexFragment extends BaseFragment { private List<View> banners; @Override public 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=new TextView(context); tv2.setText("测试2"); banners.add(tv2); ViewPager vp=(ViewPager) view.findViewById(R.id.vp_banner); vp.setAdapter(new PagerAdapter(){ @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(banners.get(position)); } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(banners.get(position)); return banners.get(position); } @Override public int getCount() { return banners.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0==arg1; } }); return view; } @Override public void initData(Bundle savedInstanceState) { // TODO Auto-generated method stub } }
时间: 2024-11-10 08:12:12