长按网格单元格内的网格样式菜单

时间:2012-11-09 07:06:33

标签: android android-gridview android-contextmenu

我正在努力实现类似的目标:

enter image description here

当用户长按网格视图中的网格单元格时,应该在网格单元格区域内绘制菜单,如上所示。如果有人尝试过类似的东西,或者有任何图书馆可以做同样的事情,请帮助我。

以下是关于长按时触发上下文菜单的代码片段。

我的活动课程:

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");
}

ImageAdapter类:

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

}

main.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>

mobile.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>

在这里尝试实现的是,长按网格单元时,它应该用上面的视图替换这个特定的网格单元视图,并且每个框(即视图,编辑,共享和删除)都应响应一些事件监听。

1 个答案:

答案 0 :(得分:0)

我想像这样的东西会在更大的屏幕上吗?但是我想你可以让每个网格单元格成为RelativeLayout并且可以存放任何网格单元格。然后,当您长按时,添加另一个视图,可能是LinearLayout,其中包含4个TextViews,表示您想要说出的内容。因为无论相对布局中的最低级别视图是否产生某种浮动效果,它都可以工作。另外,您可以添加不透明度以使其透视。哦,我应该补充一点,Android配备了长按和放开功能,所以这不应该太糟糕了。