convert MyPagerAdapter to kotlin
This commit is contained in:
parent
def05ffbe2
commit
122209fb28
2 changed files with 67 additions and 84 deletions
|
@ -1,84 +0,0 @@
|
|||
package com.simplemobiletools.gallery.adapters;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
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;
|
||||
import com.simplemobiletools.gallery.models.Medium;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class MyPagerAdapter extends FragmentStatePagerAdapter {
|
||||
private final List<Medium> mMedia;
|
||||
private final Map<Integer, ViewPagerFragment> mFragments;
|
||||
private final ViewPagerActivity mActivity;
|
||||
|
||||
public MyPagerAdapter(ViewPagerActivity act, FragmentManager fm, List<Medium> media) {
|
||||
super(fm);
|
||||
mActivity = act;
|
||||
mMedia = media;
|
||||
mFragments = new HashMap<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
return mMedia.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Fragment getItem(int position) {
|
||||
final Medium medium = mMedia.get(position);
|
||||
final Bundle bundle = new Bundle();
|
||||
bundle.putSerializable(Constants.MEDIUM, medium);
|
||||
ViewPagerFragment fragment;
|
||||
|
||||
if (medium.getIsVideo()) {
|
||||
fragment = new VideoFragment();
|
||||
} else {
|
||||
fragment = new PhotoFragment();
|
||||
}
|
||||
|
||||
mFragments.put(position, fragment);
|
||||
fragment.setArguments(bundle);
|
||||
fragment.setListener(mActivity);
|
||||
return fragment;
|
||||
}
|
||||
|
||||
public void itemDragged(int pos) {
|
||||
if (mFragments.get(pos) != null) {
|
||||
mFragments.get(pos).itemDragged();
|
||||
}
|
||||
}
|
||||
|
||||
public void updateUiVisibility(boolean isFullscreen, int pos) {
|
||||
for (int i = -1; i <= 1; i++) {
|
||||
final ViewPagerFragment fragment = mFragments.get(pos + i);
|
||||
if (fragment != null) {
|
||||
fragment.systemUiVisibilityChanged(isFullscreen);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void updateItems(int pos) {
|
||||
for (int i = -1; i <= 1; i++) {
|
||||
final ViewPagerFragment fragment = mFragments.get(pos + i);
|
||||
if (fragment != null) {
|
||||
fragment.updateItem();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void updateItems(List<Medium> newPaths) {
|
||||
mMedia.clear();
|
||||
mMedia.addAll(newPaths);
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
package com.simplemobiletools.gallery.adapters
|
||||
|
||||
import android.os.Bundle
|
||||
import android.support.v4.app.Fragment
|
||||
import android.support.v4.app.FragmentManager
|
||||
import android.support.v4.app.FragmentStatePagerAdapter
|
||||
import android.util.SparseArray
|
||||
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
|
||||
import com.simplemobiletools.gallery.models.Medium
|
||||
|
||||
class MyPagerAdapter(val activity: ViewPagerActivity, fm: FragmentManager, val media: MutableList<Medium>) : FragmentStatePagerAdapter(fm) {
|
||||
private val mFragments: SparseArray<ViewPagerFragment>
|
||||
|
||||
init {
|
||||
mFragments = SparseArray<ViewPagerFragment>()
|
||||
}
|
||||
|
||||
override fun getCount(): Int {
|
||||
return media.size
|
||||
}
|
||||
|
||||
override fun getItem(position: Int): Fragment {
|
||||
val medium = media[position]
|
||||
val bundle = Bundle()
|
||||
bundle.putSerializable(Constants.MEDIUM, medium)
|
||||
val fragment: ViewPagerFragment
|
||||
|
||||
if (medium.isVideo) {
|
||||
fragment = VideoFragment()
|
||||
} else {
|
||||
fragment = PhotoFragment()
|
||||
}
|
||||
|
||||
mFragments.put(position, fragment)
|
||||
fragment.arguments = bundle
|
||||
fragment.setListener(activity)
|
||||
return fragment
|
||||
}
|
||||
|
||||
fun itemDragged(pos: Int) {
|
||||
mFragments[pos]?.itemDragged()
|
||||
}
|
||||
|
||||
fun updateUiVisibility(isFullscreen: Boolean, pos: Int) {
|
||||
for (i in -1..1) {
|
||||
val fragment = mFragments[pos + i]
|
||||
fragment?.systemUiVisibilityChanged(isFullscreen)
|
||||
}
|
||||
}
|
||||
|
||||
fun updateItems(pos: Int) {
|
||||
for (i in -1..1) {
|
||||
val fragment = mFragments[pos + i]
|
||||
fragment?.updateItem()
|
||||
}
|
||||
}
|
||||
|
||||
fun updateItems(newPaths: List<Medium>) {
|
||||
media.clear()
|
||||
media.addAll(newPaths)
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue