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!
时间: 2024-11-08 06:00:28