[英]Android different widths of layouts in ViewPager
我想使用Horizontal View Pager創建一些滾動視圖。 左視圖必須具有全屏寬度,但只有四分之一寬度(它將是Dolphin瀏覽器中的垂直面板)。 有可能這樣做嗎? 我在正確的布局中改變了android:layout_width
,但它沒有用。
我的代碼:
public class TestActivity extends FragmentActivity {
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_view);
MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager pager = (ViewPager) findViewById(R.id.panelPager);
pager.setAdapter(adapter);
pager.setCurrentItem(0);
}
}
main_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/panelPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
MyPagerAdapter.java
public class MyPagerAdapter extends PagerAdapter {
@Override
public int getCount() {
return 2;
}
@Override
public Object instantiateItem(final View collection, final int position) {
LayoutInflater inflater =
(LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0:
resId = R.layout.left;
break;
case 1:
resId = R.layout.right;
break;
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(final View arg0, final int arg1, final Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(final View arg0, final Object arg1) {
return arg0 == ((View) arg1);
}
left.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LEFT" />
</LinearLayout>
right.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="50dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/light_blue" >
<TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="RIGHT"/>
</LinearLayout>
檢查墨菲對這個問題的回答。 您需要在PagerAdapter類上覆蓋PagerAdapter的getPageWidth()
方法,例如:
@Override
public float getPageWidth(int page) {
if(page==0) {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
return (float)LEFT_FRAGMENT_PIXEL_WIDTH / size.x;
}
else
return super.getPageWidth(page);
}
阿德里安完全正確。 ViewPager並非旨在將下一篇文章的一部分顯示為預覽/預告片,但它可以執行此操作。 在我的ViewPagerCursorAdapter extends PagerAdapter
類,我運行這個:
public Object instantiateItem(View collection, final int position) {
cursor.moveToPosition(position);
View newView = getPageView(cursor, collection);
if (cursor.getCount() > 1) {
((ViewPager)collection).setPageMargin(-overlapMargin);
if (! cursor.isLast()) { newView.setPadding(0, 0, overlapMargin, 0); }
}
((ViewPager) collection).addView(newView);
return newView; //returns the object reference as the tag for identification.
}
你會遇到一個奇怪的z重疊問題。 訣竅是后台或者只適用於ViewPager的背景或將其應用到NewView的里面 ,你剛才設置的襯墊的視圖。 看起來很好,效果很好。
查看ViewPager的源代碼,這不是它的設計目的; 它使用自己的寬度來計算滾動距離等,明確假設所有孩子都具有與ViewPager本身相同的寬度。
但是,對於您的具體情況, 可能會有一個hacky解決方法。 您可以指定相鄰頁面之間的邊距,此邊距可以為負。 如果ViewPager的子項的Z順序合適,這可能會給出您想要的結果。 試一試,看看它是否能滿足您的需求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.