当前位置:我的异常网» Android»Android TV HorizontalScrollView 图片轮播 遥控器
Android TV HorizontalScrollView 图片轮播 遥控器上下按键的处理
网友分享于:-07-22浏览:0次
Android TV HorizontalScrollView 图片轮播 遥控器左右按键的处理
/**
* 加载下一张图片
*/
protected void loadNextImg()
{
View view;
// int oldViewPos = mContainer.getChildCount() - 1;
// mViewPos.remove(mContainer.getChildAt(oldViewPos));
// mContainer.removeViewAt(oldViewPos);
//移除第一张图片,且将水平滚动位置置0
// scrollTo(0, 0);
mViewPos.remove(mContainer.getChildAt(0));
mContainer.removeViewAt(0);
//获取下一张图片,并且设置onclick事件,且加入容器中
Log.d("mContainer"," mCurrentIndex=" + mCurrentIndex + " mAdapter.getCount() = " + mAdapter.getCount() );
if(mCurrentIndex == mAdapter.getCount() - 1){
Log.d("mCurrentIndex","mCurrentIndex = " + mCurrentIndex);
Log.d("mAdapter.getCount() - 1","mAdapter.getCount() - 1 = " + mAdapter.getCount());
view = mAdapter.getView(0, null, mContainer);
Log.d("view","0 = " + 0);
mCurrentIndex = 0;
}else{
view = mAdapter.getView(++mCurrentIndex, null, mContainer);
Log.d("view","mCurrentIndex = " + mCurrentIndex);
}
view.setOnClickListener(this);
mContainer.addView(view);
Log.d("mCurrentIndex",mCurrentIndex + "");
// if(keyCodevalue == 1){
// mViewPos.put(view, mCurrentIndex - 1);
// } else{
mViewPos.put(view, mCurrentIndex);
// }
Log.d("mContainer","++++++++ mCurrentIndex=" + mCurrentIndex + " mAdapter.getCount() = " + mAdapter.getCount() );
//此处试验很多次 只能这样将诶绝了,但是滑动还有一点问题
scrollTo((findViewById(R.id.id_gallery).getWidth()), 0);
smoothScrollBy(-(findViewById(R.id.id_gallery).getWidth()), 0);
Log.d("id",(findViewById(R.id.id_gallery).getWidth()) + "");
Log.d("mChildWidth",mChildWidth + "");
//当前第一张图片小标
if(mFristIndex == mAdapter.getCount() - 1){
mFristIndex = 0;
} else {
mFristIndex++;
}
Log.d(TAG,mFristIndex + " ");
//如果设置了滚动监听则触发
Log.d("mContainer","++++++++ mCurrentIndex=" + mCurrentIndex + " mFristIndex = " + mFristIndex );
}
/**
* 加载前一张图片
*/
protected void loadPreImg()
{
int oldViewPos = mContainer.getChildCount() - 1;
Log.d("oldViewPos","oldViewPos = "+ oldViewPos);
Log.d("mFristIndex","mFristIndex = "+ mFristIndex);
if (mFristIndex == 0) {
mViewPos.remove(mContainer.getChildAt(oldViewPos));
mContainer.removeViewAt(oldViewPos);
View view = mAdapter.getView(mAdapter.getCount() - 1, null,
mContainer);
Log.d("view","mAdapter.getCount() - 1" + mAdapter.getCount());
mViewPos.put(view, mAdapter.getCount() - 1);
mContainer.addView(view, 0);
view.setOnClickListener(this);
// 水平滚动位置向左移动view的宽度个像素
scrollTo(0, 0);
Log.d("mChildWidth","mChildWidth ="+mChildWidth);
mFristIndex = mAdapter.getCount() - 1;
Log.d("mFristIndex","mFristIndex = "+mFristIndex);
} else {
int index = mCurrentIndex - mCountOneScreen;
Log.d("mCurrentIndex","mCurrentIndex "+ mCurrentIndex);
Log.d("mCountOneScreen","mCountOneScreen "+ mCountOneScreen);
Log.d("index","mCurrentIndex - mCountOneScreen = "+ index);
if (index >= 0) {
mViewPos.remove(mContainer.getChildAt(oldViewPos));
mContainer.removeViewAt(oldViewPos);
View view = mAdapter.getView(index, null, mContainer);
Log.d("view","index = " + index);
mViewPos.put(view, index);
mContainer.addView(view, 0);
view.setOnClickListener(this);
// 水平滚动位置向左移动view的宽度个像素
scrollTo(0, 0);
Log.d("mChildWidth","mChildWidth = "+mChildWidth);
mFristIndex--;
Log.d("mFristIndex","mFristIndex = "+mFristIndex);
} else {
mViewPos.remove(mContainer.getChildAt(oldViewPos));
mContainer.removeViewAt(oldViewPos);
View view = mAdapter.getView(index + mAdapter.getCount(), null,
mContainer);
Log.d("view","index + mAdapter.getCount() = " + index + mAdapter.getCount());
mViewPos.put(view, index + mAdapter.getCount());
mContainer.addView(view, 0);
view.setOnClickListener(this);
// 水平滚动位置向左移动view的宽度个像素
scrollTo(0, 0);
Log.d("mChildWidth","mChildWidth = "+mChildWidth);
// 当前位置--,当前第一个显示的下标--
mFristIndex--;
Log.d("mFristIndex","mFristIndex = "+mFristIndex);
}
}
mCurrentIndex = mViewPos
.get(mContainer.getChildAt(mCountOneScreen - 1));
Log.d("mCurrentIndex","mCurrentIndex = "+mCurrentIndex);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == event.KEYCODE_DPAD_LEFT){
Log.d("keyCodevalue","keyCodevalue = " + keyCodevalue);
if(keyCodevalue == 2){
Log.d("keyCodevalue","zhixing = " + keyCodevalue);
} else {
loadPreImg();
}
keyCodevalue = 1;
}else if(keyCode == event.KEYCODE_DPAD_RIGHT){
Log.d("keyCodevalue","keyCodevalue = " + keyCodevalue);
if(keyCodevalue == 1){
Log.d("keyCodevalue","zhixing = " + keyCodevalue);
} else {
loadNextImg();
}
keyCodevalue = 2;
if(keyCodevalue == 1){ //当之前按下的是鼠标左键的情况下,执行
for (int i = 0; i < mContainer.getChildCount(); i++)
{
mContainer.getChildAt(i).setBackgroundColor(Color.WHITE);
}
mListener.onCurrentImgChanged(mFristIndex, mContainer.getChildAt(0));
} else {
for (int i = 0; i < mContainer.getChildCount(); i++)
{
mContainer.getChildAt(i).setBackgroundColor(Color.WHITE);
}
mListener.onCurrentImgChanged(mFristIndex, mContainer.getChildAt(1));
}
return true;
}
if(keyCodevalue == 1){ //当之前按下的是鼠标左键的情况下,执行
for (int i = 0; i < mContainer.getChildCount(); i++)
{
mContainer.getChildAt(i).setBackgroundColor(Color.WHITE);
}
mListener.onCurrentImgChanged(mFristIndex, mContainer.getChildAt(0));
} else {
for (int i = 0; i < mContainer.getChildCount(); i++)
{
mContainer.getChildAt(i).setBackgroundColor(Color.WHITE);
}
mListener.onCurrentImgChanged(mFristIndex, mContainer.getChildAt(1));
}
Log.d("keyCodeValue","mFristIndex = " + mFristIndex);
return super.onKeyDown(keyCode, event);
}
文章评论