使用BaseAdapter为GridView动态配置内容

实现效果:根据数组内容动态绘制gridview,同时每个item都有长按、单击事件,长按事件里又嵌套新的单击事件。

xml代码:

app_grid_view_item.xml,用于配置gridview中一个item 的布局:


    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" 
    android:paddingBottom="4dip"
    android:layout_width="fill_parent">
            android:layout_width="wrap_content"
        android:layout_centerHorizontal="true"
        android:id="@+id/itemImage" >
   

            android:layout_below="@+id/itemImage"
        android:layout_height="wrap_content" 
        android:text="TextView01"
        android:layout_centerHorizontal="true" 
        android:id="@+id/itemText">
   

            android:layout_width="wrap_content"
        android:layout_centerHorizontal="true"
        android:id="@+id/deleteView" 
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:src="@drawable/delete">
   



activity_main.xml布局文件,用于配置gridview的布局:

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >
        android:id="@+id/wholeView"  
    android:layout_width="fill_parent"   
    android:layout_height="fill_parent"  
    android:numColumns="auto_fit"  
    android:verticalSpacing="5dp"  
    android:horizontalSpacing="5dp"  
    android:columnWidth="90dp"  
    android:stretchMode="columnWidth"  
    android:gravity="center"  
   />  
 


Activity代码:

       private int countOfApps;

private List bits = new ArrayList();     //注意:这两个表示gridview中内容的数组是放在最外层类中的,便于访问。
private List labels = new ArrayList();
private GridView mGridview;


MySimpleAdapter myadapter = new MySimpleAdapter(MainActivity.this);
mGridview.setAdapter(myadapter);
mGridview.setOnItemClickListener(this);


public final class AppViewHolder {
public ImageView img;
public ImageView delete;
public TextView title;
}



public class MySimpleAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private Context context = null;


public MySimpleAdapter(Context contextpara) {
this.context = contextpara;
mInflater = LayoutInflater.from(context);
}

              //决定gridview中有多少个item
@Override
public int getCount() {
// TODO Auto-generated method stub
return countOfApps + 1;
}


@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}


@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}

//注意:getView每过一段时间被调用一次,其作用是刷新gridview 中的单个item,至于gridview中有多少个item,则是由getCount()函数决定的
@Override

public View getView(int position, View convertView, ViewGroup parent) {

// TODO Auto-generated method stub
AppViewHolder holder = null;
if (convertView == null) {
holder = new AppViewHolder();
convertView = mInflater.inflate(R.layout.app_grid_view_item,
null);
holder.img = (ImageView) convertView
.findViewById(R.id.itemImage);
holder.title = (TextView) convertView
.findViewById(R.id.itemText);
holder.delete = (ImageView) convertView
.findViewById(R.id.deleteView);
convertView.setTag(holder);


} else {
holder = (AppViewHolder) convertView.getTag();
}
holder.delete.setVisibility(View.GONE);
holder.img.setImageBitmap(bits.get(position));
if (position < countOfApps) {
holder.img.setLongClickable(true);
holder.img.setOnLongClickListener(new OnShowDeleteListener(
holder, position));
}
holder.title.setText(labels.get(position));
return convertView;
}


public class OnShowDeleteListener implements OnLongClickListener {
private AppViewHolder holder = null;
private int position;


public OnShowDeleteListener(AppViewHolder holderPara,
int positionPara) {
this.holder = holderPara;
this.position = positionPara;
}


@Override
public boolean onLongClick(View arg0) {
// TODO Auto-generated method stub
holder.delete.setImageResource(R.drawable.delete);
holder.delete.setVisibility(View.VISIBLE);
holder.delete
.setOnClickListener(new OnDeleteListener(position));


return false;
}
}


public class OnDeleteListener implements OnClickListener {
private int position;


public OnDeleteListener(int positionPara) {
this.position = positionPara;
}


@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub


bits.remove(position);
labels.remove(position);
countOfApps--;
notifyDataSetChanged();//当gridview中的内容改变时,调用notifyDataSetChanged函数重新绘制gridview
File[] files = new File(parentPath).listFiles();
deteleFile(files[position]);
{
Toast.makeText(getApplicationContext(), "卸载成功",
Toast.LENGTH_SHORT).show();
}


}


public void deteleFile(File file) {


if (file.exists()) { // 判断文件是否存在
if (file.isFile()) { // 判断是否是文件
file.delete(); // delete()方法 你应该知道 是删除的意思;
} else if (file.isDirectory()) { // 否则如果它是一个目录
File files[] = file.listFiles(); // 声明目录下所有的文件 files
for (int i = 0; i < files.length; i++) { // 遍历目录下所有的文件
this.deteleFile(files[i]); // 把每个文件 用这个方法进行迭代
}
}
file.delete();
}


}


}
}

你可能感兴趣的:(使用BaseAdapter为GridView动态配置内容)