ViewPager包含显示ParseImage的片段

时间:2014-07-23 16:40:55

标签: android android-fragments android-viewpager

我遇到ViewPager问题。我的ViewPager必须显示来自Parse.com的图像。 ViewPager的每个项目都是一个片段,相同的片段,它根据项目显示不同的图像。 我的问题是例如:如果我有三个页面,第一页显示什么,第二页显示正确的图像,第三页也显示正确的图像。当我返回第一页然后图像加载,但只有当我返回它,而不是第一次。 这是我的代码:

我的活动:

public class FolletosActivity extends FragmentActivity {

public static final String QUERY = "query";

String selec;
ViewPager viewPager;
private PagerAdapter mPagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_carta_menu);

    Intent intent = getIntent();    
    selec = intent.getStringExtra(QUERY);

    viewPager = (ViewPager) findViewById(R.id.pager);

    mPagerAdapter = new FolletosPagerAdapter(getSupportFragmentManager());
    viewPager.setAdapter(mPagerAdapter);

}

PagerAdapter:

public class FolletosPagerAdapter extends FragmentPagerAdapter {

int numPages;

public FolletosPagerAdapter(FragmentManager fm) {
    super(fm);

    ParseQuery<ParseObject> query = ParseQuery.getQuery("folletos_app");
    query.whereGreaterThanOrEqualTo("num_pag", 0); //busco todos los objetos de parse
    List<ParseObject> promosParse = null;
    try {
        promosParse = query.find();
        numPages = promosParse.size();
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
}

@Override
public Fragment getItem(int position) {
    return new PaginaFolletoFragment(position);

}

@Override
public int getCount() {
    return numPages;
}

}

碎片:

public class PaginaFolletoFragment extends Fragment {
public static final String QUERY = "query";
ParseImageView imageView;
String selec;
int position_;

public PaginaFolletoFragment(int position) {
    // TODO Auto-generated constructor stub
    position_ = position;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);

    String filepath = "/sdcard/Android/data/package/cache/uil-images/"+"folleto_"+selec+"_"+"numpage_"+position_;
    File file = new File(filepath);
    if(file.exists()){
        Bitmap bmp = BitmapFactory.decodeFile(filepath);
        imageView.setImageBitmap(bmp);
    }
    else{

        if(ImageLoader.getInstance().isInited())
            ImageLoader.getInstance().destroy();

        //Create image options.
        DisplayImageOptions options = new DisplayImageOptions.Builder()
            .cacheInMemory(true)
            .cacheOnDisc(true)
            .build();

        //Create a config with those options.
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getActivity().getApplicationContext())
            .defaultDisplayImageOptions(options)
            .discCacheFileNameGenerator(new FileNameGenerator() {

                @Override
                public String generate(String imageUri) {

                    return "folleto_"+selec+"_"+"numpage_"+position_;

                }

            })

        .memoryCacheSize(41943040)
        .discCacheSize(104857600)
        .threadPoolSize(10)
        .build();

        ImageLoader.getInstance().init(config); 


        ParseQuery<ParseObject> query = ParseQuery.getQuery("folletos_app");

        query.whereEqualTo("num_pag", position_); 


        List<ParseObject> promosParse;
        try {
            promosParse = query.find();

            for(final ParseObject dealsObject : promosParse) {

                final ParseFile image = dealsObject.getParseFile("pagina");
                imageView.loadInBackground(new GetDataCallback() {
                     public void done(byte[] data, ParseException e) {
                     // The image is loaded and displayed!                    
                     int oldHeight = imageView.getHeight();
                     int oldWidth = imageView.getWidth();                 

                     ImageLoader imageLoader = ImageLoader.getInstance();
                     imageLoader.displayImage(image.getUrl(), imageView);


                     }
                });

            }

        } catch (ParseException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } //los meto en una lista

    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_pagina_menu, container, false);
    imageView = (ParseImageView) view.findViewById(R.id.imagePage);     

    Intent intent = getActivity().getIntent();  
    selec = intent.getStringExtra(QUERY);

    return view;
}

}

1 个答案:

答案 0 :(得分:0)

对不起,如果这已经晚了,但这就是你想做的事情..

//Get the image from your ParseObject

ParseFile file = (ParseFile) dealsObject.getParseFile("pagina");

//Set the imageview to show the content of your ParseFile

imageView.setParseFile(file);

//Load the image in the background and display ones loaded

imageView.loadInBackground(new GetDataCallback() {
    @Override
    public void done(byte[] bytes, ParseException e) {
        if (e != null) {

        }
    }
}