2012-11-04 47 views
0

我明白,爲了活動之間輕掃我需要使用FragmentPageAdapter在回答這個問題this添加周圍片段幀在ViewPager

不過,我試圖把一幀Fragments不走動。像畫框一樣,在屏幕邊緣停留的地方,當你滑動時,框架保持原位,而Fragments彼此之間滑動。基本上,掩蔽效應。我怎樣才能實現這個?

以我FragmentActivity的FragmentPagerAdapter我有不同的片段相比,所示的例子是一個列表片段(ArrayListFragment),我打電話。我使用一款通用的片段像這樣:

public static class TestFragment extends Fragment{ 
     /** 
     * Create a new instance of DetailsFragment, initialized to 
     * show the text at 'index'. 
     */ 
     public static TestFragment newInstance(int index) { 
      TestFragment f = new TestFragment(); 

      // Supply index input as an argument. 
      Bundle args = new Bundle(); 
      args.putInt("index", index); 
      f.setArguments(args); 

      return f; 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      View v = inflater.inflate(R.layout.mytext, container, false); 
      return v; 
     } 
    } 

中MyText佈局很簡單:

<?xml version="1.0" encoding="utf-8"?> 
<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:layout_height="fill_parent" 
     android:layout_width="fill_parent" 
     android:text="woohoo" 
     android:gravity="center"></TextView> 

</LinearLayout> 

回答

1

落實該框架的一種簡單方法是將ViewPagerFrameLayout或自定義一個RelativeLayoutView在它的上面:

<FrameLayout> 
    <android.support.v4.view.ViewPager 
     android:id="@+id/pager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <com.luksprog.ViewPagerOverlayFrame 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
</FrameLayout> 

在該自定義ViewonDraw方法你會畫出你想要的框架。下面的代碼將以此爲unswipeable紅框中ViewPager的邊緣附近:

public class ViewPagerOverlayFrame extends View { 

    private Paint mPaint; 

    public ViewPagerOverlayFrame(Context context) { 
     super(context); 
     mPaint = new Paint(); 
     mPaint.setColor(Color.RED); 
      mPaint.setStrokeWidth(60.0f);  
    } 

    public ViewPagerOverlayFrame(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     mPaint = new Paint(); 
     mPaint.setColor(Color.RED); 
     mPaint.setStrokeWidth(60.0f);  
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
     canvas.drawLine(0, 0, getMeasuredWidth(), 0, mPaint); 
     canvas.drawLine(getMeasuredWidth(), 0, getMeasuredWidth(), 
       getMeasuredHeight(), mPaint); 
     canvas.drawLine(getMeasuredWidth(), getMeasuredHeight(), 0, 
       getMeasuredHeight(), mPaint); 
     canvas.drawLine(0, getMeasuredHeight(), 0, 0, mPaint); 
    } 

} 

您可能需要一些填充添加到ViewPager因此它的內容沒有得到由幀被削去。

+0

嗯,這似乎隱藏在我的基礎片段的內容。另外,有沒有使用自定義圖像(.PNG)格式?像[this](http://www.juadgallery.com/images/v-frame.png)我需要創建一個自定義ImageView嗎? – codedawg82

+0

@ codedawg82是我的代碼「隱藏」下面的內容(這是我建議在ViewPager上設置填充)。你不能直接在'canvas'上繪製'png',但是你可以使用'BitmapFactory'類將其轉換爲'Bitmap'並使用它。我會打破4件,頂部,左側,右側,底部的'png'。 – Luksprog

+0

在ViewPager元素中添加填充不起作用。我已經在上面添加了我的片段代碼 - 也許這與問題有關?直接在顯示的自定義覆蓋圖之前添加TextView元素,正確顯示。但是,當textview放置在片段佈局中時,它不會(即使使用填充)。 – codedawg82