yalantis ToDoList

repository·master·Indexed 23 days ago

https://github.com/yalantis/todolist

An Android library for creating interactive To-Do list UI components with smooth micro-transitions for adding, removing, and reordering items. Features include BatRecyclerView, BatAdapter, BatModel, and BatItemAnimator for advanced list animations. Requires Android SDK 16+.

Tokens
1.7K
Snippets
6
Records
6
Agent score
32%

What's inside todolist

  1. Enable smooth animations with BatItemAnimator

    master

    For advanced list animations, use BatItemAnimator. To work correctly, the animator instance must be passed to both the BatRecyclerView and the BatAdapter. Additionally, you must call mAnimator.setPosition(to) inside the BatListener.move() callback.

    mAnimator = new BatItemAnimator();
    mAdapter = new BatAdapter(mGoals = new ArrayList<BatModel>() {{ ... }}, mListener, mAnimator);
    mRecyclerView.getView().setItemAnimator(mAnimator);
    
    // Inside BatListener.move(int from, int to):
    @Override
    public void move(int from, int to) {
         if (from >= 0 && to >= 0) {
               mAnimator.setPosition(to);
               // ... update data and notify adapter ...
         }
    }
  2. Initialize BatRecyclerView and ItemTouchHelper

    master

    To connect the components, set up the LinearLayoutManager and BatAdapter on the BatRecyclerView's internal view. Use ItemTouchHelper with a BatCallback to enable drag-and-drop functionality, and register your BatListener with the recycler view.

    mRecyclerView.getView().setLayoutManager(new LinearLayoutManager(this));
    mRecyclerView.getView().setAdapter(mAdapter);
    
    ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new BatCallback(this));
    itemTouchHelper.attachToRecyclerView(mRecyclerView.getView());
    mRecyclerView.setAddItemListener(mListener);
  3. Install ToDoList via JitPack

    master

    To use the ToDoList library in your Android project, follow these two steps:

    1. Add the JitPack repository to your root build.gradle file.
    2. Add the library dependency to your app-level build.gradle file.

    Requirements:

    • Android SDK 16+
    // In root build.gradle
    allprojects {
    	repositories {
    	...
    	maven { url "https://jitpack.io" }
    	}
    }
    
    // In app build.gradle
    dependencies {
    	compile 'com.github.yalantis:todolist:v1.0.1'
    }
  4. Add BatRecyclerView to your layout

    master

    Include the com.yalantis.beamazingtoday.ui.widget.BatRecyclerView in your XML layout file. You can customize the appearance using the following attributes:

    • app:add_button_color: A drawable selector for the add button.
    • app:hint: A string resource for the hint text.
    • app:radio_button_res: A drawable selector for the radio button.
    <com.yalantis.beamazingtoday.ui.widget.BatRecyclerView
        android:id `@+id/bat_recycler_view`
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        app:add_button_color="@drawable/selector_button_add"
        app:hint="@string/str_add_goal"
        app:radio_button_res="@drawable/selector_radio_button" />
  5. Use BatAdapter with BatModel

    master

    To display items, create a BatAdapter. The adapter requires a list of items that implement the BatModel interface and an instance of BatListener. You can also set an OnItemClickListener to handle clicks on individual items.

    // Your model must implement BatModel
    mAdapter = new BatAdapter(mGoals = new ArrayList<BatModel>() {{
          add(new Goal("first"));
          add(new Goal("second"));
    }}, mListener);
            
    mAdapter.setOnItemClickListener(new OnItemClickListener() {
          @Override
          public void onClick(BatModel item, int position) {
                Toast.makeText(ExampleActivity.this, item.getText(), Toast.LENGTH_SHORT).show();
          }
    });
  6. Implement BatListener for list operations

    master

    The BatListener interface handles user interactions for adding, deleting, and moving items. You must implement these methods to update your data source and notify the adapter of changes using AnimationType.

    private BatListener mListener = new BatListener() {
        @Override
        public void add(String string) {
            mGoals.add(0, new Goal(string));
            mAdapter.notify(AnimationType.ADD, 0);
        }
    
        @Override
        public void delete(int position) {
            mGoals.remove(position);
            mAdapter.notify(AnimationType.REMOVE, position);
        }
    
        @Override
        public void move(int from, int to) {
            if (from >= 0 && to >= 0) {
                // If using BatItemAnimator, update its position
                mAnimator.setPosition(to);
                
                BatModel model = mGoals.get(from);
                mGoals.remove(model);
                mGoals.add(to, model);
                mAdapter.notify(AnimationType.MOVE, from, to);
    
                if (from == 0 || to == 0) {
                    mRecyclerView.getView().scrollToPosition(Math.min(from, to));
                }
            }
        }
    };