欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android学习教程之动态GridView控件使用(6)

程序员文章站 2024-02-29 12:52:58
本文实例为大家分享了android动态gridview控件使用的具体代码,供大家参考,具体内容如下 mainactivity.java代码: package s...

本文实例为大家分享了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, 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内容:

<?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)

Android学习教程之动态GridView控件使用(6)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。