android可见区域,识别目标View在HorizontalScrollView可见区域

完成需求的时候涉及到这个所以撸了一下

本文章是本人原创,转载请带原地址连接

先放效果图('霁雪清虹"是目标):

AAffA0nNPuCLAAAAAElFTkSuQmCC

首先需要一个自定义HorizontalScrollView,复写一个View的onScrollChanged方法,用于监听滑动变化

代码如下:import android.content.Context;

import android.util.AttributeSet;

import android.widget.HorizontalScrollView;

/**

* Created by 霁雪清虹 on 2016/9/29.

*/

public class MyHorizontalScrollView extends HorizontalScrollView {

private MyScrollListener myScrollListener;

public MyHorizontalScrollView(Context context, AttributeSet attrs) {

super(context, attrs);

}

@Override

protected void onScrollChanged(int l, int t, int oldl, int oldt) {

super.onScrollChanged(l, t, oldl, oldt);

if (myScrollListener != null) {

myScrollListener.onScrollChanged();

}

}

public void setMyScrollListener(MyScrollListener myScrollListener) {

this.myScrollListener = myScrollListener;

}

public interface MyScrollListener {

void onScrollChanged();

}

}

然后就是MainActivity的onScrollChanged中的代码就是重点了

代码如下:

import android.graphics.Point;

import android.graphics.Rect;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.View;

import android.widget.LinearLayout;

import android.widget.RelativeLayout;

import android.widget.TextView;

import android.widget.Toast;

import java.util.ArrayList;

import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, MyHorizontalScrollView.MyScrollListener {

protected TextView targetTextView;

private final static String NAME = "霁雪清虹";

protected MyHorizontalScrollView horizontalScrollView;

private Rect rect;

private Rect globalRect;

private Point globalOffset;

private List contentList = new ArrayList();

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

super.setContentView(R.layout.activity_main);

initView();

bindDataToUi();

rect = new Rect();

globalRect = new Rect();

globalOffset = new Point();

}

private void bindDataToUi() {

contentList.add("天涯");

contentList.add("兰亭书序");

contentList.add("天涯");

contentList.add("兰亭书序");

contentList.add("晨曦");

contentList.add("虹猫");

contentList.add("天涯");

contentList.add("兰亭书序");

contentList.add("霁雪清虹");

contentList.add("懵逼虹");

contentList.add("小逗比");

contentList.add("若冰");

contentList.add("晨曦");

contentList.add("虹猫");

contentList.add("天涯");

contentList.add("兰亭书序");

for (String str : contentList) {

RelativeLayout contentPanel = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.content, null);

((LinearLayout) horizontalScrollView.getChildAt(0)).addView(contentPanel);

TextView text = (TextView) contentPanel.findViewById(R.id.text);

if (NAME.equals(str)) {

targetTextView = text;

}

text.setText(str);

}

}

@Override

public void onClick(View view) {

}

private void initView() {

horizontalScrollView = (MyHorizontalScrollView) findViewById(R.id.horizontalScrollView);

horizontalScrollView.setMyScrollListener(this);

}

private boolean isFirstBack;

@Override

public void onBackPressed() {

if (isFirstBack) {

super.onBackPressed();

}

isFirstBack = true;

Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();

}

@Override

protected void onDestroy() {

super.onDestroy();

}

@Override

public void onScrollChanged() {

targetTextView.getGlobalVisibleRect(globalRect, globalOffset);

/* Log.e("TAG","globalRect is "+globalRect.toString());

Log.e("TAG","globalOffset is "+globalOffset.toString());

Log.e("TAG","reat width is "+reat.toString());*/

if (targetTextView.getLocalVisibleRect(rect)) {

//左可见

if (globalOffset.x < 0 && rect.width()< targetTextView.getWidth()

) {

Log.e("TAG", "目标霁雪清虹左半边可见");

} else if (globalOffset.x > 0 && rect.width() < targetTextView.getWidth()

) {

Log.e("TAG", "目标霁雪清虹右半边可见");

} else {

Log.e("TAG", "目标霁雪清虹全可见");

}

} else {

Log.e("TAG", "目标霁雪清虹不可见");

}

}

}

下面是Activity的xml

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/activity_main"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context="com.example.jiawei11.animationdemo.MainActivity">

