结合了手势识别
码了一上午有点小收获
问题一:gridview不要再添加布局,否则会报错
问题二:要在gridview中再次添加手势识别,否则只能识别一次
- Activity代码
package com.phone.hty.myapplication;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.GridView;
import android.widget.Toast;
import android.widget.ViewSwitcher;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
public static final int NNMBER_PER_SCREEN = 16;
private ArrayList<DataItem> mItems = new ArrayList<DataItem>();
private int screenNo = -1;
private int screenCount;
ViewSwitcher mViewSwitcher;
LayoutInflater mInflater;
GestureDetector mDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
initLinster();
next();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return mDetector.onTouchEvent(event);
}
public void next() {
((GridView) (mViewSwitcher.getNextView()))
.setAdapter(new MyAdapter(getBaseContext(), mItems, screenNo, screenCount));
((GridView) (mViewSwitcher.getNextView())).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("hty","----"+screenNo);
return mDetector.onTouchEvent(event);
}
});
}
public void previous() {
((GridView) (mViewSwitcher.getNextView()))
.setAdapter(new MyAdapter(getBaseContext(), mItems, screenNo, screenCount));
((GridView) (mViewSwitcher.getNextView())).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("hty","----"+screenNo);
return mDetector.onTouchEvent(event);
}
});
}
private void initLinster() {
mDetector = new GestureDetector(this, new GestureDetector.OnGestureListener() {
@Override
public boolean onDown(MotionEvent e) {
return false;
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false;
}
@Override
public void onLongPress(MotionEvent e) {
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (e1.getRawX() - e2.getRawX() > 10) {
if (screenNo < screenCount - 1) {
screenNo++;
next();
mViewSwitcher.showNext();
Log.d("hty", "向左划动,下一页"+screenNo);
} else {
screenNo = -1;
Log.d("hty", "已经到达最后,现在冲重新开始");
Toast.makeText(getBaseContext(), "已经到达最后,现在冲重新开始",
Toast.LENGTH_SHORT).show();
}
}
if (e2.getRawX() - e1.getRawX() > 10) {
if (screenNo > 0) {
screenNo--;
previous();
Log.d("hty", "向右划动,上一页" + screenNo);
mViewSwitcher.showPrevious();
} else{
screenNo = 3;
Log.d("hty", "已经到达开始,现在冲转到最后"+screenNo);
Toast.makeText(getBaseContext(),"现在到达第一个。重新转到最后一瓶",
Toast.LENGTH_SHORT).show();
}
}
return true;
}
});
}
private void initData() {
mInflater = LayoutInflater.from(this);
TypedArray typedArray = getResources().obtainTypedArray(R.array.image);
int[] ache_iamge = new int[typedArray.length()];
for (int i = 0; i < typedArray.length(); i++) {
ache_iamge[i] = typedArray.getResourceId(i, 0);
}
//此步骤待用
/* String[] stringArray = getResources().getStringArray(R.array.desc);
String[] ache_desc = new String[stringArray.length];
for (int i = 0; i < stringArray.length; i++) {
ache_desc[i] = stringArray[i];
}*/
for (int i = 0; i < 50; i++) {
DataItem item = new DataItem(getResources()
.getStringArray(R.array.desc)[i], ache_iamge[i]);
mItems.add(item);
}
// 三木运算符第一个是一个boolean值然后是问号true对应的值:然后是flase对应的值
screenCount = mItems.size() % NNMBER_PER_SCREEN == 0 ? mItems.size() / NNMBER_PER_SCREEN :
mItems.size() / NNMBER_PER_SCREEN + 1;
mViewSwitcher = (ViewSwitcher) findViewById(R.id.view_switcher);
mViewSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
return mInflater.inflate(R.layout.gridview_layout, null);
}
});
}
}
- 列表内容
Adapter代码
package com.phone.hty.myapplication;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
/**
* 作者:司马啸尘
* 创建日期:2016/3/26
* 描述:
*/
public class MyAdapter extends BaseAdapter {
Context mContext;
ArrayList<DataItem> mItems;
private int ScreenNo = -1;
private int ScreenCount;
private int NUMBER_PER_SCREEN = 16;
public MyAdapter(Context context, ArrayList<DataItem> items, int screenNo, int screenCount) {
mContext = context;
mItems = items;
ScreenNo = screenNo;
ScreenCount = screenCount;
}
@Override
public int getCount() {
if (ScreenNo == ScreenCount - 1 && mItems.size() % NUMBER_PER_SCREEN != 0) {
System.out.println("-------------------");
return mItems.size() % NUMBER_PER_SCREEN;
} else
return NUMBER_PER_SCREEN;
}
@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 = View.inflate(mContext, R.layout.grid_view_item, null);
holder = new viewHolder();
holder.mImageView = (ImageView) convertView.findViewById(R.id.imageView);
holder.mTextView = (TextView) convertView.findViewById(R.id.textView);
convertView.setTag(holder);
} else {
holder = (viewHolder) convertView.getTag();
}
holder.mImageView.setImageResource(mItems.get(position).getImage());
holder.mTextView.setText(mItems.get(position).getDesc());
return convertView;
}
}
class viewHolder {
public ImageView mImageView;
public TextView mTextView;
}
好玩的是,没时间添加动画了,下午其他view在练习动画吧
周末快乐,图书馆周末的人永远比平时多,今天起晚了,老位置被抢了555