當我們使用activity加fragment的時候,每個界面都要建立一個fragment,每個fragment裡面都要重寫onCreate(),onCreateView(),onActivityCreated(),方法,我們新建一個基類BaseFragment來重寫這些方法 BaseFragment ...
當我們使用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 } }