package com.dev.bins.shop.fragment.find;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.dev.bins.shop.R;
import com.dev.bins.shop.bean.Goods;
import com.dev.bins.shop.bean.GoodsItem;
import com.dev.bins.shop.bean.ResponseMsg;
import com.dev.bins.shop.fragment.BaseFragment;
import com.dev.bins.shop.net.NetworkManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import rx.Subscriber;

/**
 * A simple {@link Fragment} subclass.
 */
public class FindFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {

    private List<GoodsItem> mGoods = new ArrayList<>();
    private int curPage = 1;
    @BindView(R.id.hot_recycler)
    RecyclerView mRecyclerView;
    @BindView(R.id.swipe)
    SwipeRefreshLayout mSwipe;
    FindAdapter mAdapter;

    public FindFragment() {
        // Required empty public constructor
    }

    public static FindFragment newInstance() {

        Bundle args = new Bundle();

        FindFragment fragment = new FindFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_hot;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mSwipe.setOnRefreshListener(this);
        mAdapter = new FindAdapter(mGoods);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        mRecyclerView.setAdapter(mAdapter);
        load(1);
    }

    public void load(int page) {
        Subscriber<ResponseMsg<Goods>> subscriber = new Subscriber<ResponseMsg<Goods>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                mSwipe.setRefreshing(false);
                e.printStackTrace();
            }

            @Override
            public void onNext(ResponseMsg<Goods> goodsResponseMsg) {
                mGoods.clear();
                mGoods.addAll(goodsResponseMsg.getData().getGoods());
                mAdapter.notifyDataSetChanged();
                mSwipe.setRefreshing(false);
            }
        };
        NetworkManager.getInstance().getGoods(subscriber, page, 10);
//        mSubscriptions.add(subscription);

    }



    @Override
    public void onRefresh() {
        load(1);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != mSubscriptions) {
            mSubscriptions.unsubscribe();
        }
    }
}

@@ -296,24 +296,151 @@
 cyclerView;%0A
+import android.support.v7.widget.helper.ItemTouchHelper;%0Aimport android.view.GestureDetector;%0Aimport android.view.MotionEvent;%0A
 import andro
@@ -1180,16 +1180,55 @@
 apter;%0A%0A
+    GestureDetector mGestureDetector;%0A%0A
     publ
@@ -1848,156 +1848,1307 @@
    m
-RecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));%0A        mRecyclerView.setAdapter(mAdapter
+GestureDetector = new GestureDetector(getContext(),new GestureDetector.SimpleOnGestureListener()%7B%0A            @Override%0A            public boolean onSingleTapUp(MotionEvent e) %7B%0A                float x = e.getX();%0A                float y = e.getY();%0A                View viewUnder = mRecyclerView.findChildViewUnder(x, y);%0A                if (null != viewUnder)%7B%0A                    int position = mRecyclerView.getChildLayoutPosition(viewUnder);%0A                    %0A                %7D%0A                return super.onSingleTapUp(e);%0A            %7D%0A        %7D);%0A        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));%0A        mRecyclerView.setAdapter(mAdapter);%0A        mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() %7B%0A            @Override%0A            public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) %7B%0A                mGestureDetector.onTouchEvent(e);%0A                return false;%0A            %7D%0A%0A            @Override%0A            public void onTouchEvent(RecyclerView rv, MotionEvent e) %7B%0A                mGestureDetector.onTouchEvent(e);%0A            %7D%0A%0A            @Override%0A            public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) %7B%0A%0A            %7D%0A        %7D
 );%0A 

add item touch listener