我正在尝试实现与此类似的目标:
当用户在网格视图中长按网格单元格时,菜单应在网格单元格区域内绘制,如上所示。如果有人曾经尝试过类似的事情,或者是否有任何图书馆可以做同样的事情,请帮助我。
这是一个关于如何在长按时触发上下文菜单的代码片段。
我的活动课:
public class GridViewActivity extends Activity {
GridView gridView;
ImageAdapter mImageAdapter;
static final String[] MOBILE_OS = new String[] {
"Android", "iOS", "Windows", "Blackberry"
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mImageAdapter = new ImageAdapter(this, MOBILE_OS);
gridView = (GridView)findViewById(R.id.gridView1);
registerForContextMenu(gridView);
gridView.setAdapter(mImageAdapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(getApplicationContext(),
((TextView)v.findViewById(R.id.grid_item_label)).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onCreateContextMenu(ContextMenu iMenu, View iView, ContextMenuInfo iMenuInfo) {
super.onCreateContextMenu(iMenu, iView, iMenuInfo);
iMenu.setHeaderTitle("OPTIONS");
iMenu.clear();
iMenu.add(Menu.NONE, 0, Menu.NONE, "VIEW");
iMenu.add(Menu.NONE, 1, Menu.NONE, "EDIT");
iMenu.add(Menu.NONE, 2, Menu.NONE, "SHARE");
iMenu.add(Menu.NONE, 3, Menu.NONE, "DELETE");
}
图像适配器类:
public class ImageAdapter extends BaseAdapter {
private Context context;
private final String[] mobileValues;
public LayoutInflater inflater;
public LinearLayout linearLayout;
public ImageAdapter(Context context, String[] mobileValues) {
this.context = context;
this.mobileValues = mobileValues;
}
public View getView(int position, View convertView, ViewGroup parent) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from mobile.xml
gridView = inflater.inflate(R.layout.mobile, null);
// set value into textview
linearLayout = (LinearLayout)gridView.findViewById(R.id.linear1);
TextView textView = (TextView) gridView
.findViewById(R.id.grid_item_label);
textView.setText(mobileValues[position]);
// set image based on selected text
ImageView imageView = (ImageView) gridView
.findViewById(R.id.grid_item_image);
String mobile = mobileValues[position];
if (mobile.equals("Windows")) {
imageView.setImageResource(R.drawable.windows_logo);
} else if (mobile.equals("iOS")) {
imageView.setImageResource(R.drawable.ios_logo);
} else if (mobile.equals("Blackberry")) {
imageView.setImageResource(R.drawable.blackberry_logo);
} else {
imageView.setImageResource(R.drawable.android_logo);
}
} else {
gridView = (View) convertView;
}
return gridView;
}
@Override
public int getCount() {
return mobileValues.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
}
主.xml:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridView1"
android:numColumns="auto_fit"
android:gravity="center"
android:columnWidth="100dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</GridView>
移动.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:orientation="vertical" >
<ImageView
android:id="@+id/grid_item_image"
android:layout_width="50px"
android:layout_height="50px"
android:layout_marginRight="10px"
android:src="@drawable/android_logo" >
</ImageView>
<TextView
android:id="@+id/grid_item_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:layout_marginTop="5px"
android:textSize="15px" >
</TextView>
</LinearLayout>
这里要实现的是,在网格单元上长按时,它应该用上面的视图替换这个特定的网格单元视图,并且每个框(即查看、编辑、共享和删除)都应该响应某个事件侦听器。