編輯:關於Android編程
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; } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。
一、概述ListView點擊item顯示菜單是要實現這樣的效果:需要實現的邏輯如下:1)點擊一個普通item,展開當前菜單,同時關閉其他菜單2)點擊一個已展開的菜單,隱藏
前沿: 如果對SurfaceFlinger架構的工作原理較為熟悉的話,本文閱讀起來會相對容易些。之所以撰寫本文是因為在閱讀Camera HAL3的實現過程中
本文參考借鑒:http://www.jb51.net/article/102983.htm先上效果圖:自定義控件:AttendanceProgressBar代碼如下: p
話不多說,我們先來看看效果:Hi前輩搜索預覽這一張是《Hi前輩》的搜索預覽圖,你可以在這裡下載這個APP查看更多效果:http://www.wandoujia.com/a