android---android 페이지 나누기

6520 단어
인용문: 모두가 알고 있는 바와 같이 데이터의 캐시 여부는 세 가지 페이지로 나눌 수 있다.
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;
        }
    }

}

좋은 웹페이지 즐겨찾기