本文实例为大家分享了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内容:
<&#63;xml version="1.0" encoding="utf-8"&#63;>
activity_staggeredgridview.xml内容:
<&#63;xml version="1.0" encoding="utf-8"&#63;>
其他:
element_header.xml
<&#63;xml version="1.0" encoding="utf-8"&#63;>
element_item.xml:
<&#63;xml version="1.0" encoding="utf-8"&#63;>
element_item_large.xml
<&#63;xml version="1.0" encoding="utf-8"&#63;>
element_item_small.xml
<&#63;xml version="1.0" encoding="utf-8"&#63;>
bg_white_box.xml
<&#63;xml version="1.0" encoding="utf-8"&#63;>
运行结果如图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。