我需要在GridView
中选择多个图片,然后在Gridview
添加共享和删除按钮。
我的ImageAdapter
课程:
private class ImageAdapter extends BaseAdapter {
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ImageView imageView;
CheckableLayout checkableLayout;
if (convertView == null) {
imageView = new ImageView(activity);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
checkableLayout = new CheckableLayout(activity);
imageView.setLayoutParams(new GridView.LayoutParams(imageWidth, imageWidth));
} else {
checkableLayout = (CheckableLayout) convertView;
imageView = (ImageView) checkableLayout.getChildAt(0);
}
bitmap = decodeFile(getItem(position), imageWidth, imageWidth);
imageView.setImageBitmap(bitmap);
imageView.setOnClickListener(new OnImageClickListener(position));
return imageView;
}
class OnImageClickListener implements View.OnClickListener {
int position;
public OnImageClickListener(int position) {
this.position = position;
}
@Override
public void onClick(View v) {
Intent intent = new Intent(activity, ImageDisplayActivity.class);
intent.putExtra("id", getPosition(position));
activity.startActivity(intent);
}
}
public class CheckableLayout extends FrameLayout implements Checkable {
private boolean mChecked;
public CheckableLayout(Context context) {
super(context);
}
@Override
public void setChecked(boolean checked) {
mChecked = checked;
setBackgroundDrawable(checked ? getResources().getDrawable(R.drawable.blue) : null);
}
@Override
public boolean isChecked() {
return mChecked;
}
@Override
public void toggle() {
setChecked(!mChecked);
}
}
public class MultiChoiceModeListener implements GridView.MultiChoiceModeListener {
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
int selectCount = gridView.getCheckedItemCount();
switch (selectCount) {
case 1:
mode.setSubtitle("One item selected");
break;
default:
mode.setSubtitle("" + selectCount + " items selected");
break;
}
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.setTitle("Select Items");
mode.setSubtitle("One item selected");
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return true;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
}
}
}
但是当我尝试打开Gridview时,它会给我一个错误。
我的Logcat详细信息
02-10 21:10:46.965 21658-21658 / com.android.example E / AndroidRuntime: 致命异议:主要 处理:com.android.example,PID:21658 java.lang.ClassCastException:android.widget.ImageView无法强制转换为com.android.example.GridViewActivity $ CheckableLayout 在com.android.example.GridViewActivity $ ImageAdapter.getView(GridViewActivity.java:224) 在android.widget.AbsListView.obtainView(AbsListView.java:2255) 在android.widget.GridView.onMeasure(GridView.java:1044) 在android.view.View.measure(View.java:16540) 在android.widget.RelativeLayout.measureChild(RelativeLayout.java:689) 在android.widget.RelativeLayout.onMeasure(RelativeLayout.java:473) 在android.view.View.measure(View.java:16540) 在android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:851) 在android.view.View.measure(View.java:16540) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 在android.view.View.measure(View.java:16540) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137) 在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) 在android.widget.LinearLayout.measureVertical(LinearLayout.java:695) 在android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 在android.view.View.measure(View.java:16540) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 在android.view.View.measure(View.java:16540) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137) 在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) 在android.widget.LinearLayout.measureVertical(LinearLayout.java:695) 在android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 在android.view.View.measure(View.java:16540) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 在com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2291) 在android.view.View.measure(View.java:16540) 在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1942) 在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1132) 在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1321) 在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1019) 在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5725) 在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:761) 在android.view.Choreographer.doCallbacks(Choreographer.java:574) 在android.view.Choreographer.doFrame(Choreographer.java:544) 在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:747) 在android.os.Handler.handleCallback(Handler.java:733) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:136) 在android.app.ActivityThread.main(ActivityThread.java:5086) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
答案 0 :(得分:2)
您应该从convertView
方法返回getView
。
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// code here
convertview.addView(checkableLayout);
return convertView;
}
答案 1 :(得分:0)
ClassCastException:android.widget.ImageView无法强制转换为 com.android.example.GridViewActivity $ CheckableLayout
因为您从ImageView
返回getView
,但是尝试将其转换为此行中的CheckableLayout
:
checkableLayout = (CheckableLayout) convertView;
从checkableLayout
方法返回getView()
:
@Override
public View getView(final int position,
View convertView, ViewGroup parent) {
final ImageView imageView;
CheckableLayout checkableLayout;
if (convertView == null) {
imageView = new ImageView(activity);
// your code here ...
// add ImageView to checkableLayout
checkableLayout.addView(imageView);
} else
checkableLayout = (CheckableLayout) convertView;
imageView = (ImageView) checkableLayout.getChildAt(0);
// your code here ...
return checkableLayout;
}