GridView是类似于ListView的控件,只是GridView可以使用多个列来呈现内容,而ListView是以行为单位,所以用法上是差不多的。 主布局文件,因为要做下拉刷
GridView是类似于ListView的控件,只是GridView可以使用多个列来呈现内容,而ListView是以行为单位,所以用法上是差不多的。
主布局文件,因为要做下拉刷新,所以加了一个ProgressBar,GridView的numColumns属性是指每一行有多少列
<RelativeLayout xmlns:Android="Http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.girdlayoutdemo.MainActivity" >
<ProgressBar
android:id="@+id/pb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
<GridView
android:layout_below="@id/pb"
android:id="@+id/gv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:gravity="center"
android:numColumns="2" >
</GridView>
</RelativeLayout>
每个Item的布局文件,这里比较简单的一张图片加一段文字
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/item_iv"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="3" />
<TextView
android:id="@+id/item_tv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
主活动代码:
public class MainActivity extends Activity {
private GridView gv;
private ProgressBar pb;
private List<Map<String, Object>> list;
private SimpleAdapter adapter;
private GestureDetector gsDetector;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
pb.setVisibility(View.GoNE);
Toast.makeText(MainActivity.this, "刷新成功", 200).show();
break;
default:
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pb = (ProgressBar) findViewById(R.id.pb);
gv = (GridView) findViewById(R.id.gv);
pb.setVisibility(View.GONE);
initData();
adapter = new SimpleAdapter(this, list, R.layout.item_layout,
new String[] { "image", "text" }, new int[] { R.id.item_iv,
R.id.item_tv });
gv.setAdapter(adapter);
gsDetector = new GestureDetector(this, new Mlistener());
gv.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// Log.e("MainActivity", event.getX()+"");
return gsDetector.onTouchEvent(event);
}
});
}
private void initData() {
list = new ArrayList<Map<String, Object>>();
for (int i = 0; i < 20; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("image", R.drawable.gift_item_default);
map.put("text", "一只冰莹猪猪");
list.add(map);
}
}
class Mlistener implements 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 (e2.getY() - e1.getY() > 0 && gv.getFirstVisiblePosition() == 0) {
pb.setVisibility(View.VISIBLE);
Animation animation = new ScaleAnimation(1f, 1f, 0, 1f);
animation.setDuration(300);
pb.startAnimation(animation);
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
Message msg = new Message();
msg.what = 1;
handler.sendMessage(msg);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
return false;
}
}
}
解析:
在onCreate方法中先获取两个组件,然后把progressBar设置为隐藏,下拉的时候再显示,刷新完毕再隐藏。然后为GridView设置数据源,这里方便起见用SimpleAdapter,然后给GridView设置ontouchListener,并在onTouch方法中把触摸事件交给我们自定义的GestureDetector对象来处理,在GestureDetector的onFling方法中处理下拉事件,在里面判断是否下拉以及GridView是否在最顶端,如果是,显示progressBar控件并开一个线程来处理刷新,这里做模拟就睡眠2000毫秒,最后用message对象返回一个消息给Handler,Handler在主线程中更新GridView。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pb = (ProgressBar) findViewById(R.id.pb);
gv = (GridView) findViewById(R.id.gv);
pb.setVisibility(View.GONE);
initData();
adapter = new SimpleAdapter(this, list, R.layout.item_layout,
new String[] { "image", "text" }, new int[] { R.id.item_iv,
R.id.item_tv });
gv.setAdapter(adapter);
gsDetector = new GestureDetector(this, new Mlistener());
gv.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// Log.e("MainActivity", event.getX()+"");
return gsDetector.onTouchEvent(event);
}
});
}
class Mlistener implements 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 (e2.getY() - e1.getY() > 0 && gv.getFirstVisiblePosition() == 0) {
pb.setVisibility(View.VISIBLE);
Animation animation = new ScaleAnimation(1f, 1f, 0, 1f);
animation.setDuration(300);
pb.startAnimation(animation);
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
Message msg = new Message();
msg.what = 1;
handler.sendMessage(msg);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
return false;
}
}
您可能感兴趣的文章:Android自定义View实现可以拖拽的GridViewAndroid GridView不改变背景色实现网格线效果Android GridView仿微信朋友圈显示图片Android中实现多行、水平滚动的分页的Gridview实例源码android ListView和GridView拖拽移位实现代码Android之ScrollView嵌套ListView和GridView冲突的解决方法android GridView多选效果的实例代码Android GridView实现滚动到指定位置的方法android中GridView的用法示例Android学习教程之动态GridView控件使用(6)
--结束END--
本文标题: Android下拉刷新以及GridView使用方法详解
本文链接: https://www.lsjlt.com/news/24641.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0