Android + ListView +滑动元素

时间:2014-09-18 19:31:02

标签: java android listview adapter baseadapter

我使用this library作为我的项目,一切正常(刷卡元素),但我的问题是 - 是否可以通过点击某个ListView元素开始滑动动画?

和第二个问题:

如何为自定义对象实现timedSwipeUndo?

这是我的ListView

代码
final ArrayAdapter<Product> adapter = new MyListAdapter(this,R.layout.text_view,productsDef);
        SimpleSwipeUndoAdapter simpleSwipeUndoAdapter = new SimpleSwipeUndoAdapter(adapter, this, new MyOnDismissCallback(adapter));
        AlphaInAnimationAdapter animAdapter = new AlphaInAnimationAdapter(simpleSwipeUndoAdapter);
        animAdapter.setAbsListView(listView);
        assert animAdapter.getViewAnimator() != null;
        animAdapter.getViewAnimator().setInitialDelayMillis(INITIAL_DELAY_MILLIS);
        listView.setAdapter(animAdapter);
        /* Enable drag and drop functionality */
        listView.enableDragAndDrop();
        listView.setDraggableManager(new           TouchViewDraggableManager(R.id.list_row_draganddrop_touchview));
        listView.setOnItemMovedListener(new MyOnItemMovedListener(adapter));
        listView.setOnItemLongClickListener(new MyOnItemLongClickListener(listView));
        /* Enable swipe to dismiss */
        listView.enableSimpleSwipeUndo();
        /* Add new items on item click */
        //listView.setOnItemClickListener(new MyOnItemClickListener(listView));

在Doc中 - 对于timedUndoAdapter使用TimedUndoAdapter。但是当我尝试创建适配器时,我有错误:

Error:(249, 51) error: constructor TimedUndoAdapter in class TimedUndoAdapter cannot be applied to given types;
required: V,Context,OnDismissCallback
found: ArrayAdapter<Product>,DefaultProductsListUserEdit,DefaultProductsListUserEdit.MyOnDismissCallback
reason: inferred type does not conform to declared bound(s)
inferred: ArrayAdapter<Product>
bound(s): BaseAdapter,UndoAdapter
where V is a type-variable:
V extends BaseAdapter,UndoAdapter declared in constructor <V>TimedUndoAdapter(V,Context,OnDismissCallback)

我不能将TimedAdapter用于自定义对象,或者我必须覆盖某些方法?

这是myAdapter:

public class MyListAdapter extends ArrayAdapter<Product> implements UndoAdapter {

    private final Context mContext;
    HashMap<Product, Integer> mIdMap = new HashMap<Product, Integer>();
    ArrayList<Product> products = new ArrayList<Product>();
    final int INVALID_ID = -1;
    LayoutInflater lInflater;

    public MyListAdapter(Context context, int textViewResourceId, List<Product> prod) {
        //super(context, textViewResourceId, prod);
        super(prod);
        lInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mContext = context;
        for (int i = 0; i < prod.size(); i++) {
            //add(prod.get(i));
            mIdMap.put(prod.get(i),i);
        }
    }

    @Override
    public long getItemId(final int position) {
        //return getItem(position).hashCode();
        Product item = (Product) getItem(position);
        return mIdMap.get(item);
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getView(final int position, final View convertView, final ViewGroup parent) {
        //TextView view = (TextView) convertView;
        View view = convertView;
        if (view == null) {
            //view = (TextView) LayoutInflater.from(mContext).inflate(R.layout.list_row, parent, false);
            view = lInflater.inflate(R.layout.item, parent, false);
        }

        Product p = getItem(position);

        //view.setText(getItem(position).getProductName());
        ((TextView) view.findViewById(R.id.tvDescr)).setText(p.getProductName());

        return view;
    }

    @NonNull
    @Override
    public View getUndoView(final int position, final View convertView, @NonNull final ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            //view = LayoutInflater.from(mContext).inflate(R.layout.undo_row, parent, false);
            view = lInflater.inflate(R.layout.undo_row, parent, false);
        }
        return view;
    }

    @NonNull
    @Override
    public View getUndoClickView(@NonNull final View view) {
        return view.findViewById(R.id.undo_row_undobutton);
    }

    public View getHeaderView(final int position, final View convertView, final ViewGroup parent) {
        TextView view = (TextView) convertView;
        //View view = convertView;
        if (view == null) {
            //view = (TextView) LayoutInflater.from(mContext).inflate(R.layout.list_header, parent, false);
            //view = lInflater.inflate(R.layout.list_header, parent, false);
        }

        //view.setText(mContext.getString(R.string.header, getHeaderId(position)));

        return view;
    }

    public long getHeaderId(final int position) {
        return position / 10;
    }
}

和类MyOnDismissCallback(适配器):

private class MyOnDismissCallback implements OnDismissCallback {

        private final ArrayAdapter<Product> mAdapter;

        @Nullable
        private Toast mToast;

        MyOnDismissCallback(final ArrayAdapter<Product> adapter) {
            mAdapter = adapter;
        }

        @Override
        public void onDismiss(@NonNull final ViewGroup listView, @NonNull final int[] reverseSortedPositions) {
            for (int position : reverseSortedPositions) {
                mAdapter.remove(position);
            }

            if (mToast != null) {
                mToast.cancel();
            }
            mToast = Toast.makeText(
                    DefaultProductsListUserEdit.this,
                    "REMOVED ITEM",
                    Toast.LENGTH_LONG
            );
            mToast.show();
        }
    }
提前谢谢!

0 个答案:

没有答案