提前声明:本篇博客是基于电视机顶盒的,全部操作是用遥控器。
转载请注明出处:http://blog.csdn.net/harryweasley/article/details/46811981
在看这篇文章之前,你有必要去了解一下我的上一篇文章,http://blog.csdn.net/harryweasley/article/details/46784667(gridview整页翻页的滑动,上滑和下滑)
本篇文章,是基于上一篇文章的升华。
建立一个类,其中的方法如下所示:
package com.example.test;
import android.view.KeyEvent;
public class GridViewItemOrderUtil {
/**
* 判断是否(到某一页的完整最后一行并且点击向下)
*
*
* @param size
* 总共的项目数目,从1开始
* @param columnNum
* 列数目,即每一行有多少列,从1开始
* @param position
* 当前的位置,从0开始
* @param rawInPage
* 每一页有完整的几行,从1开始
* @param keyCode
*
* @param event
*/
public static boolean allPageDown(int size, int columnNum, int position,
int rawInPage, int keyCode, KeyEvent event) {
// 每一个页有多少个完整的item
int itemAPage = columnNum * rawInPage;
// 每一个页的最后一行的第一个数字
int num = itemAPage - columnNum;
// 求出一共有多少页
int rawNum = size / itemAPage;
for (int i = 1; i <= rawNum; i++) {
for (int j = 0; j < columnNum; j++) {
// 每行有多少列,进行多少次相加
if (position == num + itemAPage * (i - 1) + j) {
if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN
&& event.getAction() == KeyEvent.ACTION_DOWN) {
// 如果有相等,则立即返回true,不再进行判断
return true;
}
}
}
}
return false;
}
/**
* 判断是否(到某一页的第一行并且点击向上按钮)
*
*
* @param size
* 总共的项目数目,从1开始
* @param columnNum
* 列数目,即每一行有多少列,从1开始
* @param position
* 当前的位置,从0开始
* @param rawInPage
* 每一页有完整的几行,从1开始
* @param keyCode
*
* @param event
*/
public static boolean allPageUp(int size, int columnNum, int position,
int rawInPage, int keyCode, KeyEvent event) {
// 每一个页有多少个完整的item
int itemAPage = columnNum * rawInPage;
// 求出一共有多少页+1
int rawNum = size / columnNum + 1;
for (int i = 1; i <= rawNum; i++) {
for (int j = 0; j < columnNum; j++) {
// 每行有多少列,进行多少次相加
if (position == itemAPage*i + j) {
if (keyCode == KeyEvent.KEYCODE_DPAD_UP
&& event.getAction() == KeyEvent.ACTION_DOWN) {
// 如果有相等,则立即返回true,不再进行判断
return true;
}
}
}
}
return false;
}
}
mainActivity中进行了如下的改变:
package com.example.test;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
GridView gv;
/**
* 选择的position的值
*/
int selected;
/**
* 是否有向上的动画
*/
boolean isUp = false;
/**
* 是否有向下的动画
*/
boolean isDown = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gv = (GridView) findViewById(R.id.gridview);
gv.setAdapter(new MyBaseAdapter(this));
gv.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView> parent, View view,
int position, long id) {
selected = position;
if (isDown) {
isDown = false;
gv.smoothScrollToPositionFromTop(position, 0, 1000);
}
if (isUp) {
isUp = false;
// 让该页的第一行对齐最顶边,这里的3是当前position减去的行数等于当前页的第一行
gv.smoothScrollToPositionFromTop(position - 3, 0, 1000);
}
}
@Override
public void onNothingSelected(AdapterView> parent) {
}
});
gv.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
Log.i("tag", "这里进行测试"+selected);
isDown=GridViewItemOrderUtil.allPageDown(10, 3,selected, 2, keyCode, event);
isUp=GridViewItemOrderUtil.allPageUp(10, 3,selected, 2, keyCode, event);
return false;
}
});
}
class MyBaseAdapter extends BaseAdapter {
private Context context;
ArrayList list = new ArrayList();
public MyBaseAdapter(Context context) {
this.context = context;
for (int i = 0; i < 20; i++) {
list.add(i);
}
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(
R.layout.item_program, parent, false);
holder = new ViewHolder();
holder.imageView = (ImageView) convertView
.findViewById(R.id.program_image);
holder.name = (TextView) convertView
.findViewById(R.id.program_name);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText("应用" + list.get(position));
return convertView;
}
class ViewHolder {
ImageView imageView;
TextView name;
}
}
}