2016-09-26 78 views
2

自定義視圖你好,我是用可摺疊的佈局,我想,如下圖所示佈局根據我的滾動事件改變的行爲:可摺疊佈局與安卓

  1. 這是默認圖像與列表視圖和用戶滾動時,頂部佈局應該改變,看起來像第二個圖像。

First image

Second image

我應該如何實現這種使用可摺疊佈局行爲。

+0

什麼你到目前爲止已經試過? –

+0

回答

0

你只需要添加TabLayout工具欄下,像下面

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:background="@color/colorPrimary" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize"/> 
<android.support.design.widget.TabLayout 
    android:id="@+id/tabLayoutactivity_mainId" 
    android:layout_below="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"/> 
+0

我不想要標籤佈局。標籤佈局將保持不變,但工具欄正在被替換,請參閱第二張圖片。 –

+0

我無法理解你在說什麼..,我正在使用上述佈局作爲我的一個應用程序開發 – Sam

0

嘗試重寫此acording您的需要。您可以在CollapsableToolbar中設置製作可見/隱藏組件的佈局。

appbar = (AppBarLayout) findViewById(R.id.appbar); 
    header = (Toolbar) findViewById(R.id.toolbar); 

    appbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() { 
       private State state; 

       @Override 
       public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { 
        if (verticalOffset == 0) { 
         if (state != State.EXPANDED) { 
          collapsingToolbar.setTitleEnabled(false); 
          // do something here 
         } 
         state = State.EXPANDED; 
        } else if (Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()) { 
         if (state != State.COLLAPSED) { 
          collapsingToolbar.setTitle("All Jobs "); 
          collapsingToolbar.setTitleEnabled(true); 

    //     do something here  header.setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.actionbar_gradient)); 
    //      collapsingToolbar.setContentScrim(ContextCompat.getDrawable(this, R.drawable.actionbar_gradient)); 
          collapsingToolbar.setContentScrim(ContextCompat.getDrawable(this, R.drawable.actionbar_bg)); 

    //      collapsingToolbar.setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.actionbar_bg)); 

         } 
         state = State.COLLAPSED; 
        } else { 
         if (state == State.IDLE) { 
          collapsingToolbar.setTitleEnabled(false); 
          container.setVisibility(View.VISIBLE); 
         } 
         state = State.IDLE; 
        } 
       } 
      }); 

它爲我試試吧....