2017-04-05 90 views
-2

我正在設計一個佈局,其中行 佈局的下方有一個recyclerView,當我向上滾動時佈局應平行減少並固定在某個位置。第一個圖像在滾動之前,第二張圖片在滾動後。滾動時佈局高度應該減少平行

before Scrollingenter image description here

+0

感謝的答案,但我必須做出可點擊「同步」按鈕 –

+0

有偵聽單擊回收站的視差頭查看'void onClick(View v,int position);' –

回答

0

您可以使用這些真棒庫,將滿足您的工作。

1-對於ListView的

ParallaxListView

對於RecyclerView

Android Parallax Recycleview

執行情況回收站查看

步驟1。在JitPack庫添加到您的構建文件

repositories { 
    maven { 
     url "https://jitpack.io" 
    } 
} 

步驟2。添加依賴

dependencies { 
    compile 'com.github.kanytu:android-parallax-recyclerview:v1.7' 
} 

用法 (示例項目 - https://github.com/kanytu/example-parallaxrecycler

  • 創建對象列表,並把它傳遞給ParallaxRecyclerAdapter

    List<String> myContent = new ArrayList<String>(); // or another object list 
    ParallaxRecyclerAdapter<String> adapter = new ParallaxRecyclerAdapter<String>(content) { 
        @Override 
        public void onBindViewHolderImpl(RecyclerView.ViewHolder viewHolder, ParallaxRecyclerAdapter<String> adapter, int i) { 
         // If you're using your custom handler (as you should of course) 
         // you need to cast viewHolder to it. 
         ((MyCustomViewHolder) viewHolder).textView.setText(myContent.get(i)); // your bind holder routine. 
        } 
    
        @Override 
        public RecyclerView.ViewHolder onCreateViewHolderImpl(ViewGroup viewGroup, final ParallaxRecyclerAdapter<String> adapter, int i) { 
         // Here is where you inflate your row and pass it to the constructor of your ViewHolder 
         return new MyCustomViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.myRow, viewGroup, false)); 
        } 
    
        @Override 
        public int getItemCountImpl(ParallaxRecyclerAdapter<String> adapter) { 
         // return the content of your array 
         return myContent.size(); 
        } 
    };` 
    
  • 構造函數現在我們設置視差頭。您還需要傳遞RecyclerView以實現滾動偵聽器。

    myAdapter.setParallaxHeader(LayoutInflater.from(this).inflate(R.layout.myParallaxView, myRecycler, false), myRecyclerView); 
    

還有一些其他的監聽器可以實現:

// Event triggered when you click on a item of the adapter. 
void onClick(View v, int position); 

// Event triggered when the parallax is being scrolled. 
void onParallaxScroll(float percentage, float offset, View parallax); 
+0

[只有鏈接的答案不好...](https://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links -elsewhere-really-good-answers) – 2Dee

+0

@ 2Dee code now now now –

+0

removed my do wnvote,謝謝你改進你的答案。 – 2Dee