0

我正在尝试实现与此类似的目标:

在此处输入图像描述

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

这是一个关于如何在长按时触发上下文菜单的代码片段。

我的活动课:

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>

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

4

1 回答 1

0

我会想象做这样的事情会在更大的屏幕上,对吧?但我想你可以让每个网格单元 aRelativeLayout并容纳那里的任何东西。然后当你长按时,添加另一个视图,可能是一个LinearLayout包含 4 个的视图,TextViews它可以说出你想要它说的内容。由于相对布局中的最低级别 View 会产生某种浮动效果,因此它可以工作。另外,您可以添加不透明度以使其透明。哦,我可能应该补充一点,Android 带有长按和放手功能,所以实现起来应该不会太糟糕。

于 2012-11-09T07:13:42.313 回答