Android學(xué)習(xí)教程之動(dòng)態(tài)GridView控件使用(6)
本文實(shí)例為大家分享了Android動(dòng)態(tài)GridView控件使用的具體代碼,供大家參考,具體內(nèi)容如下
MainActivity.java代碼:
package siso.haha; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { private Button btnStaggeredGridView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnStaggeredGridView=(Button)findViewById(R.id.btnStaggeredGridView); btnStaggeredGridView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(MainActivity.this,staggeredgridviewActivity.class); //直接啟動(dòng)一個(gè)Activity startActivity(intent); } }); } }
staggeredgridviewActivity.java代碼:
package siso.haha; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import java.util.Random; import viewHelper.StaggeredGridView; import viewHelper.StaggeredGridView.LayoutParams; public class staggeredgridviewActivity extends Activity { private final static String TAG = staggeredgridviewActivity.class.getSimpleName(); private StaggeredGridView mSGV; private SGVAdapter mAdapter; private StaggeredGridView.OnScrollListener mScrollListener = new StaggeredGridView.OnScrollListener() { @Override public void onScrollStateChanged(ViewGroup view, int scrollState) { Log.d(TAG, "[onScrollStateChanged] scrollState:" + scrollState); switch (scrollState) { case SCROLL_STATE_IDLE: setTitle("SCROLL_STATE_IDLE"); break; case SCROLL_STATE_FLING: setTitle("SCROLL_STATE_FLING"); break; case SCROLL_STATE_TOUCH_SCROLL: setTitle("SCROLL_STATE_TOUCH_SCROLL"); break; default: break; } } @Override public void onScroll(ViewGroup view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { Log.d(TAG, "[onScroll] firstVisibleItem:" + firstVisibleItem + " visibleItemCount:"+visibleItemCount + " totalItemCount:" + totalItemCount); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_staggeredgridview); mAdapter = new SGVAdapter(this); mSGV = (StaggeredGridView) findViewById(R.id.grid); mSGV.setColumnCount(4); mSGV.setAdapter(mAdapter); mSGV.setOnScrollListener(mScrollListener); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. // getMenuInflater().inflate(R.menu.activity_main, menu); return true; } private final class SGVAdapter extends BaseAdapter { LayoutInflater mInflater; public SGVAdapter(Context ctx) { mInflater = LayoutInflater.from(ctx); } @Override public int getCount() { return 30; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } Random r = new Random(); @Override public View getView(int position, View convertView, ViewGroup parent) { //LayoutParams相當(dāng)于一個(gè)Layout的信息包,它封裝了Layout的位置、高、寬等信息。假設(shè)在屏幕上一塊區(qū)域是由一個(gè)Layout占領(lǐng)的,如果將一個(gè)View添加到一個(gè)Layout中,最好告訴Layout用戶期望的布局方式,也就是將一個(gè)認(rèn)可的layoutParams傳遞進(jìn)去。 /*可以這樣去形容LayoutParams,在象棋的棋盤上,每個(gè)棋子都占據(jù)一個(gè)位置,也就是每個(gè)棋子都有一個(gè)位置的信息,如這個(gè)棋子在4行4列,這里的“4行4列”就是棋子的LayoutParams。 但LayoutParams類也只是簡(jiǎn)單的描述了寬高,寬和高都可以設(shè)置成三種值: 1,一個(gè)確定的值; 2,F(xiàn)ILL_PARENT,即填滿(和父容器一樣大小); 3,WRAP_CONTENT,即包裹住組件就好。*/ final LayoutParams lp; final View v; switch (position) { case 0: case 29: v = mInflater.inflate(R.layout.element_header, parent, false); lp = new LayoutParams(v.getLayoutParams()); lp.span = mSGV.getColumnCount(); break; case 8: case 9: case 18: case 19: v = mInflater.inflate(R.layout.element_item_small, parent, false); lp = new LayoutParams(v.getLayoutParams()); lp.span = 1; break; case 10: case 20: v = mInflater.inflate(R.layout.element_item_large, parent, false); lp = new LayoutParams(v.getLayoutParams()); lp.span = 4; break; default: v = mInflater.inflate(R.layout.element_item, parent, false); lp = new LayoutParams(v.getLayoutParams()); lp.span = 2; break; } v.setLayoutParams(lp); return v; } } }
activity_main.xml內(nèi)容:
<?xml version="1.0" encoding="utf-8"?> <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" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="siso.haha.MainActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="動(dòng)態(tài)GridView" android:id="@+id/btnStaggeredGridView" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button2" android:layout_below="@+id/btnStaggeredGridView" android:layout_centerHorizontal="true" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button3" android:layout_below="@+id/button2" android:layout_centerHorizontal="true" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button4" android:layout_below="@+id/button3" android:layout_centerHorizontal="true" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button5" android:layout_below="@+id/button4" android:layout_centerHorizontal="true" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button6" android:layout_below="@+id/button5" android:layout_centerHorizontal="true" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button7" android:layout_below="@+id/button6" android:layout_centerHorizontal="true" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button8" android:layout_below="@+id/button7" android:layout_centerHorizontal="true" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button9" android:layout_below="@+id/button8" android:layout_centerHorizontal="true" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button10" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" /> </RelativeLayout>
activity_staggeredgridview.xml內(nèi)容:
<?xml version="1.0" encoding="utf-8"?> <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=".staggeredgridviewActivity" > <viewHelper.StaggeredGridView android:id="@+id/grid" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
其他:
element_header.xml
<?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="56dp" android:background="@drawable/bg_white_box" android:gravity="center_vertical" android:orientation="horizontal" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="做一點(diǎn)事..." /> <ProgressBar android:id="@+id/progressBar1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout>
element_item.xml:
<?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="wrap_content" android:background="@drawable/bg_white_box" android:orientation="vertical" android:padding="2dp" > <ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="100dp" android:src="@android:color/holo_green_light" /> <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="56dp" android:layout_margin="8dp" android:drawableRight="@android:drawable/ic_menu_info_details" android:gravity="center_vertical" android:lines="2" android:text="列表項(xiàng)文本在這里,圖像上面" android:textAppearance="?android:attr/textAppearance" /> </LinearLayout>
element_item_large.xml
<?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="wrap_content" android:background="@drawable/bg_white_box" android:orientation="vertical" android:padding="2dp" > <ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="160dp" android:src="@android:color/holo_orange_light" /> <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp" android:drawableRight="@android:drawable/ic_menu_info_details" android:gravity="center_vertical" android:lines="2" android:text="列表項(xiàng)文本在這里,圖像上面" android:textAppearance="?android:attr/textAppearance" /> </LinearLayout>
element_item_small.xml
<?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="wrap_content" android:background="@drawable/bg_white_box" android:orientation="vertical" android:padding="2dp" > <ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="100dp" android:src="@android:color/holo_red_light" /> <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="56dp" android:layout_margin="8dp" android:drawableRight="@android:drawable/ic_menu_info_details" android:gravity="center_vertical" android:lines="2" android:text="列表項(xiàng)文本在這里,圖像上面" android:textAppearance="?android:attr/textAppearance" /> </LinearLayout>
bg_white_box.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <solid android:color="@android:drawable/screen_background_light" /> <stroke android:width="1dp" android:color="@android:color/holo_blue_dark" /> </shape>
運(yùn)行結(jié)果如圖:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android下拉刷新以及GridView使用方法詳解
- Android自定義View實(shí)現(xiàn)可以拖拽的GridView
- Android GridView不改變背景色實(shí)現(xiàn)網(wǎng)格線效果
- Android GridView仿微信朋友圈顯示圖片
- Android中實(shí)現(xiàn)多行、水平滾動(dòng)的分頁的Gridview實(shí)例源碼
- android ListView和GridView拖拽移位實(shí)現(xiàn)代碼
- Android之ScrollView嵌套ListView和GridView沖突的解決方法
- android GridView多選效果的實(shí)例代碼
- Android GridView實(shí)現(xiàn)滾動(dòng)到指定位置的方法
- android中GridView的用法示例
相關(guān)文章
Android RecyclerView實(shí)現(xiàn)懸浮吸頂、分隔線、到底提示效果
這篇文章主要介紹了Android RecyclerView實(shí)現(xiàn)懸浮吸頂、分隔線、到底提示效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01Android編程Widget創(chuàng)建與使用方法簡(jiǎn)明教程
這篇文章主要介紹了Android編程Widget創(chuàng)建與使用方法,結(jié)合實(shí)例形式分析了Widget的功能、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-10-10Android音頻開發(fā)之SurfaceView的使用詳解
這篇文章主要為大家介紹了Android中SurfaceView的使用方法,本文通過簡(jiǎn)要的案例,為大家進(jìn)行了詳細(xì)的講解,需要的朋友可以參考一下2022-04-04android事件分發(fā)機(jī)制的實(shí)現(xiàn)原理
本篇文章主要介紹了android事件分發(fā)機(jī)制的實(shí)現(xiàn)原理,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09Android基于OkHttp實(shí)現(xiàn)文件上傳功能
這篇文章主要為大家詳細(xì)介紹了Android基于OkHttp實(shí)現(xiàn)文件上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07Android 活動(dòng)條ActionBar的詳解及實(shí)例代碼
這篇文章主要介紹了Android 活動(dòng)條ActionBar的詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-12-12Android入門之動(dòng)態(tài)BroadCast的使用教程
系統(tǒng)自己在很多時(shí)候都會(huì)發(fā)送廣播,比如電量低或者充足,剛啟動(dòng)完,插入耳機(jī),你有一條新的微信消息,這種都是使用BroadCast機(jī)制去實(shí)現(xiàn)的。BroadCast分為靜態(tài)和動(dòng)態(tài)BroadCast兩種,本文就來聊聊動(dòng)態(tài)BroadCast的使用,需要的可以參考一下2022-12-12Android Native fdsan檢測(cè)工具介紹
這篇文章主要為大家介紹了Android Native fdsan檢測(cè)工具介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02Android ListView 默認(rèn)選中某一項(xiàng)實(shí)現(xiàn)代碼
這篇文章主要介紹了Android ListView 默認(rèn)選中某一項(xiàng)實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09