I want to display a View (example TextView
) over the RecyclerView
after scroll to a position (example: 3) so I use
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();
}
}
and
mRecyclerView.addItemDecoration(new HeaderItemDecoration());
I work but the problem is this TextView
will gone if I continue scroll. How to make this View always visible after I draw it? Any help or suggestion would be great appreciated.
Just change the if condition:
if(topChildPosition == 3) {
...
}
to:
if(topChildPosition >= 3) {
...
}
So the view will remain visible if you continue to scroll down.
If you want it remain visible even if you scroll back to top, just add a member variable to remember if the view is already shown, if it's shown, keep drawing it.