Android 当内部回收视图位于第一个位置并被拉下时,滚动坐标布局
我有以下活动布局Android 当内部回收视图位于第一个位置并被拉下时,滚动坐标布局,android,android-coordinatorlayout,bottom-sheet,Android,Android Coordinatorlayout,Bottom Sheet,我有以下活动布局 <android.support.design.widget.CoordinatorLayout> <View/> <FrameLayout android:id="@+id/fragment_container" app:layout_behavior="@string/bottom_sheet_behavior"/> </android.support.design.widget.Coordinato
<android.support.design.widget.CoordinatorLayout>
<View/>
<FrameLayout
android:id="@+id/fragment_container"
app:layout_behavior="@string/bottom_sheet_behavior"/>
</android.support.design.widget.CoordinatorLayout>
我有一个包含垂直回收视图的片段。它充当另一个水平滚动的recyclerview的容器。片段布局为:
<RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/wall_rooms"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/app_bar_height"
android:background="@color/pinkDark"
android:overScrollMode="never" />
</RelativeLayout>
当RecyclerView在顶部显示第一项并且用户向下滚动recycler视图时,协调器布局应该滚动内容。如果RecyclerView正在显示其他项目,向下滚动应该会滚动RecyclerView的内容
我尝试使用setNestedScrollingEnabled=false,但RecyclerView中的项目不会滚动。在花了很多时间在它上面,并尝试了不同的方法后,终于让它工作了。扩展BottomSheetBehavior类并重写onInterceptTouchEvent方法解决了该问题。张贴它,希望它能帮助别人
public class MyBottomsheetBehaviour extends BottomSheetBehavior {
int lastY = 0;
MyBottomsheetBehaviour() {
super();
}
@Keep
public MyBottomsheetBehaviour(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, View view, MotionEvent event) {
int dy = 0;
boolean shouldWeConsumeIt = false;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
lastY = (int) event.getY();
} else if (event.getAction() == MotionEvent.ACTION_CANCEL || event.getAction() == MotionEvent.ACTION_UP) {
lastY = 0;
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
dy = (int) event.getY() - lastY;
}
Log.d("Swapnil", "onInterceptTouchEvent() event Y=" + event.getY() + " x=" + event.getX() + getActionType(event.getActionMasked()) + view.getClass().getSimpleName() + "dy=" + dy);
if (view instanceof FrameLayout) {
View child = ((ViewGroup) ((FrameLayout) view).getChildAt(0)).getChildAt(0);
LinearLayoutManager manager = (LinearLayoutManager) ((RecyclerView) child).getLayoutManager();
if (manager.findFirstCompletelyVisibleItemPosition() == 0 && dy > 0) {
shouldWeConsumeIt = true;
int parentHeight = view.getHeight();
ViewCompat.offsetTopAndBottom(view, dy);
} else if (manager.findFirstCompletelyVisibleItemPosition() == 0) {
int height = ((View) view.getParent()).getHeight();
int top = view.getTop();
if ((event.getAction() == MotionEvent.ACTION_CANCEL || event.getAction() == MotionEvent.ACTION_UP) && top < (height >> 2)) {
view.setTop(0);
setState(BottomSheetBehavior.STATE_EXPANDED);
} else if ((event.getAction() == MotionEvent.ACTION_CANCEL || event.getAction() == MotionEvent.ACTION_UP)) {
setState(BottomSheetBehavior.STATE_COLLAPSED);
}
}
}
if (event.getAction() == MotionEvent.ACTION_MOVE) {
lastY = (int) event.getY();
}
if (!shouldWeConsumeIt)
return super.onInterceptTouchEvent(parent, view, event);
return true;
}
@Override
public boolean onTouchEvent(CoordinatorLayout parent, View child, MotionEvent event) {
return super.onTouchEvent(parent, child, event);
}
private String getActionType(int eventType) {
StringBuilder builder = new StringBuilder();
if (eventType == MotionEvent.ACTION_DOWN)
builder.append(" DOWN");
else if ((eventType & MotionEvent.ACTION_UP) == MotionEvent.ACTION_UP)
builder.append(" UP");
else if ((eventType & MotionEvent.ACTION_CANCEL) == MotionEvent.ACTION_CANCEL)
builder.append(" CANCEL");
else if ((eventType & MotionEvent.ACTION_MOVE) == MotionEvent.ACTION_MOVE)
builder.append(" MOVE");
return builder.toString();
}
}
公共类MyBottomSheetBehavior扩展了BottomSheetBehavior{
int-lastY=0;
我的行为(){
超级();
}
@保持
公共行为(上下文、属性集属性){
超级(上下文,attrs);
}
@凌驾
公共布尔值onInterceptTouchEvent(CoordinatorLayout父级、视图视图、运动事件){
int-dy=0;
布尔值shouldWeConsumeIt=false;
if(event.getAction()==MotionEvent.ACTION\u向下){
lastY=(int)event.getY();
}else if(event.getAction()==MotionEvent.ACTION_CANCEL | | event.getAction()==MotionEvent.ACTION_UP){
lastY=0;
}else if(event.getAction()==MotionEvent.ACTION\u MOVE){
dy=(int)event.getY()-lastY;
}
Log.d(“Swapnil”、“onInterceptTouchEvent()事件Y=“+event.getY()+”x=“+event.getX()+getActionType(event.getActionMasked())+view.getClass().getSimpleName()+”dy=“+dy”);
if(查看FrameLayout的实例){
视图子对象=((视图组)((框架布局)视图).getChildAt(0)).getChildAt(0);
LinearLayoutManager=(LinearLayoutManager)((RecyclerView)子项)。getLayoutManager();
if(manager.findFirstCompletelyVisibleItemPosition()==0&&dy>0){
我们应该消费它=真的;
int parentHeight=view.getHeight();
ViewCompat.offsetTopAndBottom(视图,dy);
}else if(manager.findFirstCompletelyVisibleItemPosition()=0){
int height=((视图)View.getParent()).getHeight();
int top=view.getTop();
if((event.getAction()==MotionEvent.ACTION_取消| | event.getAction()==MotionEvent.ACTION_向上)&&top<(高度>>2)){
view.setTop(0);
setState(行为。状态_扩展);
}else if((event.getAction()==MotionEvent.ACTION_CANCEL | | event.getAction()==MotionEvent.ACTION_UP)){
设置状态(行为。状态_已崩溃);
}
}
}
if(event.getAction()==MotionEvent.ACTION\u MOVE){
lastY=(int)event.getY();
}
如果(!我们应该消费它)
返回super.onInterceptTouchEvent(父级、视图、事件);
返回true;
}
@凌驾
公共布尔onTouchEvent(CoordinatorLayout父级、视图子级、MotionEvent事件){
返回super.onTouchEvent(父、子、事件);
}
私有字符串getActionType(int eventType){
StringBuilder=新的StringBuilder();
if(eventType==MotionEvent.ACTION\u DOWN)
建筑商。追加(“向下”);
else if((eventType&MotionEvent.ACTION\u UP)==MotionEvent.ACTION\u UP)
建造商。追加(“追加”);
else if((eventType&MotionEvent.ACTION\u CANCEL)=MotionEvent.ACTION\u CANCEL)
建造商。追加(“取消”);
else if((eventType&MotionEvent.ACTION\u MOVE)=MotionEvent.ACTION\u MOVE)
建造商。追加(“移动”);
返回builder.toString();
}
}
花了很多时间在上面,尝试了不同的东西,终于让它工作了。扩展BottomSheetBehavior类并重写onInterceptTouchEvent方法解决了该问题。张贴它,希望它能帮助别人
public class MyBottomsheetBehaviour extends BottomSheetBehavior {
int lastY = 0;
MyBottomsheetBehaviour() {
super();
}
@Keep
public MyBottomsheetBehaviour(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, View view, MotionEvent event) {
int dy = 0;
boolean shouldWeConsumeIt = false;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
lastY = (int) event.getY();
} else if (event.getAction() == MotionEvent.ACTION_CANCEL || event.getAction() == MotionEvent.ACTION_UP) {
lastY = 0;
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
dy = (int) event.getY() - lastY;
}
Log.d("Swapnil", "onInterceptTouchEvent() event Y=" + event.getY() + " x=" + event.getX() + getActionType(event.getActionMasked()) + view.getClass().getSimpleName() + "dy=" + dy);
if (view instanceof FrameLayout) {
View child = ((ViewGroup) ((FrameLayout) view).getChildAt(0)).getChildAt(0);
LinearLayoutManager manager = (LinearLayoutManager) ((RecyclerView) child).getLayoutManager();
if (manager.findFirstCompletelyVisibleItemPosition() == 0 && dy > 0) {
shouldWeConsumeIt = true;
int parentHeight = view.getHeight();
ViewCompat.offsetTopAndBottom(view, dy);
} else if (manager.findFirstCompletelyVisibleItemPosition() == 0) {
int height = ((View) view.getParent()).getHeight();
int top = view.getTop();
if ((event.getAction() == MotionEvent.ACTION_CANCEL || event.getAction() == MotionEvent.ACTION_UP) && top < (height >> 2)) {
view.setTop(0);
setState(BottomSheetBehavior.STATE_EXPANDED);
} else if ((event.getAction() == MotionEvent.ACTION_CANCEL || event.getAction() == MotionEvent.ACTION_UP)) {
setState(BottomSheetBehavior.STATE_COLLAPSED);
}
}
}
if (event.getAction() == MotionEvent.ACTION_MOVE) {
lastY = (int) event.getY();
}
if (!shouldWeConsumeIt)
return super.onInterceptTouchEvent(parent, view, event);
return true;
}
@Override
public boolean onTouchEvent(CoordinatorLayout parent, View child, MotionEvent event) {
return super.onTouchEvent(parent, child, event);
}
private String getActionType(int eventType) {
StringBuilder builder = new StringBuilder();
if (eventType == MotionEvent.ACTION_DOWN)
builder.append(" DOWN");
else if ((eventType & MotionEvent.ACTION_UP) == MotionEvent.ACTION_UP)
builder.append(" UP");
else if ((eventType & MotionEvent.ACTION_CANCEL) == MotionEvent.ACTION_CANCEL)
builder.append(" CANCEL");
else if ((eventType & MotionEvent.ACTION_MOVE) == MotionEvent.ACTION_MOVE)
builder.append(" MOVE");
return builder.toString();
}
}
公共类MyBottomSheetBehavior扩展了BottomSheetBehavior{
int-lastY=0;
我的行为(){
超级();
}
@保持
公共行为(上下文、属性集属性){
超级(上下文,attrs);
}
@凌驾
公共布尔值onInterceptTouchEvent(CoordinatorLayout父级、视图视图、运动事件){
int-dy=0;
布尔值shouldWeConsumeIt=false;
if(event.getAction()==MotionEvent.ACTION\u向下){
lastY=(int)event.getY();
}else if(event.getAction()==MotionEvent.ACTION_CANCEL | | event.getAction()==MotionEvent.ACTION_UP){
lastY=0;
}else if(event.getAction()==MotionEvent.ACTION\u MOVE){
dy=(int)event.getY()-lastY;
}
Log.d(“Swapnil”、“onInterceptTouchEvent()事件Y=“+event.getY()+”x=“+event.getX()+getActionType(event.getActionMasked())+view.getClass().getSimpleName()+”dy=“+dy”);
if(查看FrameLayout的实例){
视图子对象=((视图组)((框架布局)视图).getChildAt(0)).getChildAt(0);
LinearLayoutManager=(LinearLayoutManager)((RecyclerView)子项)。getLayoutManager();
if(manager.findFirstCompletelyVisibleItemPosition()==0&&dy>0){
我们应该消费它=真的;
int parentHeight=view.getHeight();
ViewCompat.offsetTopAndBottom(视图,dy);
}else if(manager.findFirstCompletelyVisibleItemPosition()=0){
int height=((视图)View.getParent()).getHeight();
int top=view.getTop();
如果((event.getAction()==MotionEvent.ACTION\u取消|