码迷,mamicode.com
首页 > 移动开发 > 详细

[android] 新闻客户端主界面部分

时间:2016-04-28 00:31:45      阅读:318      评论:0      收藏:0      [点我收藏+]

标签:

当我们使用activityfragment的时候,每个界面都要建立一个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
        
    }
}

 

[android] 新闻客户端主界面部分

标签:

原文地址:http://www.cnblogs.com/taoshihan/p/5440959.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!