package com.bigfat.itemtouchhelperdemo; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.helper.ItemTouchHelper; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import java.util.ArrayList; import java.util.List; /** * A simple {@link Fragment} subclass. */ public class MainFragment extends Fragment { //控件 private RecyclerView mRecyclerView; private RecyclerListAdapter mAdapter; public MainFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_main, container, false); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view); mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); mAdapter = new RecyclerListAdapter(generateList(30)); mRecyclerView.setAdapter(mAdapter); ItemTouchHelper.Callback callback = new ItemTouchHelperCallback(mAdapter); ItemTouchHelper itemTouchHelper = new ItemTouchHelper(callback); itemTouchHelper.attachToRecyclerView(mRecyclerView); } private List<String> generateList(int capacity) { List<String> list = new ArrayList<>(); for (int i = 0; i < capacity; i++) { list.add(String.valueOf(i)); } return list; } }