use of com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersTouchListener in project sticky-headers-recyclerview by timehop.
the class MainActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
Button button = (Button) findViewById(R.id.button_update);
final ToggleButton isReverseButton = (ToggleButton) findViewById(R.id.button_is_reverse);
// Set adapter populated with example dummy data
final AnimalsHeadersAdapter adapter = new AnimalsHeadersAdapter();
adapter.add("Animals below!");
adapter.addAll(getDummyDataSet());
recyclerView.setAdapter(adapter);
// Set button to update all views one after another (Test for the "Dance")
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Handler handler = new Handler(Looper.getMainLooper());
for (int i = 0; i < adapter.getItemCount(); i++) {
final int index = i;
handler.postDelayed(new Runnable() {
@Override
public void run() {
adapter.notifyItemChanged(index);
}
}, 50);
}
}
});
// Set layout manager
int orientation = getLayoutManagerOrientation(getResources().getConfiguration().orientation);
final LinearLayoutManager layoutManager = new LinearLayoutManager(this, orientation, isReverseButton.isChecked());
recyclerView.setLayoutManager(layoutManager);
// Add the sticky headers decoration
final StickyRecyclerHeadersDecoration headersDecor = new StickyRecyclerHeadersDecoration(adapter);
recyclerView.addItemDecoration(headersDecor);
// Add decoration for dividers between list items
recyclerView.addItemDecoration(new DividerDecoration(this));
// Add touch listeners
StickyRecyclerHeadersTouchListener touchListener = new StickyRecyclerHeadersTouchListener(recyclerView, headersDecor);
touchListener.setOnHeaderClickListener(new StickyRecyclerHeadersTouchListener.OnHeaderClickListener() {
@Override
public void onHeaderClick(View header, int position, long headerId) {
Toast.makeText(MainActivity.this, "Header position: " + position + ", id: " + headerId, Toast.LENGTH_SHORT).show();
}
});
recyclerView.addOnItemTouchListener(touchListener);
recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
adapter.remove(adapter.getItem(position));
}
}));
adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onChanged() {
headersDecor.invalidateHeaders();
}
});
isReverseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isChecked = isReverseButton.isChecked();
isReverseButton.setChecked(isChecked);
layoutManager.setReverseLayout(isChecked);
adapter.notifyDataSetChanged();
}
});
}
Aggregations