Skip to content
jimgswang edited this page Jun 12, 2015 · 11 revisions

What?

SwipeAdapter is a serials of helper class to save your time. It can help you to save and restore SwipeLayout status (OPEN | CLOSE) in ListView, GridView etc.

Why?

As you know when you want to use ListView, GridView, you need an Adapter to provide data for ListView, GridView.

While, there is a problem. How to restore the SwipeLayout status (OPEN | CLOSE) when user scolling ? For making SwipeLayout easy to use, I made a serials of SwipeAdapter.

BaseSwipeAdapter usage

Remember:

However, it's a little bit different from BaseAdapter. You don't need to override getView() method, there are 3 new methods you need to override.

//return the `SwipeLayout` resource id in your listview | gridview item layout.
public abstract int getSwipeLayoutResourceId(int position);

//render a new item layout.
public abstract View generateView(int position, ViewGroup parent);

/*fill values to your item layout returned from `generateView`.
  The position param here is passed from the BaseAdapter's 'getView()*/
public abstract void fillValues(int position, View convertView);

The SwipeAdapter will automatically maintain the convertView. You can check out the source code to have a deeper understanding.

Example:

R.id.grid_item

R.id.swipe

ATTENTION: Never bind listeners or fill values in generateView. Just generate the view and do everything else in fillValues (e.g using your holder class) See issues #14 and #17

public class GridViewAdapter extends BaseSwipeAdapter {

    private Context mContext;

    public GridViewAdapter(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public int getSwipeLayoutResourceId(int position) {
        return R.id.swipe;
    }
    
    //ATTENTION: Never bind listener or fill values in generateView.
    //           You have to do that in fillValues method.
    @Override
    public View generateView(int position, ViewGroup parent) {
        return LayoutInflater.from(mContext).inflate(R.layout.grid_item, null);
    }

    @Override
    public void fillValues(int position, View convertView) {
        TextView t = (TextView)convertView.findViewById(R.id.position);
        t.setText((position + 1 )+".");
    }

    @Override
    public int getCount() {
        return 50;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
}

Example

ArraySwipeAdapter, CursorSwipeAdapter, SimpleCursorAdapter usage

These three adapters are just like ArrayAdapter<T>, CursorAdapter, SimpleCursorAdapter

All of these 3 classes are introduced after v1.1.5, need to be override one method:

//return the SwipeLayout resource id in the layout.
public int getSwipeLayoutResourceId(int position);

Example


Well done! You just finished the entire tutorial of SwipeLayout!

Now, enjoy it!