因为第一次发文字,有些地方还不是很懂就没发全.以后改进
这是一个自定义的HorizontalScrollView,能够在屏幕上只显示部分控件.可以进行点击滑动等操作.当然有其他需求的可以直接加.本人水平一般,有问题的地方欢迎指教,互相交流…
下面就直接上代码
package com.example.ljy.demo_1;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
LinearLayout llContain;
ScrollLinearLayout scroll;
private String[] str = {"我是测试1", "我是测试1", "我是测试", "呵呵哈嘿", "哼哼哈嘿", "哼哼哈嘿1", "哼哼哈嘿1"};
private int mWidth;
private int mTvWidth;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWidth = getWindowManager().getDefaultDisplay().getWidth();
llContain = findViewById(R.id.ll_contain);
scroll = findViewById(R.id.scroll);
llContain.setBackgroundColor(Color.RED);
initView();
scroll.setWindowWidth(mWidth);
}
/**
* 初始化子控件,并添加到容器里面去
*/
private void initView() {
for (int i = 0; i < str.length; i++) {
TextView tv = new TextView(this);
tv.setText(str[i]);
tv.setTextColor(Color.WHITE);
Log.d(TAG, "width: " + mWidth);
mTvWidth = (int) (mWidth / 5 + 0.5);
Log.d(TAG, "width: " + mTvWidth);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(mTvWidth, 30);
tv.setPadding(3,0,3,0);
// params.leftMargin = 3;
// params.rightMargin = 3;
llContain.addView(tv, params);
//判定如果不是需要的就隐藏,但是需要占位置,能够保证滑动,并且取消当前的点击事件
if (i < 2 || i > 4) {
tv.setVisibility(View.INVISIBLE);
tv.setEnabled(false);
}
final int finalI = i;
//在这里设置点击事件.可以直接点击,只给3个设置了点击事件
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (finalI){
case 2:
scroll.scrollTo(0,0);
break;
case 3:
scroll.scrollTo(mTvWidth,0);
break;
case 4:
scroll.scrollTo(mTvWidth*2,0);
break;
}
int scrollX = scroll.getScrollX();
Log.d(TAG, "onClick:scrollX " + scrollX);
}
});
}
int childCount = llContain.getChildCount();
Toast.makeText(this, "childCount: " + childCount, Toast.LENGTH_SHORT).show();
Log.d(TAG, "childCount: " + childCount);
}
}
---------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:background="#88000000"
android:orientation="vertical"
tools:context="com.example.ljy.demo_1.MainActivity">
<ImageView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:src="@drawable/white_point"/>
<com.example.ljy.demo_1.ScrollLinearLayout
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f00"
android:scrollbars="none">
<LinearLayout android:id="@+id/ll_contain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
</com.example.ljy.demo_1.ScrollLinearLayout>
</LinearLayout>
-------------------------------------------------------
下面的是自定义的是HorizontalScrollView
package com.example.ljy.demo_1;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import static android.R.attr.scrollX;
import static android.R.attr.x;
import static android.R.attr.y;
/**
* Created by ljy on 2017/9/29.
*/
public class ScrollLinearLayout extends HorizontalScrollView {
private static final String TAG = "ScrollLinearLayout";
private int width;
private int mScrollX;
public ScrollLinearLayout(Context context) {
super(context);
}
public ScrollLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ScrollLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public float rawX;
int widowWidth;
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
LinearLayout childAt = (LinearLayout) getChildAt(0);
int width = childAt.getChildAt(3).getWidth();
int width2 = width*2;
int a = (int) (width / 2 + 0.5);
int b = (int) (width * 1.5 + 0.5);
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
rawX = ev.getRawX();
Log.d(TAG, "rawX: " + rawX);
mScrollX = getScrollX();
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
//在抬起的事件里面,去判定按下时的滚动位置,以及手指滑动的距离,并通过距离的判断,来区分滑动到那个位置
float upRawX = ev.getRawX();
int diffX = (int) (upRawX - rawX + 0.5);
Log.d(TAG, "dispatchTouchEvent:diffX " + diffX);
Log.d(TAG, "scrollX: " + mScrollX);
Log.d(TAG, "dispatchTouchEvent: a " + a + " b " + b);
if (diffX >= a && diffX <= b) { //向右 移动了一格
if (mScrollX == 0) {
//不动
scrollTo(0, 0);
} else if (mScrollX < width2 && mScrollX > 0) {
scrollTo(0, 0);
} else {
scrollTo(width, 0);
}
} else if (diffX > b) {//向右 移动了2格
if (mScrollX == 0) {
//不动
scrollTo(0, 0);
} else if (mScrollX < width2 && mScrollX > 0) {
//滑动到第一个
scrollTo(0, 0);
} else {
//滑动到第一个
scrollTo(0, 0);
}
} else if (diffX > -a && diffX < a) { //不动
if (mScrollX == 0) {
//不动
scrollTo(0, 0);
} else if (mScrollX < width2 && mScrollX > 0) {
scrollTo(width, 0);
} else {
scrollTo(width2, 0);
}
} else if (diffX <= -a && diffX >= -b) {//向左 移动了一格
if (mScrollX == 0) {
//滑动到第2个
scrollTo(width, 0);
} else if (mScrollX < width2 && mScrollX > 0) {
//滑动到第3个
scrollTo(width2, 0);
} else {
//滑动到第3个
scrollTo(width2, 0);
}
} else if (diffX < -b) {//向左 移动了2格
if (mScrollX == 0) {
//滑动到第3个
scrollTo(width2, 0);
} else if (mScrollX < width2 && mScrollX > 0) {
//滑动到第3个
scrollTo(width2, 0);
} else {
//滑动到第3个
scrollTo(width2, 0);
}
}
break;
}
return super.dispatchTouchEvent(ev);
}
public void setWindowWidth(int width) {
this.widowWidth = width;
}
}