本文实例为大家分享了AndroID动态GrIDVIEw控件使用的具体代码,供大家参考,具体内容如下
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); //直接启动一个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相当于一个Layout的信息包,它封装了Layout的位置、高、宽等信息。假设在屏幕上一块区域是由一个Layout占领的,如果将一个VIEw添加到一个Layout中,最好告诉Layout用户期望的布局方式,也就是将一个认可的layoutParams传递进去。 /*可以这样去形容LayoutParams,在象棋的棋盘上,每个棋子都占据一个位置,也就是每个棋子都有一个位置的信息,如这个棋子在4行4列,这里的“4行4列”就是棋子的LayoutParams。 但LayoutParams类也只是简单的描述了宽高,宽和高都可以设置成三种值: 1,一个确定的值; 2,FILL_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,false); lp = new LayoutParams(v.getLayoutParams()); lp.span = 1; break; case 10: case 20: v = mInflater.inflate(R.layout.element_item_large,false); lp = new LayoutParams(v.getLayoutParams()); lp.span = 4; break; default: v = mInflater.inflate(R.layout.element_item,false); lp = new LayoutParams(v.getLayoutParams()); lp.span = 2; break; } v.setLayoutParams(lp); return v; } }}
activity_main.xml内容:
<?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="动态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内容:
<?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="做一点事..." /> <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="列表项文本在这里,图像上面" 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="列表项文本在这里,图像上面" 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="列表项文本在这里,图像上面" 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>
运行结果如图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android学习教程之动态GridView控件使用(6)全部内容,希望文章能够帮你解决Android学习教程之动态GridView控件使用(6)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)