cn.hjl.newspush.mvp.ui.activities.NewsPhotoDetailActivity.java Source code

Java tutorial

Introduction

Here is the source code for cn.hjl.newspush.mvp.ui.activities.NewsPhotoDetailActivity.java

Source

/*
 * Copyright (c) 2016  <kaku201313@163.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package cn.hjl.newspush.mvp.ui.activities;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.hjl.newspush.R;
import cn.hjl.newspush.common.Constants;
import cn.hjl.newspush.mvp.entity.NewsPhotoDetail;
import cn.hjl.newspush.mvp.ui.BaseActivity;
import cn.hjl.newspush.mvp.ui.fragment.PhotoDetailFragment;
import cn.hjl.newspush.rxpacked.rxbus.RxBus;
import rx.functions.Action1;

/**
 * @author 
 * @version 1.0 2016/7/8
 */
public class NewsPhotoDetailActivity extends BaseActivity {

    @BindView(R.id.viewpager)
    ViewPager mViewpager;
    @BindView(R.id.photo_detail_title_tv)
    TextView mPhotoDetailTitleTv;

    private List<PhotoDetailFragment> mPhotoDetailFragmentList = new ArrayList<>();
    private NewsPhotoDetail mNewsPhotoDetail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //        mSubscriptions.add(RxBus.getDefault().toObservable(PhotoDetailOnClickEvent.class)
        //                .subscribe(new Action1<PhotoDetailOnClickEvent>() {
        //                    @Override
        //                    public void call(PhotoDetailOnClickEvent photoDetailOnClickEvent) {
        //                        if (mPhotoDetailTitleTv.getVisibility() == View.VISIBLE) {
        //                            startAnimation(View.GONE, 0.9f, 0.5f);
        //                        } else {
        //                            mPhotoDetailTitleTv.setVisibility(View.VISIBLE);
        //                            startAnimation(View.VISIBLE, 0.5f, 0.9f);
        //                        }
        //                    }
        //                }));
    }

    private void startAnimation(final int endState, float startValue, float endValue) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(mPhotoDetailTitleTv, "alpha", startValue, endValue)
                .setDuration(200);
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                mPhotoDetailTitleTv.setVisibility(endState);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        animator.start();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_news_photo_detail;
    }

    @Override
    public void initInjector() {
        mActivityComponent.inject(this);
    }

    @Override
    public void initViews() {
        Bundle args = getArguments();
        mNewsPhotoDetail = args.getParcelable(Constants.PHOTO_DETAIL);
        createFragment(mNewsPhotoDetail);
        initViewPager();
        setPhotoDetailTitle(0);
    }

    private void createFragment(NewsPhotoDetail newsPhotoDetail) {
        mPhotoDetailFragmentList.clear();
        for (NewsPhotoDetail.Picture picture : newsPhotoDetail.getPictures()) {
            PhotoDetailFragment fragment = PhotoDetailFragment.newsInstance(picture.getImgSrc());
            mPhotoDetailFragmentList.add(fragment);
        }
    }

    private void initViewPager() {
        PhotoPagerAdapter photoPagerAdapter = new PhotoPagerAdapter(getSupportFragmentManager(),
                mPhotoDetailFragmentList);
        mViewpager.setAdapter(photoPagerAdapter);
        mViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setPhotoDetailTitle(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public void setPhotoDetailTitle(int position) {
        String title = getTitle(position);
        mPhotoDetailTitleTv.setText(
                getString(R.string.photo_detail_title, position + 1, mPhotoDetailFragmentList.size(), title));
    }

    private String getTitle(int position) {
        String title = mNewsPhotoDetail.getPictures().get(position).getTitle();
        if (title == null) {
            title = mNewsPhotoDetail.getTitle();
        }
        return title;
    }

    class PhotoPagerAdapter extends FragmentStatePagerAdapter {
        private List<PhotoDetailFragment> mFragmentList;

        public PhotoPagerAdapter(FragmentManager fm, List<PhotoDetailFragment> fragmentList) {
            super(fm);
            mFragmentList = fragmentList;
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

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

}