2015-11-07 31 views
20

我在NestedScrollview中有一個RecyclerView ..一切工作正常,除了一個thig。我在NestedScrollview中總共有三個視圖。前兩個是LinearLayout,然後是Recyclerview。當我運行我的應用程序活動不顯示頂部兩個佈局,它從Recyclerview頂部開始。NestedScrollview不會從頂部開始

如何它表明我的佈局:

How its show my layout

如何的想表明:

​​

,我是一個viewpager下加載此enite佈局和我viewpager是個孩子協調員佈局。

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.NestedScrollView 
android:id="@+id/scrollView" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@color/home_layout_background"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal"> 

     <com.daimajia.slider.library.SliderLayout 
      android:id="@+id/image_slider" 
      android:layout_width="match_parent" 
      android:layout_height="@dimen/image_slider_height" 
      android:layout_marginTop="@dimen/image_slider_top_margin" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="@dimen/popular_fragment_side_padding" 
     android:layout_marginRight="@dimen/popular_fragment_side_padding" 
     android:layout_marginTop="@dimen/popular_fragment_side_padding" 
     android:orientation="horizontal" 
     android:weightSum="4"> 

     <RelativeLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1"> 

      <RelativeLayout 
       android:id="@+id/free_delivery" 
       android:layout_width="76dp" 
       android:layout_height="76dp" 
       android:layout_centerInParent="true" 
       android:background="@color/white"> 

       <RelativeLayout 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerInParent="true"> 

        <ImageView 
         android:id="@+id/btnImageViewFreeDelivery" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_centerHorizontal="true" 
         android:background="@drawable/icon_free_delivery" /> 

        <TextView 
         android:id="@+id/btnTextFreeDelivery" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_below="@+id/btnImageViewFreeDelivery" 
         android:layout_centerHorizontal="true" 
         android:layout_marginTop="2dp" 
         android:text="@string/free_delivery_txt" 
         android:textColor="@color/popular_fragment_four_btn_txt" 
         android:textSize="@dimen/popular_fragment_four_btn_txt_size" /> 
       </RelativeLayout> 

      </RelativeLayout> 

     </RelativeLayout> 

     <RelativeLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1"> 

      <RelativeLayout 
       android:id="@+id/flash_deals" 
       android:layout_width="76dp" 
       android:layout_height="76dp" 
       android:layout_centerInParent="true" 
       android:background="@color/white"> 


       <RelativeLayout 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerInParent="true"> 

        <ImageView 
         android:id="@+id/btnImageViewFlashDeals" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_centerHorizontal="true" 
         android:background="@drawable/icon_flash_deals" /> 

        <TextView 
         android:id="@+id/btnTextFlashDeals" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_below="@+id/btnImageViewFlashDeals" 
         android:layout_centerHorizontal="true" 
         android:layout_marginTop="2dp" 
         android:text="@string/flash_deals_txt" 
         android:textColor="@color/popular_fragment_four_btn_txt" 
         android:textSize="@dimen/popular_fragment_four_btn_txt_size" /> 
       </RelativeLayout> 

      </RelativeLayout> 

     </RelativeLayout> 

     <RelativeLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1"> 

      <RelativeLayout 
       android:id="@+id/for_you" 
       android:layout_width="76dp" 
       android:layout_height="76dp" 
       android:layout_centerInParent="true" 
       android:background="@color/white"> 

       <RelativeLayout 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerInParent="true"> 

        <ImageView 
         android:id="@+id/btnImageViewForYou" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_centerHorizontal="true" 
         android:background="@drawable/icon_for_you" /> 

        <TextView 
         android:id="@+id/btnTextForYou" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_below="@+id/btnImageViewForYou" 
         android:layout_centerHorizontal="true" 
         android:layout_marginTop="2dp" 
         android:text="@string/for_you_txt" 
         android:textColor="@color/popular_fragment_four_btn_txt" 
         android:textSize="@dimen/popular_fragment_four_btn_txt_size" /> 
       </RelativeLayout> 


      </RelativeLayout> 
     </RelativeLayout> 

     <RelativeLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1"> 

      <RelativeLayout 
       android:id="@+id/shake_deals" 
       android:layout_width="76dp" 
       android:layout_height="76dp" 
       android:layout_centerInParent="true" 
       android:background="@color/white"> 

       <RelativeLayout 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerInParent="true"> 

        <ImageView 
         android:id="@+id/btnImageViewShakeDeals" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_centerHorizontal="true" 
         android:background="@drawable/icon_shake_deals" /> 

        <TextView 
         android:id="@+id/btnTextShakeDeals" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_below="@+id/btnImageViewShakeDeals" 
         android:layout_centerHorizontal="true" 
         android:layout_marginTop="2dp" 
         android:text="@string/shake_deals" 
         android:textColor="@color/popular_fragment_four_btn_txt" 
         android:textSize="@dimen/popular_fragment_four_btn_txt_size" /> 
       </RelativeLayout> 

      </RelativeLayout> 
     </RelativeLayout> 
    </LinearLayout> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recycler_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginLeft="@dimen/popular_fragment_side_padding" 
     android:layout_marginRight="@dimen/popular_fragment_side_padding" 
     android:layout_marginTop="18dp" 
     android:scrollbars="none" /> 

