3

在应用程序的主屏幕上,如何显示类似于 android 菜单但不需要在特定单元格中显示项目的菜单。考虑到 3 x 3 的网格,只需在 (Row, Col) 处显示五个项目:[0,1]、[1,0]、[1,1]、[1,2]、[2,1] .

我们已经尝试了 GridView 并将不需要显示的项目的可见性设置为 GONE (convertView.setVisibility(View.GONE);)。在此之后,项目不会显示在网格中,但是当用户使用上下键浏览空白项目或直接单击空白项目时,该图标会突出显示并被选中,就好像它是网格中的空白项目一样。我们希望它是空白的,它不应该响应用户事件,既没有突出显示也没有选择。

网格视图代码:

package org.XXX;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class XXXActivity extends Activity {
    GridView MyGrid;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maingrid);
        MyGrid = (GridView)findViewById(R.id.MyGrid);
        MyGrid.setAdapter(new ImageAdapter(this));


        MyGrid.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
                Toast.makeText(arg0.getContext(), position + " selected", Toast.LENGTH_LONG).show();

                switch(position) {
                case 0:break;
                case 1:
                    //Browse
                    Intent newIntent = new Intent(XXXActivity.this, YYYListItemIcons.class);
                    startActivity(newIntent);
                    break;
                case 2:break;
                case 3:
                    //Saved Searches
                    newIntent = new Intent(XXXActivity.this, ZZZListItemIcons.class);
                    startActivity(newIntent);
                    break;
                case 4:
                    //Sign in
                    break;
                case 5:
                    //Reminders
                    break;
                case 6:break;
                case 7:
                    //Sign up
                    break;
                case 8:break;
                }
            }

        });

        //onSearchRequested(); //to open search by default
    }


    public class ImageAdapter extends BaseAdapter
    {
        Context MyContext;

        public ImageAdapter(Context _MyContext)
        {
            MyContext = _MyContext;
        }

        @Override
        public int getCount()
        {
            return 9;
        }

        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder;
            LayoutInflater mInflater = LayoutInflater.from(MyContext);

            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.grid_item, null);
                holder = new ViewHolder();
                holder.text = (TextView) convertView.findViewById(R.id.grid_item_text);
                holder.icon = (ImageView) convertView.findViewById(R.id.grid_item_image);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            // Bind the data efficiently with the holder.
            holder.text.setText(getTextId(position));
            holder.icon.setImageBitmap(BitmapFactory.decodeResource(MyContext.getResources(), getIconId(position)));
            if(getIconId(position) == R.drawable.nothing) {
                convertView.setVisibility(View.GONE);
            }
            return convertView;
        }

        @Override
        public Object getItem(int arg0) {
            return arg0;
        }

        @Override
        public long getItemId(int arg0) {
            return arg0;
        }

        private int getIconId(int position) {
            int iconImages[] = {
                    R.drawable.nothing,
                    R.drawable.browse,
                    R.drawable.nothing,
                    R.drawable.saved_searches,
                    R.drawable.sign_in,
                    R.drawable.reminders,
                    R.drawable.nothing,
                    R.drawable.sign_up,
                    R.drawable.nothing
            };
            return iconImages[position];
        }

        private int getTextId(int position) {
            int iconNames[] = {
                    R.string.nothing,
                    R.string.browse,
                    R.string.nothing,
                    R.string.saved_searches,
                    R.string.sign_in,
                    R.string.reminders,
                    R.string.nothing,
                    R.string.sign_up,
                    R.string.nothing
            };
            return iconNames[position];
        }

    }

    static class ViewHolder {
        TextView text;
        ImageView icon;
    }

}

网格布局:

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/MyGrid"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="center"
    android:padding="5dp"
    android:verticalSpacing="5dp"
    android:horizontalSpacing="20dp"
    android:numColumns="auto_fit"
    android:columnWidth="60dp"
    android:stretchMode="columnWidth"
    android:gravity="center">
</GridView>            

网格中的 PerItemIconLayout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/GridItem"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:orientation="vertical"
   android:gravity="center_horizontal">

   <ImageView android:id="@+id/grid_item_image"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">
   </ImageView>

   <TextView android:id="@+id/grid_item_text"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="TextView"
      android:gravity="center_horizontal"
      android:textColor="#FFFFFF">
   </TextView>

</LinearLayout>
4

2 回答 2

1
if(getIconId(position) == R.drawable.nothing) {
convertView.setVisibility(View.GONE);
}

用下面替换上面的行并尝试....

if(getIconId(position) == R.drawable.nothing) {
   convertView.setVisibility(View.GONE);
   convertView.setClickable(false);
   convertView.setEnabled(false);
}

在 getview() 中尝试此代码。

于 2012-04-05T05:32:43.173 回答
0

现在您已经发布了代码,我不确定您是否可以从技术上删除,但您可以禁用您正在谈论的“突出显示”点击,这样,当用户点击其中一个图标时,它将不再强调。

这可以通过 XML 或在您的代码中完成:

https://stackoverflow.com/questions/2865683/android-disable-highlighting-in-gridview

Code: GridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
XML: android:listSelector="#00000000"

但是,这将影响 gridview 中的所有图标。

也看看这个:

https://stackoverflow.com/questions/5514629/how-to-disable-item-click-for-particular-positions-in-grid-view-in-android

于 2012-04-05T04:51:46.193 回答