2015-11-06 41 views
0

我在下面viewpager並在初始狀態(當什麼也沒有滾動)一個listview,該viewpager顯示下一個和以前的10dp「預覽」只有一個項目項目(我已通過設置負頁邊距達到此目的:viewPager.setPageMargin(-48);)。我正在試圖做的是,在向下滾動listview變化viewpager和viewpager項目與列表視圖滾動動態尺寸

1)listview應該「推」viewpager,將其高度降低到某個點。在達到這一點時(一些minHeight的viewpager),listview應該正常滾動與上面的較小尺寸viewpager

2)下,並在viewpager根據前述項目應該拉內(向中央項目),並在最終狀態,項目viewpager應得到充分展示。 (下面的圖片來說明這一點)

滾動列表視圖應該做相反的事情。

我設法完成了我的任務的第(1)部分。下面的代碼

viewpagerlistviewFrameLayout像這裏面:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:showIn="@layout/activity_main" tools:context=".MainActivity"> 

<ListView 
    android:id="@+id/listView" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:dividerHeight="1dp" 
    android:divider="#000000" 
    android:scrollbars="none" /> 


<android.support.v4.view.ViewPager 
    android:id="@+id/pager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:clipToPadding="false" 
    android:background="#FFFFFF"/> 

</FrameLayout> 

我「假」的listviewviewpager下方加入了transaprent頭欣賞到listview和製造的高度headeviewviewpager相同。這裏的代碼片段:

screenWidth = // Screen width of the phone 
headerHeight = // Required height of the viewpager and the headerview 

headerView = inflater.inflate(R.layout.fake_list_header, listView, false); 
headerView.getLayoutParams().height = headerHeight; 
headerView.getLayoutParams().width = screenWidth; 
viewPager.getLayoutParams().height = headerHeight; 
viewPager.getLayoutParams().width = screenWidth; 
viewPager.setPageMargin(negativeMargin); 
listView.addHeaderView(headerView, null, false); 

// Other initializations and stuff 

fake_list_header佈局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

</LinearLayout> 

最後,我listviewOnScrollListener這需要調整viewpager高度依賴於由listview和停止滾動量的護理當我們達到viewpager的最小高度時:

OnScrollListener() { 
     @Override 
     public void onScrollStateChanged(AbsListView absListView, int i) { 


     } 

     @Override 
     public void onScroll(AbsListView absListView, int i, int i1, int i2) { 

      if (listview.getFirstVisiblePosition() == 0) { 
       View firstChild = listview.getChildAt(1); // 0th element is the fake headerview itself 
       int topY = 0; 
       if (firstChild != null) { 
        topY = firstChild.getTop(); 

        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
          RelativeLayout.LayoutParams.MATCH_PARENT, 
          RelativeLayout.LayoutParams.WRAP_CONTENT); 

        layoutParams.width = screenWidth; 
        layoutParams.height = topY; 

        if (topY < headerHeight && topY >= minHeight) { 
         // minHeight is the minimum height the viewpager takes, after this point it stops getting smaller 
         //And vice-versa with headerHeight taking care of the maximum height the viewpager can take 

         viewpager.setLayoutParams(layoutParams);       
        } 
       } 


      } 

     } 
    } 

我的任務的第(2)部分是我被卡住的地方(並且用完了想法),我試圖用改變pageMargin,但結果不好(也不認爲這是正確的方法來實現這樣的事情)。通過滾動調用setTranslationX來設置尋呼機中下一個(或前一個)視圖的X位置也不起作用。

這裏是什麼,我想實現一些嘲笑:

初始狀態(沒有滾動)

enter image description here

最終狀態(實現viewpager的了minHeight)

enter image description here

正在使用viewpagerlistview達到這樣的目標嗎?我想使用水平recyclerview而不是viewpager,但我需要「逐頁」滾動行爲的viewpager水平滾動/滑動的項目。任何建議表示歡迎

回答

0

在主佈局試試這個

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:showIn="@layout/activity_main" tools:context=".MainActivity"> 

    <ListView 
     android:id="@+id/listView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:dividerHeight="1dp" 
     android:layout_below="@+id/pager" 
     android:divider="#000000" 
     android:scrollbars="none" /> 


    <android.support.v4.view.ViewPager 
     android:id="@+id/pager" 
     android:layout_width="match_parent" 
     android:layout_height="150dp" 
     android:clipToPadding="false" 
     android:background="#FFFFFF"/> 

</RelativeLayout> 
+0

你能解釋一下這是如何工作的?你似乎正在設置一個高度viewpager –

+0

我也採取RelativeLayout代表框架佈局,並在列表視圖中使用屬性android:layout_below =「@ + id/pager」。 –

+0

但這並不能解決我試圖解決的問題 –