android---浅谈android分页

引言:众所周知,以数据是否缓存,可以分为三种分页方式:

1.全部取出,全部缓存缓存。

2.部分取出,部分缓存。

3.查一页,输出一页,不缓存。

三种分页方式的实现方法各不相同,使用的地方不同。

例如:

第一种,高体验的VIEW,例如:导航页,文字输入缓存页,数据量主要为文本的(QQ页面)。小说也可以采用这样的方式。

第二种,适合文本,小说这样大批量的文字,而实际占用内存空间大的数据(例如图文混排,一般不现实,实现代价太大)

第三种,适合BMP位图,基于android平台下的bmp图片缓存不可以超过2M,而一般一张BMP图就非常大。


具体实现代码之后会编辑上来!

第三种代码实现:

import java.util.Locale;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link FragmentPagerAdapter} derivative, which will keep every
     * loaded fragment in memory. If this becomes too memory intensive, it
     * may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);



        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
       

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).
        	so.p("position---"+position);
            return PlaceholderFragment.newInstance(position + 1);
//        	return null;
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 5;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            so.p("getPageTitle调用");
            switch (position) {
                case 0:
//                	so.p(getString(R.string.title_section1).toUpperCase(l));
                	return getString(R.string.title_section1).toUpperCase(l);
                case 1:
//                	so.p(getString(R.string.title_section2).toUpperCase(l));
                    return getString(R.string.title_section2).toUpperCase(l);
                case 2:
//                	so.p(getString(R.string.title_section3).toUpperCase(l));
                    return getString(R.string.title_section3).toUpperCase(l);
            }
            return null;
        }
    }
    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
//            so.p(sectionNumber);
            fragment.setArguments(args);
           
            return fragment;
        }

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            TextView textView = (TextView) rootView.findViewById(R.id.section_label);
//            textView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
            
           int check= getArguments().getInt(ARG_SECTION_NUMBER);
           //分页器
           so.p("-------------------------------------");
           switch (check) {
				case 1:textView.setText("第一页");
						so.p(1);
					break;
				case 2:textView.setText("第二页");
						so.p(2);
					break;
				case 3:textView.setText("第三页");
						so.p(3);
					break;
				case 4:textView.setText("第四页");
						so.p(4);
					break;
				case 5:textView.setText("第五页");
						so.p(5);
					break;
		
				default:
					break;
				}
            so.p("-------------------------------------");
            so.p(ARG_SECTION_NUMBER);
            so.p(getArguments());
            return rootView;
        }
    }

}


你可能感兴趣的:(android---浅谈android分页)