package com.hotbitmapgg.moequest.module.meizitu; import butterknife.Bind; import com.hotbitmapgg.moequest.R; import com.hotbitmapgg.moequest.adapter.MeiziTuAdapter; import com.hotbitmapgg.moequest.base.RxBaseFragment; import com.hotbitmapgg.moequest.entity.meizitu.MeiziTu; import com.hotbitmapgg.moequest.network.RetrofitHelper; import com.hotbitmapgg.moequest.rx.RxBus; import com.hotbitmapgg.moequest.utils.LogUtil; import com.hotbitmapgg.moequest.utils.MeiziUtil; import com.hotbitmapgg.moequest.utils.SnackbarUtil; import io.realm.Realm; import io.realm.RealmResults; import java.io.IOException; import java.util.List; import java.util.Map; import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers; import android.app.ActivityOptions; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.support.v4.app.SharedElementCallback; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.StaggeredGridLayoutManager; import android.view.View; import android.view.ViewTreeObserver; /** * Created by hcc on 16/7/19 20:44 * 100332338@qq.com * <p/> * 妹子图详情界面 */ public class MeiziTuSimpleFragment extends RxBaseFragment { @Bind(R.id.recycle) RecyclerView mRecyclerView; @Bind(R.id.swipe_refresh) SwipeRefreshLayout mSwipeRefreshLayout; private static final int PRELOAD_SIZE = 6; public static final String EXTRA_TYPE = "extra_type"; private boolean mIsLoadMore = true; private int pageNum = 24; private int page = 1; private MeiziTuAdapter mAdapter; private StaggeredGridLayoutManager mLayoutManager; private String type; private Realm realm; private RealmResults<MeiziTu> meizis; private int imageIndex; private boolean mIsRefreshing = false; public static MeiziTuSimpleFragment newInstance(String type) { MeiziTuSimpleFragment mFragment = new MeiziTuSimpleFragment(); Bundle bundle = new Bundle(); bundle.putString(EXTRA_TYPE, type); mFragment.setArguments(bundle); return mFragment; } @Override public int getLayoutId() { return R.layout.fragment_simple_meizi; } @Override public void initViews() { type = getArguments().getString(EXTRA_TYPE); showProgress(); realm = Realm.getDefaultInstance(); meizis = realm.where(MeiziTu.class) .equalTo("type", type) .findAll(); initRecycleView(); RxBus.getInstance().toObserverable(Intent.class) .compose(this.bindToLifecycle()) .subscribe(intent -> { imageIndex = intent.getIntExtra("index", -1); scrollIndex(); finishTask(); }, throwable -> { LogUtil.all(throwable.getMessage()); }); setEnterSharedElementCallback(new SharedElementCallback() { @Override public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) { super.onMapSharedElements(names, sharedElements); String newTransitionName = meizis.get(imageIndex).getImageurl(); View newSharedView = mRecyclerView.findViewWithTag(newTransitionName); if (newSharedView != null) { names.clear(); names.add(newTransitionName); sharedElements.clear(); sharedElements.put(newTransitionName, newSharedView); } } }); } private void initRecycleView() { mRecyclerView.setHasFixedSize(true); mLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); mRecyclerView.setLayoutManager(mLayoutManager); mRecyclerView.addOnScrollListener(OnLoadMoreListener(mLayoutManager)); mAdapter = new MeiziTuAdapter(mRecyclerView, meizis); mRecyclerView.setAdapter(mAdapter); setRecycleScrollBug(); } private void showProgress() { mSwipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary); mSwipeRefreshLayout.setOnRefreshListener(() -> { page = 1; mIsRefreshing = true; clearCache(); getMeizis(); }); mSwipeRefreshLayout.postDelayed(() -> { mSwipeRefreshLayout.setRefreshing(true); mIsRefreshing = true; clearCache(); getMeizis(); }, 500); } private void clearCache() { try { realm.beginTransaction(); realm.where(MeiziTu.class) .equalTo("type", type) .findAll().clear(); realm.commitTransaction(); } catch (Exception e) { e.printStackTrace(); } } private void getMeizis() { RetrofitHelper.getMeiziTuApi() .getMeiziTuApi(type, page) .compose(this.bindToLifecycle()) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(responseBody -> { try { String html = responseBody.string(); List<MeiziTu> list = MeiziUtil.getInstance().parserMeiziTuHtml(html, type); MeiziUtil.getInstance().putMeiziTuCache(list); finishTask(); } catch (IOException e) { e.printStackTrace(); } }, throwable -> { mSwipeRefreshLayout.post(() -> mSwipeRefreshLayout.setRefreshing(false)); SnackbarUtil.showMessage(mRecyclerView, getString(R.string.error_message)); LogUtil.all(throwable.getMessage()); }); } private void finishTask() { if (page * pageNum - pageNum - 1 > 0) { mAdapter.notifyItemRangeChanged(page * pageNum - pageNum - 1, pageNum); } else { mAdapter.notifyDataSetChanged(); } if (mSwipeRefreshLayout.isRefreshing()) { mSwipeRefreshLayout.setRefreshing(false); } mIsRefreshing = false; mAdapter.setOnItemClickListener((position, holder) -> { Intent intent = MeiziTuPageActivity.luanch(getActivity(), position, type); if (Build.VERSION.SDK_INT >= 22) { startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(getActivity(), holder.getParentView().findViewById(R.id.item_img), meizis.get(position).getImageurl()).toBundle()); } else { startActivity(intent); } }); } RecyclerView.OnScrollListener OnLoadMoreListener(StaggeredGridLayoutManager layoutManager) { return new RecyclerView.OnScrollListener() { @Override public void onScrolled(RecyclerView rv, int dx, int dy) { boolean isBottom = mLayoutManager.findLastCompletelyVisibleItemPositions( new int[2])[1] >= mAdapter.getItemCount() - PRELOAD_SIZE; if (!mSwipeRefreshLayout.isRefreshing() && isBottom) { if (!mIsLoadMore) { mSwipeRefreshLayout.setRefreshing(true); page++; getMeizis(); } else { mIsLoadMore = false; } } } }; } public void scrollIndex() { if (imageIndex != -1) { mRecyclerView.scrollToPosition(imageIndex); mRecyclerView.getViewTreeObserver() .addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { @Override public boolean onPreDraw() { mRecyclerView.getViewTreeObserver().removeOnPreDrawListener(this); mRecyclerView.requestLayout(); return true; } }); } } private void setRecycleScrollBug() { mRecyclerView.setOnTouchListener((v, event) -> mIsRefreshing); } }