完成需求的时候涉及到这个所以撸了一下
本文章是本人原创,转载请带原地址连接
先放效果图('霁雪清虹"是目标):
首先需要一个自定义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"/>