我正在尝试在整个RecyclerView上实现滑动手势来显示/隐藏缩略图。请参见下图。
它正在运作,但工作不顺利。有时候,如果我们开始滚动recyclelerView,它就会抽搐。
请在下面找到我的代码
....
public class ChatFragment extends Fragment implements View.OnTouchListener {
OnSwipeInteractionListener listener;
private OnListFragmentInteractionListener mListener;
private int min_distance = 100;
private float downX, downY, upX, upY;
private NestedScrollView nestedScrollView;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public ChatFragment() {
}
public static ChatFragment newInstance(int columnCount) {
ChatFragment fragment = new ChatFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_item_list, container, false);
// Set the adapter
Context context = view.getContext();
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.list);
recyclerView.addItemDecoration(HeaderDecoration.with(recyclerView)
.inflate(R.layout.inflate_header_view_chat)
.parallax(0.8f)
.dropShadowDp(0)
.build());
recyclerView.setHasFixedSize(true);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setAdapter(new ChatAdapter(getActivity(), getDummyData(), mListener, this));
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.setOnTouchListener(this);
return view;
}
private ArrayList<PostItemParent> getDummyData() {
ArrayList<PostItemParent> posts = new ArrayList<>();
for (int i = 0; i < 4; i++) {
ArrayList<PostItem> postItems = new ArrayList<>();
for (int j = 1; j < 15; j++) {
postItems.add(new PostItem("" + (j + 1), "This is the dummy post, will ne changed after enabling api", j % 3 == 0, getAttachments()));
}
PostItemParent postItemParent = new PostItemParent("header " + i, postItems);
posts.add(postItemParent);
}
return posts;
}
private ArrayList<PostItem.AttachmentItem> getAttachments() {
ArrayList<PostItem.AttachmentItem> attachmentItems = new ArrayList<>();
for (int i = 0; i < 5; i++) {
if(i % 3 == 0)
attachmentItems.add(new PostItem.AttachmentItem(FileUtils.FILE_TYPE_IMAGE, true, false, true, "fiel url", "file url") );
else
attachmentItems.add(new PostItem.AttachmentItem(FileUtils.FILE_TYPE_OTHER, true, false, true, "fiel url", "file url") );
}
return attachmentItems;
}
public void setListener(OnSwipeInteractionListener listener) {
this.listener = listener;
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
downX = event.getX();
downY = event.getY();
return true;
}
case MotionEvent.ACTION_UP: {
upX = event.getX();
upY = event.getY();
float deltaX = downX - upX;
float deltaY = downY - upY;
//HORIZONTAL SCROLL
if (Math.abs(deltaX) > Math.abs(deltaY)) {
if (Math.abs(deltaX) > min_distance) {
// left or right
if (deltaX < 0) {
listener.onLeftToRighSwipeDone();
return true;
}
if (deltaX > 0) {
listener.onRightToLeftSwipeDone();
return true;
}
}
}
return false;
}
}
return false;
}
public interface OnSwipeInteractionListener {
/**
* called when LTR swipe done
*/
void onLeftToRighSwipeDone();
/**
* on RTL swipe done
*/
void onRightToLeftSwipeDone();
}
}