Search in sources :

Example 1 with OnRecyclerItemTouchCallBack

use of com.shiqkuangsan.mycustomviews.ui.custom.OnRecyclerItemTouchCallBack in project CustomViews by AndroidStudy233.

the class RecyclerViewActivity method fillContentView.

private void fillContentView() {
    View view = View.inflate(this, R.layout.layout_recycler_main, null);
    // 初始化SwipeRefreshLayout
    swipe_main = (SwipeRefreshLayout) view.findViewById(R.id.swipe_recycler_main);
    swipe_main.setColorSchemeResources(R.color.swipe_schema_red, swipe_schema_blue, R.color.swipe_schema_green);
    swipe_main.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

        @Override
        public void onRefresh() {
            MyApplication.getHandler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    showToast("刷新成功~");
                    swipe_main.setRefreshing(false);
                }
            }, 1500);
        }
    });
    // 初始化RecyclerView
    recycler_main = (RecyclerView) view.findViewById(R.id.recycler_recycler_main);
    // 设置布局管理者,采用线性布局管理者,竖直排列,不用反转(反转就是数据反序)
    recycler_main.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
    adapter = new SimpleRecyclerAdapter(this, new SimpleRecyclerAdapter.OnItemClickListener() {

        @Override
        public void onClick(int position, ImgAndText bean) {
            /*
                    这里有个要注意的地方就是,在adapter中设置侦听的时候你要是用onBindViewHolder方法中的参数position
                    那么remove之后会position错乱BUG.这是RecyclerView的一个大坑...要用holder.getAdapterPosition传递
                 */
            System.arraycopy(hits, 1, hits, 0, hits.length - 1);
            hits[hits.length - 1] = SystemClock.uptimeMillis();
            if (hits[0] >= SystemClock.uptimeMillis() - 500) {
                MyLogUtil.debug("position: " + position);
                adapter.removeItem(position);
            } else {
                ToastUtil.toastShort(RecyclerViewActivity.this, "点击了图片");
            }
        }
    });
    // 设置拖拽监听, 首先定义一个callback定义拖拽功能, callback中定义listener与adapter传递事件
    // 然后将callback传给helper, 并由helper绑定recycle
    ItemTouchHelper.Callback callback = new OnRecyclerItemTouchCallBack(adapter);
    ItemTouchHelper helper = new ItemTouchHelper(callback);
    helper.attachToRecyclerView(recycler_main);
    /*
            细节注意, 上述方式是通过长按条目来实现拖拽, 比如条目右边有个图标, 希望按住图标实现拖拽呢?
            adapter的holder中设置图标触摸事件setOnTouchListener. 这就需要在onTouch方法中调用上面helper的
            onStartDrag方法, 怎么调用呢? adapter定义一个接口OnIconDragHandler. 里边定义
            onHandleDrag(RecyclerView.ViewHolder viewHolder); 然后通过构造传进来.
            这样再让当前activity实现接口, 调用helper的onStartDrag方法. 然后new adapter的时候传过去.
            最后具体一下图标的ouTouch方法
             如果按下
                if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN){
                    回调RecyclerViewActivity中的onHandleDrag方法
                    让mItemTouchHelper执行拖拽操作
                    handler.startDrag(holder);
                }
         */
    // recycler_main.setAdapter(adapter);
    // 使用第三方库中简单的动画来设置适配器,其对象可以setDuration、setInterpolator,甚至可以继续传入形成装饰者
    // recycler_main.setAdapter(new ScaleInAnimationAdapter(adapter));
    recycler_main.setAdapter(new SlideInLeftAnimationAdapter(adapter));
    /*
            这里有个非常重要的问题,如果你想使用增删动画,那么在增删操作的时候
            你就不能使用notifyDataSetChanged,不然动画不会执行的,需要使用notifyItemRemoved(posi)、notifyItemInserted(posi)
         */
    // 设置条目动画,增删的时候会用到
    SlideInLeftAnimator animator = new SlideInLeftAnimator();
    animator.setInterpolator(new OvershootInterpolator());
    recycler_main.setItemAnimator(animator);
    // 将填充好的View放到主页面上
    frame_main.addView(view);
}
Also used : ImgAndText(com.shiqkuangsan.mycustomviews.bean.ImgAndText) OvershootInterpolator(android.view.animation.OvershootInterpolator) SlideInLeftAnimationAdapter(jp.wasabeef.recyclerview.adapters.SlideInLeftAnimationAdapter) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) View(android.view.View) ContentView(org.xutils.view.annotation.ContentView) RecyclerView(android.support.v7.widget.RecyclerView) SwipeRefreshLayout(android.support.v4.widget.SwipeRefreshLayout) SimpleRecyclerAdapter(com.shiqkuangsan.mycustomviews.adapter.SimpleRecyclerAdapter) ItemTouchHelper(android.support.v7.widget.helper.ItemTouchHelper) OnRecyclerItemTouchCallBack(com.shiqkuangsan.mycustomviews.ui.custom.OnRecyclerItemTouchCallBack) SlideInLeftAnimator(jp.wasabeef.recyclerview.animators.SlideInLeftAnimator)

Aggregations

SwipeRefreshLayout (android.support.v4.widget.SwipeRefreshLayout)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 ItemTouchHelper (android.support.v7.widget.helper.ItemTouchHelper)1 View (android.view.View)1 OvershootInterpolator (android.view.animation.OvershootInterpolator)1 SimpleRecyclerAdapter (com.shiqkuangsan.mycustomviews.adapter.SimpleRecyclerAdapter)1 ImgAndText (com.shiqkuangsan.mycustomviews.bean.ImgAndText)1 OnRecyclerItemTouchCallBack (com.shiqkuangsan.mycustomviews.ui.custom.OnRecyclerItemTouchCallBack)1 SlideInLeftAnimationAdapter (jp.wasabeef.recyclerview.adapters.SlideInLeftAnimationAdapter)1 SlideInLeftAnimator (jp.wasabeef.recyclerview.animators.SlideInLeftAnimator)1 ContentView (org.xutils.view.annotation.ContentView)1