Scroll, hide and show header in Android
The basic layout structure to make the scroll up and hide the header, scroll down and show the header. The header has to be AppBarLayout, the root view has to be CoordinatorLayout, the scroll view could be an RecyclerView or NestedScrollView or other scroll views. The child view in the AppBarLayout has to set app:layout_scrollFlags="scroll|enterAlways"
, and the scroll view has to set the layout behavior to app:layout_behavior="@string/appbar_scrolling_view_behavior"
The RecyclerView list item layout, list_item.xml
The layout file activity_scrolling.xml
The Activity class
import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class ScrollingActivity extends AppCompatActivity { RecyclerView rvNumbers; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_scrolling); String [] numbers = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}; rvNumbers = (RecyclerView) findViewById(R.id.rv_numbers); rvNumbers.setLayoutManager(new LinearLayoutManager(this)); rvNumbers.setAdapter(new RecyclerViewAdapter(numbers)); } class RecyclerViewAdapter extends RecyclerView.Adapter{ String listData[]; public RecyclerViewAdapter(String data[]){ this.listData = data; } @Override public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false); RecyclerViewAdapter.ViewHolder vh = new RecyclerViewAdapter.ViewHolder(v); return vh; } @Override public void onBindViewHolder(RecyclerViewAdapter.ViewHolder holder, int position) { holder.tvNumber.setText(listData[position]); } @Override public int getItemCount() { return listData.length; } class ViewHolder extends RecyclerView.ViewHolder { public TextView tvNumber; public ViewHolder(View v) { super(v); tvNumber = (TextView) v.findViewById(R.id.tv_number); } } } }
Other Examples
Search within Codexpedia
Custom Search
Search the entire web
Custom Search
Related Posts