</LinearLayout> 

</android.support.v4.widget.NestedScrollView> 

我ViewPager佈局

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/main_content" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<android.support.v4.view.ViewPager 
    android:id="@+id/viewPager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/home_layout_background" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/appbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:layout_gravity="center" 
     android:background="@drawable/toolbar_background" 
     app:layout_scrollFlags="scroll|enterAlways"> 

     <TextView 
      android:id="@+id/toolbar_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:text="@string/title_home" 
      android:textColor="@color/toolbar_title_txt_color" 
      android:textSize="@dimen/toolbar_txt_size" 
      android:textStyle="bold" /> 

    </android.support.v7.widget.Toolbar> 


    <android.support.design.widget.TabLayout android:id="@+id/tabs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/white" 
     app:tabMode="scrollable" 
     app:tabTextColor="@color/tab_bar_txt" 
     app:tabIndicatorColor="@color/tab_bar_txt_selected" 
     app:tabSelectedTextColor="@color/tab_bar_txt_selected" 
     app:tabTextAppearance="@style/TabTextAppeareance"/> 

</android.support.design.widget.AppBarLayout> 



</android.support.design.widget.CoordinatorLayout> 
+0

請添加代碼。 –

+0

代碼已被添加到問題 – Suvro

回答

27

這是一個關於焦點問題。嘗試:

findViewById(R.id.recycler_view).setFocusable(false); 
findViewById(R.id.temp).requestFocus(); 

