https://blog.csdn.net/sinat_29924199/article/details/53019128
本例子实现在GridView的最后固定显示一个增加图片,点击图片动态增加内容item。
1.main.xml:
-
<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" >
-
-
<GridView
-
android:id= "@+id/gv_test"
-
android:numColumns= "3"
-
android:layout_width= "match_parent"
-
android:layout_height= "match_parent" />
-
-
</RelativeLayout>
-
"1.0" encoding="utf-8" xml version=
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
android:layout_width= "match_parent"
-
android:layout_height= "match_parent"
-
android:orientation= "vertical" >
-
-
<ImageView
-
android:id= "@+id/item"
-
android:layout_width= "wrap_content"
-
android:layout_height= "wrap_content" />
-
package com.yayun.gridviewdemo;
-
-
import java.util.List;
-
-
import android.content.Context;
-
import android.view.LayoutInflater;
-
import android.view.View;
-
import android.view.ViewGroup;
-
import android.widget.BaseAdapter;
-
import android.widget.ImageView;
-
-
public class GridViewAdapter extends BaseAdapter {
-
private Context context;
-
private List<Integer> list;
-
LayoutInflater layoutInflater;
-
private ImageView mImageView;
-
-
public GridViewAdapter(Context context, List<Integer> list) {
-
this.context = context;
-
this.list = list;
-
layoutInflater = LayoutInflater.from(context);
-
}
-
-
-
public int getCount() {
-
return list.size()+ 1; //注意此处
-
}
-
-
-
public Object getItem(int position) {
-
return list.get(position);
-
}
-
-
-
public long getItemId(int position) {
-
return position;
-
}
-
-
-
public View getView(int position, View convertView, ViewGroup parent) {
-
-
convertView = layoutInflater.inflate(R.layout.grid_item, null);
-
mImageView = (ImageView) convertView.findViewById(R.id.item);
-
if (position < list.size()) {
-
mImageView.setBackgroundResource(list.get(position));
-
} else{
-
mImageView.setBackgroundResource(R.drawable.pic3); //最后一个显示加号图片
-
}
-
return convertView;
-
}
-
-
}
-
package com.yayun.gridviewdemo;
-
-
import java.util.ArrayList;
-
import java.util.List;
-
-
import android.os.Bundle;
-
import android.support.v7.app.ActionBarActivity;
-
import android.view.View;
-
import android.widget.AdapterView;
-
import android.widget.AdapterView.OnItemClickListener;
-
import android.widget.GridView;
-
import android.widget.Toast;
-
-
public class MainActivity extends ActionBarActivity {
-
private List<Integer> mDatas;
-
private GridView mGridView;
-
private GridViewAdapter adapter;
-
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
mGridView=(GridView) findViewById(R.id.gv_test);
-
-
initDatas();
-
adapter= new GridViewAdapter(MainActivity. this,mDatas);
-
mGridView.setAdapter(adapter);
-
mGridView.setOnItemClickListener( new OnItemClickListener() {
-
-
-
public void onItemClick(AdapterView<?> parent, View view,
-
int position, long id) {
-
if(position==parent.getChildCount()- 1){
-
mDatas.add(R.drawable.pic1);
-
Toast.makeText(MainActivity. this, "您点击了添加", 1).show();
-
adapter= new GridViewAdapter(MainActivity. this, mDatas);
-
mGridView.setAdapter(adapter);
-
adapter.notifyDataSetChanged();
-
}
-
-
}
-
});
-
-
}
-
private void initDatas() {
-
mDatas= new ArrayList<>();
-
mDatas.add(R.drawable.pic1);
-
mDatas.add(R.drawable.pic1);
-
mDatas.add(R.drawable.pic1);
-
mDatas.add(R.drawable.pic1);
-
mDatas.add(R.drawable.pic1);
-
-
}
-
-
-
}