android:id="@+id/horizontalScrollView"

android:layout_width="match_parent"

android:layout_height="wrap_content"

>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:paddingLeft="5dp"

android:paddingRight="5dp">

下面是 content.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingLeft="5dp"

android:paddingRight="5dp">

android:id="@+id/text"

android:layout_width="wrap_content"

android:layout_height="wrap_content"/>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/428896.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

1015 德才论 (25 分)

1015 德才论 (25 分) 宋代史学家司马光在《资治通鉴》中有一段著名的“德才论”&#xff1a;“是故才德全尽谓之圣人&#xff0c;才德兼亡谓之愚人&#xff0c;德胜才谓之君子&#xff0c;才胜德谓之小人。凡取人之术&#xff0c;苟不得圣人&#xff0c;君子而与之&#xff0c…

AI单挑Dota 2世界冠军:被电脑虐哭……

OpenAI的机器人刚刚在 Dota2 1v1 比赛中战胜了人类顶级职业玩家 Denti。以建设安全的通用人工智能为己任的 OpenAI&#xff0c;通过“Self-Play”的方式&#xff0c;从零开始训练出了这个机器人。 Dota2沦陷 继横扫顶级的人类国际象棋大师和围棋大师后&#xff0c;计算机如今在…

用session实现html登录页面跳转页面跳转页面跳转,js判断登录与否并确定跳转页面的方法...

这篇文章主要介绍了js判断登录与否并确定跳转页面的方法,涉及Ajax及session使用的技巧,非常具有实用价值,需要的朋友可以参考下本文实例讲述了js判断登录与否并确定跳转页面的方法。分享给大家供大家参考。具体如下&#xff1a;使用session存储&#xff0c;确定用户是否登录&am…

7-26 Windows消息队列(25 分)

7-26 Windows消息队列&#xff08;25 分&#xff09; 消息队列是 Windows 系统的基础。对于每个进程&#xff0c;系统维护一个消息队列。如果在进程中有特定事件发生&#xff0c;如点击鼠标、文字改变等&#xff0c;系统将把这个消息加到队列当中。同时&#xff0c;如果队列不…

Java——操作集合的工具类:Collections

Java 提供了一个操作 Set 、List 和 Map 等集合的工具类 &#xff1a;Collections&#xff0c;该工具类里提供了大量方法对集合元素进行排序、查询和修改等操作 转载于:https://www.cnblogs.com/szj-ang/p/7383027.html

鸿蒙关键技术研究,华为鸿蒙 2.0 系统主题演讲公布,详细架构 9 月 11 日揭晓

IT之家 8 月 30 日消息 华为 9 月 10 日将举行华为开发者大会 2020&#xff0c;华为官网表示&#xff0c;“我们将与您分享 HMS Core 5.0 最新进展&#xff0c; 揭开 HarmonyOS 和 EMUI 11 的神秘面纱。 振奋人心的新技术&#xff0c;深入的交流学习机会&#xff0c; 更灵动的想…

shell 提示符个性化设置

提示符具体含义可参考&#xff1a; http://billie66.github.io/TLCL/book/zh/chap14.html Ubuntu16.04个人配置如下&#xff0c;供以后查阅 1 function git_branch {2 branch"git branch 2>/dev/null | grep "^\*" | sed -e "s/^\*\ //""3…

如何设置鼠标滚轮html,win7如何设置鼠标滚轮

你们知道在W7中怎么设置鼠标的滚轮吗?下面是小编带来的关于win7如何设置鼠标滚轮的内容&#xff0c;欢迎阅读!Win7设置滚轮方法一&#xff1a;首先要在电脑的左下角点击开始按钮点击开始按钮以后出现上拉菜单&#xff0c;在菜单上面点击控制面板点击控制面板以后进入到控制面板…

湛江高考2021成绩查询,2021广东省高中学业水平考试成绩查询(入口+方式)

2021年广东高中学业水平合格性考试成绩查询查询方式&#xff1a;考生登录广东省教育考试服务中心的广东教育考试服务网&#xff0c;通过综合查询栏目页面&#xff0c;按相关提示即可查询考试成绩。查询入口二&#xff1a;“广东省教育考试院”小程序查询方式&#xff1a;①在“…

