Search in sources :

Example 1 with StickyRecyclerHeadersTouchListener

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();
        }
    });
}
Also used : ToggleButton(android.widget.ToggleButton) Handler(android.os.Handler) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) StickyRecyclerHeadersTouchListener(com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersTouchListener) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) View(android.view.View) StickyRecyclerHeadersDecoration(com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersDecoration) Button(android.widget.Button) ToggleButton(android.widget.ToggleButton) RecyclerView(android.support.v7.widget.RecyclerView)

Aggregations

Handler (android.os.Handler)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 View (android.view.View)1 Button (android.widget.Button)1 TextView (android.widget.TextView)1 ToggleButton (android.widget.ToggleButton)1 StickyRecyclerHeadersDecoration (com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersDecoration)1 StickyRecyclerHeadersTouchListener (com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersTouchListener)1