android滑动view
程序员文章站
2022-05-09 10:25:13
package com.example.demo;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.view.MotionEvent;import android.view.View;import android.view.ViewGroup;import android.widget.LinearLayout;/** * @author Huang xudon....
package com.example.demo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
/**
* @author Huang xudong
* @date 2020/7/26
*/
public class MainActivity extends AppCompatActivity {
private LinearLayout view;
/**
* 定义你的View最大高度,maybe set max PHONE height
* 这里设置最大值,防止底部还有组件
*/
private int maxTopHeight=5000;
/**
* 获取控件的默认高度,当下滑最小高度的时候不再滑动了
*/
private int defaultHeight=0;
/**
* 这里设置的组属性
*/
private ViewGroup.LayoutParams layoutParams;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view=findViewById(R.id.ll_main);
layoutParams = view.getLayoutParams();
defaultHeight=layoutParams.height;
/**
* 注册view滑动事件,可以选择其他的,比喻指定的组件
*/
view.setOnTouchListener(new View.OnTouchListener() {
int y=0;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
y= (int) event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
int lastY= (int) event.getRawY();
if (lastY-y>0){
if (layoutParams.height>defaultHeight){
layoutParams.height=layoutParams.height-Math.abs(lastY-y)/20;
view.setLayoutParams(layoutParams);
}
}else {
if (layoutParams.height<maxTopHeight-50){
layoutParams.height=layoutParams.height+Math.abs(lastY-y)/20;
view.setLayoutParams(layoutParams);
}
}
break;
default:
break;
}
return true;
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:background="@color/colorPrimaryDark"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/ll_main"
android:background="@color/colorAccent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_width="0dp"
android:layout_height="200dp"
android:orientation="vertical">
<TextView
android:layout_margin="10dp"
android:maxLines="1"
android:ellipsize="end"
android:textColor="#ffff"
android:textSize="40sp"
android:text="111111111111111111111"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView
android:layout_margin="10dp"
android:maxLines="1"
android:ellipsize="end"
android:textColor="#ffff"
android:textSize="40sp"
android:text="222222222222222222"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView
android:layout_margin="10dp"
android:maxLines="1"
android:ellipsize="end"
android:textColor="#ffff"
android:textSize="40sp"
android:text="3333333333333333"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView
android:layout_margin="10dp"
android:maxLines="1"
android:ellipsize="end"
android:textColor="#ffff"
android:textSize="40sp"
android:text="4444444444444444"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView
android:layout_margin="10dp"
android:maxLines="1"
android:ellipsize="end"
android:textColor="#ffff"
android:textSize="40sp"
android:text="55555555555555"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView
android:layout_margin="10dp"
android:maxLines="1"
android:ellipsize="end"
android:textColor="#ffff"
android:textSize="40sp"
android:text="666666666666"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
主要有一个算法,其他的都是比较简单的。上面是效果图
本文地址:https://blog.csdn.net/s452195377/article/details/107595869
上一篇: 国庆旅游珠海哪里好玩 珠海旅游景点推荐