本次的Demo是通过GridView展示一个List,通过2个按钮实现GridView从视觉上的增加和减少。
为了方便增加和减少,本次用的储存结构是List。与String在BaseAdapter一样,只需要更改BaseAdapter的2个基本函数分别是
getItem(),getCount()
public Object getItem(int position) { return mad[position]; } public int getCount() { return mad.length; }
这是String的 BaseAdapter
@Override public Object getItem(int position) { // TODO Auto-generated method stub return mList.get(position); } @Override public int getCount() { // TODO Auto-generated method stub return mList.size(); }
这是List的BaseAdapter
下边放上整个代码
package com.example.onclickgridview; import java.security.acl.LastOwnerException; import java.util.ArrayList; import java.util.List; import android.R.integer; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.widget.Adapter; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.GridView; import android.widget.ImageView; import android.widget.ListAdapter; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { // 作用: // 1、对于一个没有被载入或者想要动态载入的界面, 都需要使用inflate来载入. // // 2、对于一个已经载入的Activity, 就可以使用实现了这个Activiyt的的findViewById方法来获得其中的界面元素. private LayoutInflater mInflater; //创建初始化数据源 private String[] mad = {"1","2","3","4","5","6","7","8","9"}; private List<String> mList; //声明控件 private GridView mGridView; //声明adpter private Adapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //关联 Button btnAdd = (Button) findViewById(R.id.button1); Button btnDel = (Button) findViewById(R.id.button2); mGridView = (GridView) findViewById(R.id.gridView1); mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); mList = new ArrayList<String>(); for(int i = 0; i<mad.length; i++){ mList.add(mad[i]); } //新建BaseAdapter mAdapter = new BaseAdapter() { @Override public View getView(int position, View convertView, ViewGroup parent) { TextView text; ImageView imgview; RelativeLayout rLayout; //初始化时加载页面用到的 if里的东西 if (convertView == null) { rLayout = (RelativeLayout) mInflater.inflate(R.layout.new_file, parent, false); } else { rLayout = (RelativeLayout) convertView; } String item = (String) getItem(position); //设置控件取回的数据 text = (TextView) rLayout.findViewById(R.id.TextView2); text.setText(item); imgview = (ImageView) rLayout.findViewById(R.id.ImageView1); imgview.setImageResource(R.drawable.ic_launcher); return rLayout; } //本次重点 准确返回数据位置的关键 必须在getItemID设置对的位置选项默认从0开始 @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return mList.get(position); } @Override public int getCount() { // TODO Auto-generated method stub return mList.size(); } }; mGridView.setAdapter((ListAdapter) mAdapter); //监听事件 对于按下gridView控件的其中一个做出的响应 mGridView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position,long id) { Toast.makeText(MainActivity.this, "你选择了" + (position + 1) + " 号图片", Toast.LENGTH_SHORT).show(); } }); //监听2个按钮 btnAdd.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { //通过对list的长度在选择累计增加的数值 int j= mList.size(); //这里用到了类型转换 String s = Integer.toString(j+1); mList.add(s); mGridView.setAdapter((ListAdapter) mAdapter); } }); btnDel.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { //通过对list的长度在选择累计减少的数值 int z= mList.size(); //这里用到了类型转换 String t = Integer.toString(z); mList.remove(t); mGridView.setAdapter((ListAdapter) mAdapter); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
展示效果