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

View File

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

View File

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

View File

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

View File

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