android切换卡顿解决方法

如果想要让应用用户流畅的滑动体验的话,那么就必须对activity和fragment的生命周期有一个完整的概念以及在何种情况下会触发哪些事件。

 

在自己目前做的项目中,就遇到了这样的问题,那么就把自己的解决方法罗列,重点在于onpause,onstop,onstart,onresume的事件处理。

某些耗时的操作延迟加载,先展示总体页面。

当对某些复杂的页面进行切换时,先隐藏后显示,如果不是特别卡顿不需要隐藏,如果卡顿严重的话强烈推荐隐藏,提升用户体验。

 

代码如下:

 

package com.hengtiansoft.mecb.fragment;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.hengtiansoft.mecb.R;
import com.hengtiansoft.mecb.adapter.FragmentTabPagerAdapter;
import com.hengtiansoft.mecb.adapter.ProductOnSaleAdapter;
import com.hengtiansoft.mecb.model.MenuProductOnSaleModel;
import com.viewpagerindicator.UnderlinePageIndicator;

public class MenuProductFragment extends BaseFragment{
    
    private ViewPager mVpProductPage;
    private FragmentTabPagerAdapter mTabPagerProductAdapter;
    private List<BaseFragment> mProductTabFragment;
    
    private RadioGroup mRgProductTab;
    private RadioButton mRbOnSale;
    private RadioButton mRbForSale;
    private RadioButton mRbOffSale;
    
    private UnderlinePageIndicator mIndicator;
    private View mView;
    
    //流畅显示切换所用的线程
    private Handler handler = new Handler();
    
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_menu_product, container, false);
        initView(mView);
        
        return mView;
    }
    
    @Override
    public void onStart() {
        super.onStart();
        handler.postDelayed(new Runnable() {
            public void run() {
                setData(mView);
            }
        }, 200);
        setListener(mView);
    }
    
    

    @Override
    public void onResume() {
        super.onResume();
        handler.postDelayed(new Runnable() {
            public void run() {
                mVpProductPage.setVisibility(View.VISIBLE);
            }
        }, 0);
    }

    @Override
    public void onPause() {
        super.onPause();
        handler.postDelayed(new Runnable() {
            public void run() {
                mVpProductPage.setVisibility(View.INVISIBLE);
            }
        }, 100);
    }
    
    private void initView(View view) {
        mVpProductPage = (ViewPager)view.findViewById(R.id.vp_product_page);
        mIndicator = (UnderlinePageIndicator)view.findViewById(R.id.indicator_product_sort);
        mRgProductTab = (RadioGroup) view.findViewById(R.id.rg_product_tab);
        mRbOnSale = (RadioButton)view.findViewById(R.id.rb_product_onsale);
        mRbOffSale = (RadioButton)view.findViewById(R.id.rb_product_offsale);
        mRbForSale = (RadioButton)view.findViewById(R.id.rb_product_forsale);
    }
    
    private void setData(View view) {
        mProductTabFragment = new ArrayList<BaseFragment>();
        mProductTabFragment.add(new ProductOnSaleFragment());
        mProductTabFragment.add(new ProductForSaleFragment());
        mProductTabFragment.add(new ProductOffSaleFragment());
        
        mTabPagerProductAdapter = new FragmentTabPagerAdapter(mProductTabFragment,
                getChildFragmentManager());
        mVpProductPage.setAdapter(mTabPagerProductAdapter);
        mVpProductPage.setOffscreenPageLimit(2);
        mIndicator.setViewPager(mVpProductPage);
        mIndicator.setFades(false);
    }
    
    private void setListener(View view) {
        mRgProductTab.setOnCheckedChangeListener(new OnCheckedChangeListener(){

            @Override
            public void onCheckedChanged(RadioGroup arg0, int checkedId) {
                if(checkedId == mRbOnSale.getId()){
                    mVpProductPage.setCurrentItem(0,false);
                }else if(checkedId == mRbForSale.getId()){
                    mVpProductPage.setCurrentItem(1,false);
                }else{
                    mVpProductPage.setCurrentItem(2,false);
                }
            }
        });
        
        mIndicator.setOnPageChangeListener(new OnPageChangeListener(){

            @Override
            public void onPageScrollStateChanged(int position) {
            }
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }
            @Override
            public void onPageSelected(int position) {
                switch(position){
                case 0:
                    mRbOnSale.setChecked(true);
                    break;
                case 1:
                    mRbForSale.setChecked(true);
                    break;
                case 2:
                    mRbOffSale.setChecked(true);
                    break;
                default:
                    break;
                }
            }
        });
        
        
        
    }

}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。