gpt4 book ai didi

Android 自定义 gridview 在每个网格项目上都有 (imagebutton + imageview)

转载 作者:行者123 更新时间:2023-11-30 01:44:26 25 4
gpt4 key购买 nike

我正在尝试将 imageButton 和 ImageView 放在 GridView 项上,所以这是我的代码如下所示,我想做的是创建一个 Adapter 扩展 BaseAdapter,然后创建一个 Viewholder 并将 ImageView 和 Imagebutton 放在 holder 中。但是,没有错误,但是当 GridView 打开时,应用程序再次停止。

任何线索都会有很大帮助...谢谢!!

public class DestinationActivity extends Activity implements OnClickListener, OnItemClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_view);
GridView grid = (GridView) findViewById(R.id.gvGrid);
myImageAdapter = new ImageAdapter(this);
grid.setAdapter(myImageAdapter);
}

class ViewHolder {
int id;
ImageView imageview;
ImageButton button;
}

public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;

public ImageAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

private Context mContext;
ArrayList<String> itemList = new ArrayList<String>();

public ImageAdapter(Context c) {
mContext = c;
}

void add(String path) {
itemList.add(path);
}

@Override
public int getCount() {
return itemList.size();
}

@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 position;
}

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.image_button, null);
holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
holder.button = (ImageButton) convertView.findViewById(R.id.ibClose);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();

}

holder.button.setId(position);
holder.imageview.setId(position);
holder.imageview.setPadding(1, 1, 1, 1);
holder.imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
holder.imageview.setLayoutParams(new GridView.LayoutParams(330, 330));
Bitmap bm = decodeSampledBitmapFromUri(itemList.get(position),
330, 330);
holder.imageview.setImageBitmap(bm);
holder.button.setOnClickListener(new ImageButton.OnClickListener() {

@Override
public void onClick(View v) {
// // TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Hello..!!", Toast.LENGTH_SHORT).show();

}
});

holder.imageview.setImageBitmap(null);
holder.id = position;
return convertView ;

}
}

image_button.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ImageView
android:id="@+id/thumbImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />

<ImageButton
android:id="@+id/ibClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/close_dialog" />

</RelativeLayout>

GridView .xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<GridView
android:id="@+id/gvGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:clipToPadding="false"
android:columnWidth="10dp"
android:gravity="center"
android:horizontalSpacing="0dp"
android:numColumns="2"
android:scrollbarStyle="outsideOverlay"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp"
/>
</LinearLayout>

enter image description here

最佳答案

代替下面几行:

convertView = mInflater.inflate(R.layout.gallerydeleteitems, null);
holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
holder.imageview = (ImageButton) convertView.findViewById(R.id.ibClose);

更改为:

convertView = mInflater.inflate(R.layout.image_button, null); //layout name changed
holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
holder.button= (ImageButton) convertView.findViewById(R.id.ibClose); //changed the name to holder.button

关于Android 自定义 gridview 在每个网格项目上都有 (imagebutton + imageview),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33934892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com