• viewpager+fragment实现滑动页面


    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.example.fragment.WillFragment;
    
    public class WillPagerAdapter extends FragmentStatePagerAdapter {
    
        private List<Will> 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;
        }
    
        /*
         * 生成新的 Fragment 对象。 .instantiateItem() 在大多数情况下,都将调用 getItem() 来生成新的对象
         */
        @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();
        }
    
        /**
         * 要求getItemPosition、FragmentStatePagerAdapter
         */
        public void remove(int position) {
            mList.remove(position);
            notifyDataSetChanged();
        }
    
        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }
    
    }

    2、WillFragment

        public static WillFragment newInstance(Will will) {
            Log.i("WillFragment", "newInstance=" + 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);
            mWill = getArguments() != null ? (Will) getArguments().getSerializable(
                    "will") : mWill;
    
            Log.i(tag, "onCreate=" + mWill);
    
            likeFlag = mWill.isLike();
            viewFlag = likeFlag;
    
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            // 如果提供root时,返回值其实就是这个root,这个方法就是把xml解析成view之后挂载这个root下。
            // 如果root传null(不提供root),返回值是View,它就是xml布局里面的根节点
            View view = inflater.inflate(R.layout.fragment_will, container, false);
    return view
    }
        public static WillFragment newInstance(Will will) {
            Log.i("WillFragment", "newInstance=" + 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);
            mWill = getArguments() != null ? (Will) getArguments().getSerializable(
                    "will") : mWill;
    
            Log.i(tag, "onCreate=" + mWill);
    
            likeFlag = mWill.isLike();
            viewFlag = likeFlag;
    
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            // 如果提供root时,返回值其实就是这个root,这个方法就是把xml解析成view之后挂载这个root下。
            // 如果root传null(不提供root),返回值是View,它就是xml布局里面的根节点
            View view = inflater.inflate(R.layout.fragment_will, container, false);
            return view
    }

    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!

  • 相关阅读:
    vue-router重写push方法,解决相同路径跳转报错
    Vue中的权限管理怎么做
    linux启动过程中建立临时页表
    用redis当作LRU缓存
    用IPV6隧道连接IPV4孤岛
    redis协议规范
    nginx的脚本引擎(二)rewrite
    nginx的脚本引擎(一)
    nginx的变量系统
    Singleton
  • 原文地址:https://www.cnblogs.com/xingyyy/p/4236712.html
Copyright © 2020-2023  润新知