Vorrei fornire un aggiornamento a chiunque desideri la stessa funzione. Sono stati fatti molti progressi per implementare questa funzionalità finora e ora la vista funziona esattamente come ne abbiamo bisogno.
ViewPager ha un metodo denominato setPageMargin(). Questo metodo può ricevere un valore negativo che farà sì che i frammenti/viste si sovrappongano l'un l'altro. Per arrivare al layout desiderato, abbiamo prima calcolato in modo dinamico i margini sinistro e destro di una percentuale dello schermo. Questo può essere fatto anche in modo statico, ma dato che ci rivolgeremo a una gamma di schermi di dimensioni diverse, questo sembra essere l'approccio migliore.
Successivamente impostiamo il margine della pagina di ViewPager su 2 volte la dimensione dei margini laterali. Questo fa sì che le viste si riavvicinino. Tuttavia, in questo momento, ci sarà più di una vista visualizzata dal ViewPager.
Tutto quello che resta da fare è applicare una trasformazione (scala) alle viste a sinistra e destra (Android 3.0+) o aggiungere alcuni margini attorno a loro per ridurle alla giusta dimensione (pre 3.0) .
OnPageChangeListener.onPageScrolled() può essere utilizzato per tracciare lo scorrimento di ViewPager. È possibile ottenere una trasformazione liscia . Il codice è simile al seguente:
private OnPageChangeListener onPageChangeListener = new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// positionOffset varies from 0 to 1 and indicates how far the view is
// from the center of the ViewPager. When a view is selected (centered), this
// value is 0.
// Fades the text in an out while the ViewPager scrolls from one view to another.
// On our final design the text views are fixed to the center of the screen and
// do not scroll along with the views.
if (positionOffset < 0.5) {
setTextViewAlpha(1 - positionOffset * 2);
} else {
setTextViewAlpha((positionOffset - 0.5f) * 2);
}
// It's surprisingly complicated to get the current object being displayed by
// a ViewPager (there's no getCurrentObject method). ScaleableFragment is just
// a custom class to allow an adapter to cache it internally and also correctly
// manage a fragment's lifecycle and restore from a saved state.
ScaleableFragment sampleFragment = (ScaleableFragment) ((ShowHeroShotImageFragmentPagerAdapter) pager
.getAdapter()).getItem(position);
// Calculates by how much the current view will be scaled down. The RATIO_SCALE
// is 0.3 in our case, which makes the side views 70% of the size of the
// center view. When centered, the scale will be 1. When
// fully scrolled, the scale will be 0.7.
float scale = 1 - (positionOffset * RATIO_SCALE);
// Just a shortcut to findViewById(R.id.image).setScale(scale);
sampleFragment.scaleImage(scale);
// Now, if not in the last element, scale the next one up (in opposite direction).
if (position + 1 < pager.getAdapter().getCount()) {
sampleFragment = (ScaleableFragment) ((ShowHeroShotImageFragmentPagerAdapter) pager.getAdapter())
.getItem(position + 1);
scale = positionOffset * RATIO_SCALE + (1 - RATIO_SCALE);
sampleFragment.scaleImage(scale);
}
}
// Once scrolling is done. Make sure the views are in the right scale (1 for center,
// 0.7 for sides). Required as the onPageScrolled() method does not guarantee it
// will interpolate to 1.0 precisely.
@Override
public void onPageScrollStateChanged(int state) {
if (state == ViewPager.SCROLL_STATE_IDLE) {
setTextViewAlpha(1);
ScaleableFragment sampleFragment = (ScaleableFragment) ((ShowHeroShotImageFragmentPagerAdapter) pager
.getAdapter()).getItem(pager.getCurrentItem());
sampleFragment.scaleImage(1);
sampleFragment.enableClicks();
if (pager.getCurrentItem() > 0) {
sampleFragment = (ScaleableFragment) ((ShowHeroShotImageFragmentPagerAdapter) pager.getAdapter())
.getItem(pager.getCurrentItem() - 1);
sampleFragment.scaleImage(1 - RATIO_SCALE);
sampleFragment.disableClicks();
}
if (pager.getCurrentItem() + 1 < pager.getAdapter().getCount()) {
sampleFragment = (ScaleableFragment) ((ShowHeroShotImageFragmentPagerAdapter) pager.getAdapter())
.getItem(pager.getCurrentItem() + 1);
sampleFragment.scaleImage(1 - RATIO_SCALE);
sampleFragment.disableClicks();
}
}
}
};
Questo è tutto. Non ho pubblicato la soluzione completa, ma spero che questo sia sufficiente per far iniziare qualcun altro.
P.S. Su 3.0+ abilita l'accelerazione hardware. Senza di esso, lo scorrimento appariva instabile su una scheda Samsung Galaxy 10.1.
[Stai descrivendo questo?] (Http://stackoverflow.com/questions/9816371/how-to-use-multi-pane-layouts-with-viewpager) – adneal
Close, eccetto che la vista selezionata non dovrebbe occupare l'intero schermo. Vedi modifica sopra con un'immagine che spiega il layout. – AngraX