java - 当框出现在屏幕中间时更改框的背景颜色
问题描述
我正在尝试制作一个应用程序,当它出现在屏幕中间时,框的背景颜色会发生变化。我能够做到这一点,javascript
但我在 android java 中找不到任何好的方法。这意味着我在 android java 中找不到与 , 类似的方法来构造类似的逻辑$this.scrollY
。所以请帮助我实现这一目标。$this.innerHeight
box.offsetTop
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp"
android:background="@color/skyblue"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="80dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
MainActivity.java:
package com.example.test;
import android.support.v7.app.AppCompatActivity;
import android.os.*;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout content = findViewById(R.id.content);
for (int x=0;x<25;x++) {
RelativeLayout element = new RelativeLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 400);
params.setMargins(0, 0, 0, 50);
element.setLayoutParams(params);
element.setBackgroundResource(R.color.green);
content.addView(element);
}
}
}
MY JAVASCRIPT WORKING EXAMPLE:
window.onbeforeunload = function () {
this.scrollTo(0, 0);
}
var content = document.getElementById("content");
for (var x=0;x<25;x++) {
var box = document.createElement("DIV");
box.id = "box";
content.appendChild(box);
}
content.children[0].style.backgroundColor = "#008100";
var current_box = content.children[0],
scroll_timeout = undefined;
window.addEventListener("scroll", function () {
var $this = this;
window.clearTimeout(scroll_timeout);
scroll_timeout = setTimeout(function() {
var middle_line = $this.scrollY + ($this.innerHeight/2);
for (var y=0;y<content.querySelectorAll("#box").length;y++) {
var box = content.children[y];
if ((box.offsetTop) < middle_line && (box.offsetTop + box.clientHeight + 50) > middle_line) {
if (box != current_box) {
current_box.style.backgroundColor = "#6CABF7";
current_box = box;
current_box.style.backgroundColor = "#008100";
}
break;
}
}
}, 100);
});
body {
margin: 0;
}
#content {
width: 60%;
height: 100%;
margin: 0% 20% 0% 20%;
}
#box {
width: 100%;
height: 500px;
background-color: #6CABF7;
overflow: hidden;
margin-bottom: 50px;
}
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div id="content"></div>
</body>
</html>
解决方案
如果你想对滚动事件做出反应,那么你需要监听它们。我在您的 java 代码中没有看到任何此类侦听器,但您的 javascript 代码中有一个侦听器。
我发现了这个 StackOverflow 问题,我相信它可能会对您有所帮助。
推荐阅读
- linux - 如何创建不带参数 $1 和 $2 的字符串=$*
- selenium - Groovy gives "No such Property" but its declared in the method: selenium and page object modelling
- java - 使用Java将文件复制到具有相同文件名的文件夹中
- haskell - 如何设置列表的范围?
- java - 是否可以在资源中返回超类列表并在客户端接收正确的子类?(使用resteasy)
- r - 根据季度和年份将两个数据框连接在一起
- r - 与(闪亮的)服务器断开连接:大 Plotly R 热图的原因
- java - 如何使用 iText 7 将 Pdf 页面呈现为光栅图像
- arrays - 如何在 Kotlin 中将字符串转换为字符串数组?
- excel - 当 VBA 代码在 Excel 中运行时,用户如何处理其他事情?