码迷,mamicode.com
首页 > 其他好文 > 详细

viewpager+fragment实现滑动页面

时间:2015-01-20 20:01:19      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

1、适配器

package com.example.adapter;

import java.util.List;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.util.Log;
import android.view.ViewGroup;

import com.example.bean.Will;
import com.fragment.WillFragment;

/**
 * @author JL
 * FragmentPagerAdapter该类内的每一个生成的 Fragment 都将保存在内存之中
 * FragmentStatePagerAdapter的实现将只保留当前页面,当页面离开视线后,就会被消除,释放其资源;
 * 而在页面需要显示时,生成新的页面(就像 ListView的实现一样)。这么实现的好处就是当拥有大量的页面时,不必在内存中占用大量的内存。
 */
public class WillPagerAdapter extends FragmentStatePagerAdapter {

    private List<Will> mList;
public List<Will> getmList() {
        return mList;
    }

    public void setmList(List<Will> mList) {
        this.mList = mList;
    }

    public WillPagerAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    public WillPagerAdapter(FragmentManager fm, List<Will> list) {
        this(fm);
        // TODO Auto-generated constructor stub
        mList = list;
    }

    @Override
    public Fragment getItem(int position) {
        // TODO Auto-generated method stub
        WillFragment fragment = WillFragment.newInstance(mList.get(position));
        return fragment;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        // TODO Auto-generated method stub
        Log.i("instantiateItem", "position=" + position);
        // 得到缓存的fragment
        WillFragment fragment = (WillFragment) super.instantiateItem(container,
                position);
        return fragment;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mList.size();
    }

    public IntentListenr getIntentListenr() {
        return intentListenr;
    }
}

2、WillFragment

import android.support.v4.app.Fragment;
public class WillFragment extends Fragment implements OnClickListener {

    private Will mWill;private TextView mTextName;public static WillFragment newInstance(Will will) {
        WillFragment fragment = new WillFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("will", will);
        fragment.setArguments(bundle);
        return fragment;
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle args = getArguments();
        if (args != null) {
            mWill = (Will) args.get("will");
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        Log.i("WILLACTIVITY", "requestCode=" + requestCode + "resultCode="
                + resultCode);

        switch (resultCode) { // resultCode为回传的标记,我在B中回传的是RESULT_OK

        case 10:break;
        default:
            break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}

WillActivity

private WillPagerAdapter mAdapter;
....
mViewPager = (ViewPager) findViewById(R.id.will_activity_pager);
mAdapter = new WillPagerAdapter(getSupportFragmentManager(), mWills);
mViewPager.setAdapter(mAdapter);
Log.i("WillActivity", "" + position);
mViewPager.setCurrentItem(position);

 

 

Done!

viewpager+fragment实现滑动页面

标签:

原文地址:http://www.cnblogs.com/xingyyy/p/4236712.html

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