布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.lib.demo.viewpagedemo.MyScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#333333"
android:orientation="horizontal"
android:paddingLeft="15dp"
android:paddingRight="15dp">
</LinearLayout>
</com.lib.demo.viewpagedemo.MyScrollView>
</LinearLayout>
重写HorizontalScrollView
package com.lib.demo.viewpagedemo;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import java.util.ArrayList;
/**
* Created by CHENGC on 2018/1/18.
*/
public class MyScrollView extends HorizontalScrollView {
private int subChildCount = 0;
private ViewGroup firstChild = null;
private int downX = 0;
private int currentPage = 0;
private ArrayList<Integer> pointList = new ArrayList<Integer>();
public MyScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyScrollView(Context context) {
super(context);
init();
}
private void init() {
setHorizontalScrollBarEnabled(false);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
receiveChildInfo();
}
public void receiveChildInfo() {
firstChild = (ViewGroup) getChildAt(0);
if (firstChild != null) {
subChildCount = firstChild.getChildCount();
for (int i = 0; i < subChildCount; i++) {
if (((View) firstChild.getChildAt(i)).getWidth() > 0) {
pointList.add(((View) firstChild.getChildAt(i)).getLeft() - firstChild.getPaddingLeft());
}
}
}
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
downX = (int) ev.getX();
break;
case MotionEvent.ACTION_MOVE: {
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL: {
if (Math.abs((ev.getX() - downX)) > getWidth() / 10) {
if (ev.getX() - downX > 0) {
smoothScrollToPrePage();
} else {
smoothScrollToNextPage();
}
} else {
smoothScrollToCurrent();
}
return true;
}
}
return super.onTouchEvent(ev);
}
private void smoothScrollToCurrent() {
smoothScrollTo(pointList.get(currentPage), 0);
}
private void smoothScrollToNextPage() {
if (currentPage < subChildCount - 1) {
currentPage++;
smoothScrollTo(pointList.get(currentPage), 0);
}
}
private void smoothScrollToPrePage() {
if (currentPage > 0) {
currentPage--;
smoothScrollTo(pointList.get(currentPage), 0);
}
}
/**
* 下一页
*/
public void nextPage() {
smoothScrollToNextPage();
}
/**
* 上一页
*/
public void prePage() {
smoothScrollToPrePage();
}
/**
* 跳转到指定的页面
*
* @param page
* @return
*/
public boolean gotoPage(int page) {
if (page > 0 && page < subChildCount - 1) {
smoothScrollTo(pointList.get(page), 0);
currentPage = page;
return true;
}
return false;
}
}
使用:
package com.lib.demo.viewpagedemo;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class HorizontalScrollViewActivity extends AppCompatActivity {
private LinearLayout mContainer = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_horizontal_scroll_view);
mContainer = (LinearLayout) findViewById(R.id.container);
int def = (getWinWidth() - dip2px(this, 30) - dip2px(this, 10)) / 2;
int w = def;
int h = (def / 4) * 3;
int num = 3;
for (int i = 0; i < num; i++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(w, h);
if (i == num - 1) {
params.rightMargin = 0;
} else {
params.rightMargin = dip2px(this, 10);
}
ImageView imageView1 = new ImageView(this);
imageView1.setLayoutParams(params);
imageView1.setImageResource(R.drawable.t02);
imageView1.setScaleType(ImageView.ScaleType.CENTER);
mContainer.addView(imageView1);
}
}
@Override
protected void onResume() {
// ((MyScrollView)mContainer.getParent()).init();
super.onResume();
}
private int getWinWidth() {
DisplayMetrics dm = new DisplayMetrics();
//获取屏幕信息
getWindowManager().getDefaultDisplay().getMetrics(dm);
return dm.widthPixels;
}
private int getWinHeight() {
DisplayMetrics dm = new DisplayMetrics();
//获取屏幕信息
getWindowManager().getDefaultDisplay().getMetrics(dm);
return dm.heightPixels;
}
public static int dip2px(Context context, float dpValue) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, context
.getResources().getDisplayMetrics());
}
}