Android使⽤HorizontalScrollView实现⽔平滚动HorizontalScrollView 和 ScrollView 都是由 FrameLayout 派⽣出来的。它们就是⼀个⽤于为普通组件添加滚动条的组件。且HorizontalScrollView 和 ScrollView ⾥⾯最多只能包含⼀个组件(当然组件⾥⾯还可以嵌套组件)。它们不同的是HorizontalScrollView ⽤于添加⽔平滚动,⽽ ScrollView ⽤于添加垂直滚动。
突然间想到做⼀个屏幕下⽅⽔平滑动,屏幕上⽅并作出相应的反应的效果。只是在下⽅滚动时,屏幕上⽅没有作出理想的反应,点击事件倒是实现了。最终只能在⽹上搜索,终于到了⼀个。于是作出的效果如下:
只是这个效果还有所缺陷,加载了 13 张图⽚,在屏幕下⽅⽔平滚动到最后⼀页时,第 9 张的图⽚并没有在上⾯的显⽰出来(原作者的也有这个问题);如果图⽚的数量⼩于或者等于 4 张时则不能运⾏。
本例的难点主要在于 MyHorizontalView 类中,并且还有收集⽽来的注解。
MainActivity.java :
azy.horizontalscrollviewtest;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
azy.horizontalscrollviewtest.MyHorizontalView.CurrentImageChangeListener;
azy.horizontalscrollviewtest.MyHorizontalView.OnItemClickListener;
t.Context;
aphics.Bitmap;
aphics.BitmapFactory;
aphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private ImageView mImageView;
private MyHorizontalView myHorizontalView;
private List<Bitmap> bitmapList;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
init();
}
private void init() {
mImageView = (ImageView)findViewById(R.id.imageView);
bitmapList = new ArrayList<>(Arrays.asList(
readBitMap(this, R.drawable.bricks),
readBitMap(this, R.drawable.dog),
readBitMap(this, R.drawable.flower),
readBitMap(this, ass),
readBitMap(this, R.drawable.stones),
readBitMap(this, R.drawable.wood),
readBitMap(this, R.drawable.bg_01),
readBitMap(this, R.drawable.bg_02),
readBitMap(this, R.drawable.bg_03),
readBitMap(this, R.drawable.bg_04),
readBitMap(this, R.drawable.bg_05),
readBitMap(this, R.drawable.bg_06),
readBitMap(this, R.drawable.bg_07)
));
myHorizontalView = (MyHorizontalView)findViewById(_horizontal);
adapter = new MyAdapter(this, bitmapList);
//设置适配器
myHorizontalView.initDatas(adapter);
//添加滚动回调
myHorizontalView
.setCurrentImageChangeListener(new CurrentImageChangeListener() {
@Override
public void onCurrentImgChanged(int position, View viewIndicator) {
Log.e("==============","=============== " + position);
mImageView.(position));
viewIndicator.setBackgroundColor(Color.parseColor("#AA024DA4"));
}
});
/
/添加点击回调
myHorizontalView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
mImageView.(position));
view.setBackgroundColor(Color.parseColor("#AA024DA4"));
}
});
}
public static Bitmap readBitMap(Context mContext, int resId) {
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
InputStream is = Resources().openRawResource(resId);
return BitmapFactory.decodeStream(is, null, opt);
}
}
MyAdapter 这部分并不是为 AbsListView 和 AbsSpinner 及其⼦类提供列表项的。它主要⽤于为 HorizontalScrollView 提供数据。
MyAdapter.java :
azy.horizontalscrollviewtest;
import java.util.ArrayList;
import java.util.List;
t.Context;
aphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
/**
* Created by antimage on 2016/1/9.
*/
public class MyAdapter extends BaseAdapter {
泰山小吃private List<Bitmap> bitmapList;
private Context mContext;
public MyAdapter(Context context, List<Bitmap> bitmapList) {
mContext = context;
if (bitmapList == null) {
bitmapList = new ArrayList<Bitmap>();
} else {
this.bitmapList = bitmapList;
}哦字组词
}
@Override
public int getCount() {
return bitmapList.size();
}
@Override
public Object getItem(int position) {
(position);
}
@Override
东南方有哪些城市public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
View view = null;
// 此处要⽤相对布局,且与 XML 中的布局相同;
// 如果使⽤线性布局,则显⽰不完整
RelativeLayout layout;
if (convertView == null) {
layout = (RelativeLayout) View.inflate(mContext, R.layout.image_item_layout, null);
viewHolder = new ViewHolder();
红花郎酒价格表viewHolder.image = (ImageView) layout.findViewById(p_image);
layout.setTag(viewHolder);
} else {
layout = (RelativeLayout) convertView;
view = layout;
viewHolder = (ViewHolder) Tag();
Log.e("MyAdapter", "正在检测数据来了没有 ");
}
Bitmap btm = (Bitmap) getItem(position);
viewHolder.image.setImageBitmap(btm);
Log.e("MyAdapter", "信息来了哦!");
return layout;
}
private static class ViewHolder {
ImageView image;
}
}
MyHorizontalView 类主要⽤于未 MainAcitivity 类提供接⼝、⽔平滚动时屏幕上⽅的反应及相应的点击事件等。该类主要使⽤了收集⽽来的代码,并做了相应的调整。
MyHorizontalView.java :
azy.horizontalscrollviewtest;
import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
t.Context;
aphics.Color;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
/
**
* Created by antimage on 2016/1/9.
*/
public class MyHorizontalView extends HorizontalScrollView
implements View.OnClickListener {
private String TAG = "MyHorizontalView";
private ViewGroup parent;
private int screenWidth;
/* 当前最后⼀张图⽚的index*/
private int mCurrentIndex;
/* 当前第⼀张图⽚的下标*/
初三祝福语简短private int mFristIndex;
/* 每屏幕最多显⽰的个数*/
private int mCountOneScreen;
/* ⼦元素的宽度*/
private int mChildWidth;
/* ⼦元素的⾼度*/
private int mChildHeight;
private MyAdapter mAdapter;
private CurrentImageChangeListener mListener;
private OnItemClickListener mOnItemClickListener;
/**
* 图⽚滚动时的回调接⼝
*/
public interface CurrentImageChangeListener {
void onCurrentImgChanged(int position, View viewIndicator);
}
/**
* 点击条⽬时的回调
*/
public interface OnItemClickListener {
void onItemClick(View view, int pos);
}
/
* 保存View与位置的键值对 */
private Map<View, Integer> mViewPos = new HashMap<>();
public MyHorizontalView(Context context) {
this(context, null);
}
public MyHorizontalView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyHorizontalView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.setSmoothScrollingEnabled(true);
DisplayMetrics metrics = new DisplayMetrics();
// 取得窗⼝属性
((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metrics); // 窗⼝的宽度(像素)
screenWidth = metrics.widthPixels;
}
/**
* 初始化数据,设置数据适配器
*/
public void initDatas(MyAdapter mAdapter) {
if (getChildCount() == 0) {
Log.e(TAG, "必须要有⼦元素");
}
if (getChildCount() == 0 || mAdapter == null)
return;
this.mAdapter = mAdapter;
parent = (ViewGroup) getChildAt(0);
// 获得适配器中第⼀个View
final View view = View(0, null, parent);
parent.addView(view);
// 强制计算当前View的宽和⾼
if (mChildWidth == 0 && mChildHeight == 0) {
int w = View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED);
int h = View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED);
mChildHeight = MeasuredHeight();
mChildWidth = MeasuredWidth();
Log.e(TAG, "⼦组件的宽:" + mChildWidth + ", ⼦组件的⾼:" + mChildHeight); // 计算每次加载多少个View
mCountOneScreen = screenWidth / mChildWidth + 2;
Log.e(TAG, "mCountOneScreen = " + mCountOneScreen
+ " ,mChildWidth = " + mChildWidth);
}
//初始化第⼀屏幕的元素
loadFirstChild(mCountOneScreen);
}
/**
* 加载第⼀屏的View
*/
public void loadFirstChild(int mCountOneScreen) {
mViewPos.clear();
for (int i = 0; i < mCountOneScreen; i++) {
View view = View(i, null, parent);
view.setOnClickListener(this);
parent.addView(view);
mViewPos.put(view, i);
mCurrentIndex = i;
}
if (mListener != null) {
notifyCurrentImageChanged();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (Action()) {
case MotionEvent.ACTION_MOVE:
int scrollX = getScrollX();
// 如果当前scrollX为view的宽度,加载下⼀张,移除第⼀张
if (scrollX >= mChildWidth) {
loadNextImage();
}
// 如果当前scrollX = 0,往前设置⼀张,移除最后⼀张
if (scrollX == 0) {
loadPreImage();
}
break;
}
// 这⾥⽆论返回值是设置 true 还是 false
// HorizontalScrollView都不会滑动
TouchEvent(event);
}
/**
* 加载下⼀张图⽚
*/
protected void loadNextImage() {
// 数组边界值计算
if (mCurrentIndex == Count() - 1) {
return;
学校出纳工作总结}
//移除第⼀张图⽚,且将⽔平滚动位置置0(图⽚有宽度,所以为置0)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论