高仿微信主界面:ViewPage+Fragment 不预加载Fragment 也不会销毁Fragment

微信支持下面四个Tab滑动,之前做的demo,遇到两个问题,1:Fragment会预加载,2:创建过的Fragment,来回滑动,会销毁重新创建。今天我这个demo,就要解决这两个问题。第一个问题需要导入一个新的V4包,最后我会提供,ViewPage要设置 mViewPager .setOffscreenPageLimit(0);这样的就能解决预加载的问题。第二个问题:我贴上代码:

package com.example.fragmentviewpage;

import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements OnClickListener
{
	private ViewPager mViewPager;
	private FragmentStatePagerAdapter mAdapter;//没用到
	private List<Fragment> mFragments;

	private LinearLayout mTabWeixin;
	private LinearLayout mTabContact;
	private LinearLayout mTabFind;
	private LinearLayout mTabSettings;

	private ImageButton mImgWeixin;
	private ImageButton mImgContact;
	private ImageButton mImgFind;
	private ImageButton mImgSettings;
	
	private TextView mWeixintxt;
	private TextView mContacttxt;
	private TextView mFindtxt;
	private TextView mSettingtxt;
	Fragment mTab01 ;
	Fragment mTab02 ;
	Fragment mTab03;
	Fragment mTab04 ;
	FragmentTransaction transaction ;
	private PagerAdapter myPageAdapter;
	
	FragmentManager fm ;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);

		initView();
		initEvent();
		
		setSelect(0);
	}

	private void initEvent()
	{
		mTabWeixin.setOnClickListener(this);
		mTabContact.setOnClickListener(this);
		mTabFind.setOnClickListener(this);
		mTabSettings.setOnClickListener(this);
	}

	private void initView()
	{
		mViewPager = (ViewPager) findViewById(R.id.id_viewpager);

		mTabWeixin = (LinearLayout) findViewById(R.id.id_tab_weixin);
		mTabContact = (LinearLayout) findViewById(R.id.id_tab_frd);
		mTabFind = (LinearLayout) findViewById(R.id.id_tab_address);
		mTabSettings = (LinearLayout) findViewById(R.id.id_tab_settings);

		mImgWeixin = (ImageButton) findViewById(R.id.id_tab_weixin_img);
		mImgContact = (ImageButton) findViewById(R.id.id_tab_frd_img);
		mImgFind = (ImageButton) findViewById(R.id.id_tab_address_img);
		mImgSettings = (ImageButton) findViewById(R.id.id_tab_settings_img);
		
		mWeixintxt = (TextView)findViewById(R.id.id_tab_weixin_txt);
		mContacttxt = (TextView) findViewById(R.id.id_tab_frd_txt);
		mFindtxt = (TextView) findViewById(R.id.id_tab_address_txt);
		mSettingtxt = (TextView) findViewById(R.id.id_tab_settings_txt);
		 fm = getSupportFragmentManager();
		 transaction = fm.beginTransaction();
		mFragments = new ArrayList<Fragment>();
		mTab01 = new MessageFragment();
		 mTab02 = new ContactFragment();
		 mTab03 = new FindFragment();
		 mTab04 = new SettingFragment();
		mFragments.add(mTab01);
		mFragments.add(mTab02);
		mFragments.add(mTab03);
		mFragments.add(mTab04);
	

		mAdapter = new FragmentStatePagerAdapter(getSupportFragmentManager())
		{

			@Override
			public int getCount()
			{
				return mFragments.size();
			}

			@Override
			public Fragment getItem(int arg0)
			{
				return mFragments.get(arg0);
			}
		};
		
		myPageAdapter = new PagerAdapter()
		{

			@Override
			public void destroyItem(ViewGroup container, int position,
					Object object)
			{
				 container.removeView(mFragments.get(position).getView());
			}

			@Override
			public Object instantiateItem(ViewGroup container, int position)
			{
				/*View view = mViews.get(position);
				container.addView(view);*/
				
				Fragment fragment = mFragments.get(position);
		        if(!fragment.isAdded()){ // 如果fragment还没有added
		            FragmentTransaction ft = fm.beginTransaction();
		            ft.add(fragment, fragment.getClass().getSimpleName());
		            ft.commit();
		            /**
		             * 在用FragmentTransaction.commit()方法提交FragmentTransaction对象后
		             * 会在进程的主线程中,用异步的方式来执行。
		             * 如果想要立即执行这个等待中的操作,就要调用这个方法(只能在主线程中调用)。
		             * 要注意的是,所有的回调和相关的行为都会在这个调用中被执行完成,因此要仔细确认这个方法的调用位置。
		             */
		            fm.executePendingTransactions();
		        }

		        if(fragment.getView().getParent() == null){
		            container.addView(fragment.getView()); // 为viewpager增加布局
		        }

		        return fragment.getView();
				//return view;
			}

			@Override
			public boolean isViewFromObject(View arg0, Object arg1)
			{
				return arg0 == arg1;
			}

			@Override
			public int getCount()
			{
				return mFragments.size();
			}
		};

		
	  //设置缓存数,我们这里不需要预加载
		mViewPager .setOffscreenPageLimit(0);
		mViewPager.setAdapter(myPageAdapter);
	
		
		mViewPager.setOnPageChangeListener(new OnPageChangeListener()
		{
			
			@Override
			public void onPageSelected(int arg0)
			{
				int currentItem = mViewPager.getCurrentItem();
				setTab(currentItem);
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2)
			{
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0)
			{
				// TODO Auto-generated method stub
				
			}
		});
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.id_tab_weixin:
			setSelect(0);
			break;
		case R.id.id_tab_frd:
			setSelect(1);
			break;
		case R.id.id_tab_address:
			setSelect(2);
			break;
		case R.id.id_tab_settings:
			setSelect(3);
			break;

		default:
			break;
		}
	}

	private void setSelect(int i)
	{
		
		FragmentManager fm = getSupportFragmentManager();
		FragmentTransaction transaction = fm.beginTransaction();
		hideFragment(transaction);
		
		switch (i)
		{
		case 0:
			
			
			//mImgWeixin.setImageResource(R.drawable.tab_weixin_pressed);
			break;
		case 1:
			//transaction.show(mTab02);
			mTab02.onResume();
		//	mImgFrd.setImageResource(R.drawable.tab_find_frd_pressed);
			break;
		case 2:
			
			
			//mImgAddress.setImageResource(R.drawable.tab_address_pressed);
			break;
		case 3:
			
			
			//mImgSettings.setImageResource(R.drawable.tab_settings_pressed);
			break;

		default:
			break;
		}

		
		setTab(i);
		mViewPager.setCurrentItem(i);
	}

	private void setTab(int i)
	{
		resetImgs();
		// 设置图片为亮色
		// 切换内容区域
		setBottom(i);
	
	}

	/**
	 * 切换图片至暗色,不选中
	 */
	private void resetImgs()
	{
		mImgWeixin.setSelected(false);
		mImgContact.setSelected(false);
		mImgFind.setSelected(false);
		mImgSettings.setSelected(false);
		
		 mContacttxt.setSelected(false);
		 mWeixintxt.setSelected(false);
		 mFindtxt.setSelected(false);
		 mSettingtxt.setSelected(false);
	}
	 //选中
	private void setBottom(int position)
	{
		
		switch (position) {
		case 0:
		
			mImgWeixin.setSelected(true);
			mWeixintxt.setSelected(true);
			break;
		case 1:
		
			mImgContact.setSelected(true);
			mContacttxt.setSelected(true);
			break;
		case 2:
			
			mImgFind.setSelected(true);
			mFindtxt.setSelected(true);
			break;
		case 3:
			
			mImgSettings.setSelected(true);
			mSettingtxt.setSelected(true);
			break;

		default:
			break;
		}
	}

	private void hideFragment(FragmentTransaction transaction)
	{
		if (mTab01 != null)
		{
			transaction.hide(mTab01);
		}
		if (mTab02 != null)
		{
			transaction.hide(mTab02);
		}
		if (mTab03 != null)
		{
			transaction.hide(mTab03);
		}
		if (mTab04 != null)
		{
			transaction.hide(mTab04);
		}
	}
}

代码不复杂,很简单。最后附上源码和包。有问题联系我。。

点击打开链接

高仿微信主界面:ViewPage+Fragment 不预加载Fragment 也不会销毁Fragment

上一篇:AI之HCI:人机交互Human-Computer Interaction的简介、发展历史、案例应用之详细攻略(一)


下一篇:zabbix微信报警