0

我有 50 张 100 kb 大小的图像。我将这些图像放在我的可绘制文件夹中。
使用下面的代码我在我的应用程序中使用这些图像

package com.example.ferrero;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
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.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class MainActivity extends FragmentActivity {
        private ViewPager mPager;
        private ScreenSlidePagerAdapter mPagerAdapter;
        public static final String ARG_PAGE = "page";
        private static final String TAG = "MainActivity";
        ArrayList<Integer> arrayListOne ;
        private ArrayList<Integer> arrayListTwo;
        ArrayList<Integer> mainArrayList;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main_activity);
            initialiseArrayList();
            mainArrayList = arrayListOne;
             mPager = (ViewPager) findViewById(R.id.pager);
             mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
             mPager.setAdapter(mPagerAdapter);
        }

        private void initialiseArrayList() {
            arrayListOne = new ArrayList<Integer>();
            arrayListOne.add(R.drawable.s01);
            arrayListOne.add(R.drawable.s002);
            arrayListOne.add(R.drawable.s003);
            arrayListOne.add(R.drawable.s004);
            arrayListOne.add(R.drawable.s005);
            arrayListOne.add(R.drawable.s006);
            arrayListOne.add(R.drawable.s007);
            arrayListOne.add(R.drawable.s008);
            arrayListOne.add(R.drawable.s009);
            arrayListOne.add(R.drawable.s010);
            arrayListOne.add(R.drawable.s011);
            arrayListOne.add(R.drawable.s012);

            arrayListTwo = new ArrayList<Integer>();
            arrayListTwo.add(R.drawable.video);
            arrayListTwo.add(R.drawable.video);
            arrayListTwo.add(R.drawable.video);
            arrayListTwo.add(R.drawable.video);
            arrayListTwo.add(R.drawable.video);



        }

        public void click(View v){
            Log.v(TAG, "Click");
            mainArrayList.clear();
            mainArrayList = arrayListTwo;
             mPager = (ViewPager) findViewById(R.id.pager);
             mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
             mPager.setAdapter(mPagerAdapter);

        }
         public class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
                public ScreenSlidePagerAdapter(FragmentManager fm) {
                    super(fm);
                }

                @SuppressLint("ValidFragment")
                @Override
                public Fragment getItem(int position) {
                    ScreenSlidePageFragment fragment = new ScreenSlidePageFragment(position);
                    return fragment;
                }

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


         @SuppressLint("ValidFragment")
        public class ScreenSlidePageFragment extends Fragment {
                private int mPosition;
                public ScreenSlidePageFragment(int pos) {
                    mPosition = pos;
                }

                @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container,
                        Bundle savedInstanceState) {
                    // Inflate the layout containing a title and body text.
                    ViewGroup rootView=null ;
                    if(mPosition == 11){
                        rootView    = (ViewGroup) inflater
                                .inflate(R.layout.fragment_screen_slide_page_one, container, false);
                    }else{
                     rootView = (ViewGroup) inflater
                            .inflate(R.layout.fragment_screen_slide_page, container, false);
                    ImageView imageView = (ImageView) rootView.findViewById(R.id.ImageView);
                    imageView.setImageResource(mainArrayList.get(mPosition));
                    }
                    return rootView;
                }

         }
}
4

0 回答 0