use interfaces for communicating with fragments

This commit is contained in:
tibbi 2016-06-14 11:05:49 +02:00
parent e97f9b1db8
commit a6e4e949e0
5 changed files with 22 additions and 7 deletions

View file

@ -24,6 +24,7 @@ import com.simplemobiletools.gallery.MyViewPager;
import com.simplemobiletools.gallery.R;
import com.simplemobiletools.gallery.Utils;
import com.simplemobiletools.gallery.adapters.MyPagerAdapter;
import com.simplemobiletools.gallery.fragments.ViewPagerFragment;
import com.simplemobiletools.gallery.models.Medium;
import java.io.File;
@ -36,7 +37,8 @@ import butterknife.ButterKnife;
import butterknife.OnClick;
public class ViewPagerActivity extends AppCompatActivity
implements ViewPager.OnPageChangeListener, View.OnSystemUiVisibilityChangeListener, ViewPager.OnTouchListener {
implements ViewPager.OnPageChangeListener, View.OnSystemUiVisibilityChangeListener, ViewPager.OnTouchListener,
ViewPagerFragment.FragmentClickListener {
@BindView(R.id.undo_delete) View undoBtn;
@BindView(R.id.view_pager) MyViewPager pager;
@ -76,7 +78,7 @@ public class ViewPagerActivity extends AppCompatActivity
if (isDirEmpty())
return;
final MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager(), media);
final MyPagerAdapter adapter = new MyPagerAdapter(this, getSupportFragmentManager(), media);
pager.setAdapter(adapter);
pager.setCurrentItem(pos);
pager.addOnPageChangeListener(this);
@ -301,6 +303,7 @@ public class ViewPagerActivity extends AppCompatActivity
return myMedia;
}
@Override
public void fragmentClicked() {
deleteFile();
isFullScreen = !isFullScreen;

View file

@ -6,6 +6,7 @@ import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import com.simplemobiletools.gallery.Constants;
import com.simplemobiletools.gallery.activities.ViewPagerActivity;
import com.simplemobiletools.gallery.fragments.PhotoFragment;
import com.simplemobiletools.gallery.fragments.VideoFragment;
import com.simplemobiletools.gallery.fragments.ViewPagerFragment;
@ -18,9 +19,11 @@ import java.util.Map;
public class MyPagerAdapter extends FragmentStatePagerAdapter {
private List<Medium> media;
private Map<Integer, ViewPagerFragment> fragments;
private ViewPagerActivity activity;
public MyPagerAdapter(FragmentManager fm, List<Medium> media) {
public MyPagerAdapter(ViewPagerActivity act, FragmentManager fm, List<Medium> media) {
super(fm);
this.activity = act;
this.media = media;
fragments = new HashMap<>();
}
@ -45,6 +48,7 @@ public class MyPagerAdapter extends FragmentStatePagerAdapter {
fragments.put(position, fragment);
fragment.setArguments(bundle);
fragment.setListener(activity);
return fragment;
}

View file

@ -9,7 +9,6 @@ import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import com.simplemobiletools.gallery.Constants;
import com.simplemobiletools.gallery.R;
import com.simplemobiletools.gallery.activities.ViewPagerActivity;
import com.simplemobiletools.gallery.models.Medium;
public class PhotoFragment extends ViewPagerFragment implements View.OnClickListener {
@ -38,7 +37,7 @@ public class PhotoFragment extends ViewPagerFragment implements View.OnClickList
@Override
public void onClick(View v) {
((ViewPagerActivity) getActivity()).fragmentClicked();
listener.fragmentClicked();
}
@Override

View file

@ -24,7 +24,6 @@ import android.widget.TextView;
import com.simplemobiletools.gallery.Constants;
import com.simplemobiletools.gallery.R;
import com.simplemobiletools.gallery.Utils;
import com.simplemobiletools.gallery.activities.ViewPagerActivity;
import com.simplemobiletools.gallery.models.Medium;
import java.io.IOException;
@ -157,7 +156,7 @@ public class VideoFragment extends ViewPagerFragment
default:
isFullscreen = !isFullscreen;
checkFullscreen();
((ViewPagerActivity) getActivity()).fragmentClicked();
listener.fragmentClicked();
break;
}
}

View file

@ -3,7 +3,17 @@ package com.simplemobiletools.gallery.fragments;
import android.support.v4.app.Fragment;
public abstract class ViewPagerFragment extends Fragment {
protected FragmentClickListener listener;
public void setListener(FragmentClickListener ln) {
listener = ln;
}
public abstract void itemDragged();
public abstract void systemUiVisibilityChanged(boolean toFullscreen);
public interface FragmentClickListener {
void fragmentClicked();
}
}