A. Red and Blue Beans

题意&#xff1a;红豆子和绿豆子分在不同的篮子里。问最小的最大差是能不能比给的d小。 方法&#xff1a;尽可能用更多的篮子里。 #include<iostream> using namespace std; int main() {double a,b,k;int n;cin>>n;for (int i0;i<n;i){cin>>a>>b&…

JAVA经典算法40题

【程序1】 题目&#xff1a;古典问题&#xff1a;有一对兔子&#xff0c;从出生后第3个月起每个月都生一对兔子&#xff0c;小兔子长到第四个月后每个月又生一对兔子&#xff0c;假如兔子都不死&#xff0c;问每个月的兔子总数为多少&#xff1f; 1.程序分析&#xff1a; 兔子…

中英对照 关于计算机的科技英语,《计算机专业英语》(中英文对照).pdf

《计算机专业英语》(中英文对照)计算机专业英语Computer EnglishChapter 1 The History andFuture of Computers2009.9.1Chapter 1 The History and Future of ComputersKey points:Key points:useful terms and definitions ofuseful terms and definitions ofcomputerscomput…

[php] in_array 判断问题(坑)

<?php $arr array("Linux"); if (in_array(0, $arr)) {echo "match"; } ?> 执行以上代码&#xff0c;0和字符串是可以匹配成功的。 原因是在in_array&#xff0c;如果比较的类型不匹配&#xff0c;并且第一个参数是0&#xff0c;它会返回true&…

B. The Cake Is a Lie

题意&#xff1a;从&#xff08;1&#xff0c;1&#xff09;走到他给的点&#xff0c;只能向上和向右。int cou 0;如果向上就coux;,如果向右就couy; 题解&#xff1a;最大的cou是两条直线。最小的cou是一直转弯。 注意点&#xff1a;如果x>y 先走x;反之亦反&#xff1b; #i…

学计算机应该具备什么能力,学习计算机专业该具备那些能力?

计算机专业涵盖软件工程专业&#xff0c;主要培养具有良好的科学素养&#xff0c;系统地、较好地掌握计算机科学与技术包括计算机硬件、软件与应用的基本理论、基本知识和基本技能与方法&#xff0c;能在科研部门、教育单位、企业、事业、技术和行政管理部门等单位从事计算机教…

度度熊与邪恶大魔王

链接&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid6082 Problem Description 度度熊为了拯救可爱的公主&#xff0c;于是与邪恶大魔王战斗起来。邪恶大魔王的麾下有n个怪兽&#xff0c;每个怪兽有a[i]的生命值&#xff0c;以及b[i]的防御力。度度熊一共拥有m种攻击方…

Codeforces Round #719 (A-C)

第一题题意&#xff1a;就是不能回去&#xff1a; #include<iostream> using namespace std; int main() {int t;cin>>t;while (t--){int n,ch[1001]{0};cin>>n;string str;cin>>str;bool flag true;ch[str[0]];for (int i1;i<n;i){if (str[i]!st…

html加注算法源码,200种加密算法(源码)

【实例简介】【实例截图】【核心代码】3way.cpp3way.h3wayval.datalgebra.cppalgebra.hasn.cppasn.hbase64.cppbase64.hbench.cppbench.hbfinit.cppblowfish.cppblowfish.hblum1024.datblum2048.datblum512.datblumgold.cppblumgold.hblumshub.cppblumshub.hcast.cppcast.hcast…

ibm量子计算机科学家,重磅!IBM发布全球首个独立商用量子计算机

原标题&#xff1a;【重磅】IBM发布全球首个独立商用量子计算机雷锋网消息&#xff0c;全球的科技巨头都在量子计算上投入了大量资源。值得关注的是&#xff0c;在2019 CES上&#xff0c;IBM宣布推出IBM Q System One&#xff0c;该系统是世界上首个专为科学和商业用途设计的集…

1012 数字分类 (20 分)(C语言实现)

帮同学改的代码&#xff0c;简单易懂因为他是刚开始做 的写法&#xff1a; #include <stdio.h> #define MAX 10001 int main() {int i, n;int sum1 0;int A1 0, A2 0, A3 0, A4 0;int a[MAX];scanf("%d", &n);for (i 0; i < n; i){scanf("%…