<?xml version="1.0" encoding="utf-8"?> 
    <android.support.v4.widget.NestedScrollView 
    android:id="@+id/scrollView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/home_layout_background"> 

    <LinearLayout 
     android:id="@+id/temp" 
     android:focusable="true" 
     android:focusableInTouchMode="true" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="horizontal"> 

      <com.daimajia.slider.library.SliderLayout 
       android:id="@+id/image_slider" 
       android:layout_width="match_parent" 
       android:layout_height="@dimen/image_slider_height" 
       android:layout_marginTop="@dimen/image_slider_top_margin" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="@dimen/popular_fragment_side_padding" 
      android:layout_marginRight="@dimen/popular_fragment_side_padding" 
      android:layout_marginTop="@dimen/popular_fragment_side_padding" 
      android:orientation="horizontal" 
      android:weightSum="4"> 

      <RelativeLayout 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1"> 

       <RelativeLayout 
        android:id="@+id/free_delivery" 
        android:layout_width="76dp" 
        android:layout_height="76dp" 
        android:layout_centerInParent="true" 
        android:background="@color/white"> 

        <RelativeLayout 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_centerInParent="true"> 

         <ImageView 
          android:id="@+id/btnImageViewFreeDelivery" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_centerHorizontal="true" 
          android:background="@drawable/icon_free_delivery" /> 

         <TextView 
          android:id="@+id/btnTextFreeDelivery" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_below="@+id/btnImageViewFreeDelivery" 
          android:layout_centerHorizontal="true" 
          android:layout_marginTop="2dp" 
          android:text="@string/free_delivery_txt" 
          android:textColor="@color/popular_fragment_four_btn_txt" 
          android:textSize="@dimen/popular_fragment_four_btn_txt_size" /> 
        </RelativeLayout> 

       </RelativeLayout> 

      </RelativeLayout> 

      <RelativeLayout 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1"> 

       <RelativeLayout 
        android:id="@+id/flash_deals" 
        android:layout_width="76dp" 
        android:layout_height="76dp" 
        android:layout_centerInParent="true" 
        android:background="@color/white"> 


        <RelativeLayout 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_centerInParent="true"> 

         <ImageView 
          android:id="@+id/btnImageViewFlashDeals" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_centerHorizontal="true" 
          android:background="@drawable/icon_flash_deals" /> 

         <TextView 
          android:id="@+id/btnTextFlashDeals" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_below="@+id/btnImageViewFlashDeals" 
          android:layout_centerHorizontal="true" 
          android:layout_marginTop="2dp" 
          android:text="@string/flash_deals_txt" 
          android:textColor="@color/popular_fragment_four_btn_txt" 
          android:textSize="@dimen/popular_fragment_four_btn_txt_size" /> 
        </RelativeLayout> 

       </RelativeLayout> 

      </RelativeLayout> 

      <RelativeLayout 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1"> 

       <RelativeLayout 
        android:id="@+id/for_you" 
        android:layout_width="76dp" 
        android:layout_height="76dp" 
        android:layout_centerInParent="true" 
        android:background="@color/white"> 

        <RelativeLayout 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_centerInParent="true"> 

         <ImageView 
          android:id="@+id/btnImageViewForYou" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_centerHorizontal="true" 
          android:background="@drawable/icon_for_you" /> 

         <TextView 
          android:id="@+id/btnTextForYou" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_below="@+id/btnImageViewForYou" 
          android:layout_centerHorizontal="true" 
          android:layout_marginTop="2dp" 
          android:text="@string/for_you_txt" 
          android:textColor="@color/popular_fragment_four_btn_txt" 
          android:textSize="@dimen/popular_fragment_four_btn_txt_size" /> 
        </RelativeLayout> 


       </RelativeLayout> 
      </RelativeLayout> 

      <RelativeLayout 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1"> 

       <RelativeLayout 
        android:id="@+id/shake_deals" 
        android:layout_width="76dp" 
        android:layout_height="76dp" 
        android:layout_centerInParent="true" 
        android:background="@color/white"> 

        <RelativeLayout 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_centerInParent="true"> 

         <ImageView 
          android:id="@+id/btnImageViewShakeDeals" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_centerHorizontal="true" 
          android:background="@drawable/icon_shake_deals" /> 

         <TextView 
          android:id="@+id/btnTextShakeDeals" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_below="@+id/btnImageViewShakeDeals" 
          android:layout_centerHorizontal="true" 
          android:layout_marginTop="2dp" 
          android:text="@string/shake_deals" 
          android:textColor="@color/popular_fragment_four_btn_txt" 
          android:textSize="@dimen/popular_fragment_four_btn_txt_size" /> 
        </RelativeLayout> 

       </RelativeLayout> 
      </RelativeLayout> 
     </LinearLayout> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/recycler_view" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginLeft="@dimen/popular_fragment_side_padding" 
      android:layout_marginRight="@dimen/popular_fragment_side_padding" 
      android:layout_marginTop="18dp" 
      android:scrollbars="none" /> 

    </LinearLayout> 

    </android.support.v4.widget.NestedScrollView> 
+1

它的工作原理。非常感謝:) – Suvro

+2

是的。他是對的。只需添加'mRecyclerView.setFoucsable(false);'。和原因是[這裏](http://stackoverflow.com/a/21235114/1710926) – IndieBoy

+0

感謝這一點。花了很長時間尋找。我有一個類似的佈局,有一個viewpager和一個drawerlayout。當我打開navigationdrawer時,奇怪的滾動問題變得更加棘手。現在它都是固定的:) – ruifn

22

我有同樣的問題,我固定它下面的方式。

android:descendantFocusability="blocksDescendants"添加到線性佈局這是嵌套滾動視圖的唯一孩子。

這允許嵌套滾動視圖從最上面開始,只需對代碼進行最少更改。

+2

這應該是被接受的答案,因爲不會改變視圖的風格。而'android:focusableInTouchMode =「true」'使得視圖看起來像點擊一樣。 – IgniteCoders

+0

也爲我工作 – ymerdrengene

3

此行只需添加到NestedScrollView孩子的xml:

android:focusableInTouchMode="true" 

問題就沒有了!

0

加入這一行NestedScrollView的子佈局

機器人:descendantFocusability = 「afterDescendants」