2017-07-28 143 views
0

後我想顯示的位置滾動後視圖(實施例TextView)在RecyclerView(例如:3),所以我使用RecyclerView onDrawOver視圖中消失滾動

public class HeaderItemDecoration extends RecyclerView.ItemDecoration { 

    @Override 
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { 
     super.onDrawOver(c, parent, state); 
     int topChildPosition = parent.getChildAdapterPosition(parent.getChildAt(0)); 

     if(topChildPosition == 3) { 
      Log.i("TAG", "draw header"); 
      TextView textView = new TextView(parent.getContext()); 
      textView.setText("bbdasdasd"); 
      textView.setBackgroundColor(Color.RED); 
      textView.layout(0, 0, 100, 100); 
      drawText(c, textView); 
     } 
    } 

    private void drawText(Canvas c, View header) { 
     c.save(); 
     c.translate(0, 0); 
     header.draw(c); 
     c.restore(); 
    } 
} 

mRecyclerView.addItemDecoration(new HeaderItemDecoration()); 

我工作,但問題是這個TextView將消失,如果我繼續滾動。繪製後如何使該視圖始終可見?任何幫助或建議將不勝感激。

enter image description here

回答

1

只要改變,如果條件:

if(topChildPosition == 3) { 
    ... 
} 

到:

if(topChildPosition >= 3) { 
    ... 
} 

所以,如果你繼續向下滾動視圖將保持可見。

如果你希望它保持可見,即使你回滾到頂部,只需添加一個成員變量來記住視圖是否已經顯示,如果顯示,請繼續繪製。

+0

非常感謝。我明白它爲什麼會消失 –

相關問題