content: package info.meizi.ui.largepic;
  
  import android.content.Intent;
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentStatePagerAdapter;
  import android.support.v4.app.SharedElementCallback;
  import android.support.v4.view.ViewPager;
  import android.support.v7.widget.Toolbar;
  import android.view.View;
  
  import java.util.List;
  import java.util.Map;
  
  import butterknife.Bind;
  import butterknife.ButterKnife;
  import info.meizi.R;
  import info.meizi.base.BaseActivity;
  import info.meizi.bean.Content;
  import io.realm.Realm;
  
  /**
   * Created by Mr_Wrong on 15/10/6.
   * 查看大图的
   */
  public class LargePicActivity extends BaseActivity {
      private static final int SYSTEM_UI_BASE_VISIBILITY = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
              | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
              | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
      private static final int SYSTEM_UI_IMMERSIVE = View.SYSTEM_UI_FLAG_IMMERSIVE
              | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
              | View.SYSTEM_UI_FLAG_FULLSCREEN;
  
      @Bind(R.id.large_toolbar)
      Toolbar mToolbar;
      @Bind(R.id.pager)
      ViewPager mPager;
      private int index;
      private Realm realm;
      private List<Content> images;
      private PagerAdapter adapter;
      private String groupid;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.large_pic);
          ButterKnife.bind(this);
          initviews();
      }
  
      private void initviews() {
          mToolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
          mToolbar.setTitle("Meizi");
          mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  supportFinishAfterTransition();
              }
          });
  
          index = getIntent().getIntExtra("index", 0);
          groupid = getIntent().getStringExtra("groupid");
  
          realm = Realm.getInstance(this);
          images = Content.all(realm, groupid);
  
          adapter = new PagerAdapter();
  
          mPager.setAdapter(adapter);
          mPager.setCurrentItem(index);
  
  
          setEnterSharedElementCallback(new SharedElementCallback() {
              @Override
              public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
                  Content image = images.get(mPager.getCurrentItem());
                  LargePicFragment fragment = (LargePicFragment) adapter.instantiateItem(mPager, mPager.getCurrentItem());
                  sharedElements.clear();
                  sharedElements.put(image.getUrl(), fragment.getSharedElement());
              }
          });
      }
  
      @Override
      public void supportFinishAfterTransition() {
          Intent data = new Intent();
          data.putExtra("index", mPager.getCurrentItem());
          setResult(RESULT_OK, data);
  //        showSystemUi();
          super.supportFinishAfterTransition();
      }
  
      public void toggleToolbar() {
          if (mToolbar.getTranslationY() == 0) {
              hideSystemUi();
          } else {
              showSystemUi();
          }
      }
  
      private void showSystemUi() {
          mPager.setSystemUiVisibility(SYSTEM_UI_BASE_VISIBILITY);
          mToolbar.animate()
                  .translationY(0)
                  .setDuration(400)
                  .start();
      }
  
      private void hideSystemUi() {
          mPager.setSystemUiVisibility(SYSTEM_UI_BASE_VISIBILITY | SYSTEM_UI_IMMERSIVE);
          mToolbar.animate()
                  .translationY(-mToolbar.getHeight())
                  .setDuration(400)
                  .start();
      }
  
      private class PagerAdapter extends FragmentStatePagerAdapter {
  
          public PagerAdapter() {
              super(getSupportFragmentManager());
          }
  
          @Override
          public int getCount() {
              return images.size();
          }
  
          @Override
          public Fragment getItem(int position) {
              return LargePicFragment.newFragment(
                      images.get(position).getUrl(),
                      position == index);
          }
  
      }
  }
  