Skip to content

item里面放一个可以上下滑动的布局 #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
zyonehangao opened this issue Apr 13, 2019 · 7 comments
Open

item里面放一个可以上下滑动的布局 #25

zyonehangao opened this issue Apr 13, 2019 · 7 comments

Comments

@zyonehangao
Copy link

在item里面放一个比屏幕大的布局不能上下滑动的问题怎么解决

@ansnail
Copy link

ansnail commented May 16, 2019

@zyonehangao 这个你解决了么

@ansnail
Copy link

ansnail commented May 22, 2019

package com.yuqirong.cardswipeview;

import android.support.annotation.NonNull;
import android.support.v4.view.MotionEventCompat;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

/**
 * @author yuqirong
 */

public class CardLayoutManager extends RecyclerView.LayoutManager {

    private ItemTouchHelper mItemTouchHelper;

    private float mPrevX;
    private float mPrevY;

    public CardLayoutManager(@NonNull RecyclerView recyclerView, @NonNull ItemTouchHelper itemTouchHelper) {
        this.mItemTouchHelper = checkIsNull(itemTouchHelper);
        recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
            @Override
            public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent event) {
                View child = rv.findChildViewUnder(event.getX(), event.getY());
                if (child != null) {
                    RecyclerView.ViewHolder childViewHolder = rv.getChildViewHolder(child);
                    switch (MotionEventCompat.getActionMasked(event)) {
                        case MotionEvent.ACTION_DOWN:
                            mPrevX = event.getX();
                            mPrevY = event.getY();
                            break;

                        case MotionEvent.ACTION_MOVE:
                            float eventX = event.getX();
                            float eventY = event.getY();
                            float xDiff = Math.abs(eventX - mPrevX);
                            float yDiff = Math.abs(eventY - mPrevY);
                            if (xDiff > yDiff) {
                                mItemTouchHelper.startSwipe(childViewHolder);
                            }
                            break;
                        default:
                            break;
                    }
                }
                return false;
            }

            @Override
            public void onTouchEvent(RecyclerView rv, MotionEvent e) {
            }

            @Override
            public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
            }
        });
    }

    private <T> T checkIsNull(T t) {
        if (t == null) {
            throw new NullPointerException();
        }
        return t;
    }

    @Override
    public RecyclerView.LayoutParams generateDefaultLayoutParams() {
        return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    @Override
    public void onLayoutChildren(final RecyclerView.Recycler recycler, RecyclerView.State state) {
        // 先把所有的View先从RecyclerView中detach掉,然后标记为"Scrap"状态,表示这些View处于可被重用状态(非显示中)。
        // 实际就是把View放到了Recycler中的一个集合中。
        detachAndScrapAttachedViews(recycler);
        int itemCount = getItemCount();
        for (int position = itemCount > CardConfig.DEFAULT_SHOW_ITEM ? CardConfig.DEFAULT_SHOW_ITEM : itemCount - 1; position >= 0; position--) {
            // 遍历Recycler中保存的View取出来
            final View view = recycler.getViewForPosition(position);
            // 因为刚刚进行了detach操作,所以现在可以重新添加
            addView(view);
            // 通知测量view的margin值
            measureChildWithMargins(view, 0, 0);
            // 计算view实际大小,包括了ItemDecorator中设置的偏移量
            int width = getDecoratedMeasuredWidth(view);
            int height = getDecoratedMeasuredHeight(view);
            int widthSpace = getWidth() - width;
            int heightSpace = getHeight() - height;
            // 指定了该View的显示区域,并将View显示上去,此时所有区域都用于显示View
            layoutDecoratedWithMargins(view, widthSpace / 2, heightSpace / 2, widthSpace / 2 + width, heightSpace / 2 + height);

            if (position == CardConfig.DEFAULT_SHOW_ITEM) {
                view.setScaleX(1 - (position - 1) * CardConfig.DEFAULT_SCALE);
                view.setScaleY(1 - (position - 1) * CardConfig.DEFAULT_SCALE);
                view.setTranslationY((position - 1f) * view.getMeasuredHeight() / CardConfig.DEFAULT_TRANSLATE_Y);
            } else if (position > 0) {
                view.setScaleX(1 - position * CardConfig.DEFAULT_SCALE);
                view.setScaleY(1 - position * CardConfig.DEFAULT_SCALE);
                view.setTranslationY(position * view.getMeasuredHeight() / CardConfig.DEFAULT_TRANSLATE_Y);
            }
        }
    }
}

@isayWu
Copy link

isayWu commented May 25, 2019

怎么解决呢

@ansnail
Copy link

ansnail commented May 25, 2019

@isayWu 对的 你把这个Layoutmanger拷贝过去直接用 就可以在item里面放recyclerview或者ScrollView了 这里面主要处理了滑动冲突

@isayWu
Copy link

isayWu commented May 25, 2019

@isayWu 对的 你把这个Layoutmanger拷贝过去直接用 就可以在item里面放recyclerview或者ScrollView了 这里面主要处理了滑动冲突

thank you very mush

@ansnail
Copy link

ansnail commented May 25, 2019

@isayWu 我又试了下是可以的 你检查下自己的环境哈 直接拷贝改下包名就行

@cjIsMyName
Copy link

cjIsMyName commented Apr 14, 2020

@ansnail 是用这个 LayoutManager 直接覆盖原来的 CardLayoutManager 吗?貌似并没有什么用啊!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants