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

所属分类: 软件编程 / Android 阅读数: 23
收藏 0 赞 0 分享

本文实例为大家分享了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中加入名片扫描功能实例代码

这篇文章主要介绍了Android中加入名片扫描功能实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信发表说说实现拍照、多图上传功能

这篇文章主要为大家详细介绍了Android仿微信发表说说实现拍照、多图上传功能,使用Retrofit2.0技术,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

设置Android系统永不锁屏永不休眠的方法

在进行Android系统开发的时候,有些特定的情况需要设置系统永不锁屏,永不休眠。本篇文章给大家介绍Android 永不锁屏,开机不锁屏,删除设置中休眠时间选项,需要的朋友一起学习吧
收藏 0 赞 0 分享

Android Retrofit 2.0框架上传图片解决方案

这篇文章主要介绍了Android Retrofit 2.0框架上传一张与多张图片解决方案,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义等待对话框

这篇文章主要为大家详细介绍了Android自定义等待对话框的实现方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Window添加View的底层原理

这篇文章主要介绍了Android中Window添加View的底层原理,需要的朋友可以参考下
收藏 0 赞 0 分享

Android调用系统默认浏览器访问的方法

这篇文章主要介绍了Android调用系统默认浏览器访问的方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发退出程序的方法汇总

Android程序有很多Activity,比如说主窗口A,调用了子窗口B,子窗口B又调用子窗口C,back返回子窗口B后,在B中如何关闭整个Android应用程序呢? 下面脚本之家小编就给大家介绍android开发退出程序的几种方法,感兴趣的朋友参考下吧
收藏 0 赞 0 分享

Android程序开发中单选按钮(RadioGroup)的使用详解

在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

这篇文章主要介绍了Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多