1

I am trying to implement a Gallery with a FragmentPagerAdapter on a ViewPager. Each fragment has a "delete" button which deletes the file.

Then I am trying to remove this fragment and scroll to next fragment when a callback of a AsyncTask is called.

In summary, when the delete button is touched first, delete the file in a remote FTP server and then remove the local file.

This is the Activity container of the ViewPager

 public class MediaViewerActivity extends FragmentActivity implements OnFTPResult{

    (...)
    protected void onCreate(Bundle savedInstanceState) {
    ArrayList<String> multimedia;

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

            multimedia = getFiles();
            MediaViewerAdapter adapter;
            adapter = new MediaViewerAdapter(getSupportFragmentManager(), MediaViewerActivity.this, multimedia);
            viewPager.setOffscreenPageLimit(2);
            viewPager.setAdapter(adapter);


            viewPager.setCurrentItem(position);


    (...)
}    
    @Override
        public void onActionPerformed(String result) {

            switch (result) {
            case "Done delete":
            //remove the fragment and local file
                break;
        }
        }
    }

This is the code of the adapter for the ViewPager

public class MediaViewerAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener{

(...)
    public MediaViewerAdapter(FragmentManager fm, Context _ctx,ArrayList<String> imagePaths) {
        super(fm);
        manager = fm;
        _imagePaths = imagePaths;
        ctx = _ctx;
    }
}

(...)

@Override
    public Fragment getItem(int position) {

        Fragment fragment;
        String[] fileExtensionCaching = _imagePaths.get(position).split("\\.");
        String currentExtension = fileExtensionCaching[fileExtensionCaching.length - 1];

        Bundle args = new Bundle();     
        args.putString("path", _imagePaths.get(position));

        if((currentExtension.equals("pdf")))
            fragment = FragmentPDFViewer.newInstance(args);
        else
        if((currentExtension.equals("jpg") ||  currentExtension.equals("png") ||  currentExtension.equals("gif"))) {
            fragment = FragmentImageViewer.newInstance(args);

        }
        else{
            fragment = FragmentVideoPlayer.newInstance(args);
        }


        return fragment;
    }

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

And this is a slice of the AsyncTask class

@Override
    protected void onPostExecute(String response) {

       ((OnFTPResult)ctx).onActionPerformed(response);

    }

Which would be the best safety way to remove the current Fragment and then scroll to next (If there is)?

Thanks

Hanzo
  • 1,839
  • 4
  • 30
  • 51
  • I have found the solution in [this](http://stackoverflow.com/questions/10396321/remove-fragment-page-from-viewpager-in-android) answer. Thanks for all. – Hanzo Dec 03 '15 at 12:49

0 Answers0