在 GridView Android 中实现 Multi Select
Implementing Multi Select in GridView Android
我需要在 GridView
中 select 多张图片,并在 Gridview
中添加分享和删除按钮。
我的ImageAdapter
class:
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﹕
FATAL EXCEPTION: main
Process: com.android.example, PID: 21658
java.lang.ClassCastException: android.widget.ImageView cannot be cast to com.android.example.GridViewActivity$CheckableLayout
at com.android.example.GridViewActivity$ImageAdapter.getView(GridViewActivity.java:224)
at android.widget.AbsListView.obtainView(AbsListView.java:2255)
at android.widget.GridView.onMeasure(GridView.java:1044)
at android.view.View.measure(View.java:16540)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:689)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:473)
at android.view.View.measure(View.java:16540)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:851)
at android.view.View.measure(View.java:16540)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16540)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16540)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16540)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16540)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
at android.view.View.measure(View.java:16540)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1942)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1132)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1321)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1019)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5725)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
ClassCastException: android.widget.ImageView cannot be cast to
com.android.example.GridViewActivity$CheckableLayout
因为您正在从 getView
returning ImageView
但试图在这一行中将其转换为 CheckableLayout
:
checkableLayout = (CheckableLayout) convertView;
return 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;
}
你应该 return convertView
来自 getView
方法。
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// code here
convertview.addView(checkableLayout);
return convertView;
}
我需要在 GridView
中 select 多张图片,并在 Gridview
中添加分享和删除按钮。
我的ImageAdapter
class:
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﹕ FATAL EXCEPTION: main Process: com.android.example, PID: 21658 java.lang.ClassCastException: android.widget.ImageView cannot be cast to com.android.example.GridViewActivity$CheckableLayout at com.android.example.GridViewActivity$ImageAdapter.getView(GridViewActivity.java:224) at android.widget.AbsListView.obtainView(AbsListView.java:2255) at android.widget.GridView.onMeasure(GridView.java:1044) at android.view.View.measure(View.java:16540) at android.widget.RelativeLayout.measureChild(RelativeLayout.java:689) at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:473) at android.view.View.measure(View.java:16540) at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:851) at android.view.View.measure(View.java:16540) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at android.view.View.measure(View.java:16540) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) at android.widget.LinearLayout.measureVertical(LinearLayout.java:695) at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) at android.view.View.measure(View.java:16540) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at android.view.View.measure(View.java:16540) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) at android.widget.LinearLayout.measureVertical(LinearLayout.java:695) at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) at android.view.View.measure(View.java:16540) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291) at android.view.View.measure(View.java:16540) at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1942) at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1132) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1321) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1019) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5725) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) at android.view.Choreographer.doCallbacks(Choreographer.java:574) at android.view.Choreographer.doFrame(Choreographer.java:544) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5086) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
ClassCastException: android.widget.ImageView cannot be cast to com.android.example.GridViewActivity$CheckableLayout
因为您正在从 getView
returning ImageView
但试图在这一行中将其转换为 CheckableLayout
:
checkableLayout = (CheckableLayout) convertView;
return 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;
}
你应该 return convertView
来自 getView
方法。
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// code here
convertview.addView(checkableLayout);
return convertView;
}