MyTetra Share
Делитесь знаниями!
CrimePagerActivity.java
29.04.2017
16:01
Раздел: Android - books - AndroidProgramming2e - 14 Базы данных SQLite
CrimePagerActivity.java

CrimePagerActivity.java

package com.bignerdranch.android.criminalintent; 
 
import android.content.Context; 
import android.content.Intent; 
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.app.AppCompatActivity; 
 
import java.util.List; 
import java.util.UUID; 
 
public class CrimePagerActivity extends AppCompatActivity { 
    private static final String EXTRA_CRIME_ID = 
            "com.bignerdranch.android.criminalintent.crime_id"; 
 
    private ViewPager mViewPager; 
    private List<Crime> mCrimes; 
 
    public static Intent newIntent(Context packageContext, UUID crimeId) { 
        Intent intent = new Intent(packageContext, CrimePagerActivity.class); 
        intent.putExtra(EXTRA_CRIME_ID, crimeId); 
        return intent; 
    } 
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_crime_pager); 
 
        UUID crimeId = (UUID) getIntent() 
                .getSerializableExtra(EXTRA_CRIME_ID); 
 
        mViewPager = (ViewPager) findViewById(R.id.activity_crime_pager_view_pager); 
 
        mCrimes = CrimeLab.get(this).getCrimes(); 
        FragmentManager fragmentManager = getSupportFragmentManager(); 
        mViewPager.setAdapter(new FragmentStatePagerAdapter(fragmentManager) { 
 
            @Override 
            public Fragment getItem(int position) { 
                Crime crime = mCrimes.get(position); 
                return CrimeFragment.newInstance(crime.getId()); 
            } 
 
            @Override 
            public int getCount() { 
                return mCrimes.size(); 
            } 
        }); 
 
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 
            @Override 
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } 
 
            @Override 
            public void onPageSelected(int position) { 
                Crime crime = mCrimes.get(position); 
                if (crime.getTitle() != null) { 
                    setTitle(crime.getTitle()); 
                } 
            } 
 
            @Override 
            public void onPageScrollStateChanged(int state) { } 
        }); 
 
        for (int i = 0; i < mCrimes.size(); i++) { 
            if (mCrimes.get(i).getId().equals(crimeId)) { 
                mViewPager.setCurrentItem(i); 
                break; 
            } 
        } 
    } 
} 

 
MyTetra Share v.0.52
Яндекс индекс цитирования