在应用程序的主屏幕上,如何显示类似于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;
}
}
GridLayout的:
<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>
答案 0 :(得分:0)
既然您已经发布了代码,我不确定您是否可以在技术上删除但是您可以禁用您正在谈论的“突出显示”的点击,这样,当用户点击其中一个图标时,它将不再强调。
这可以通过XML或代码完成:
https://stackoverflow.com/questions/2865683/android-disable-highlighting-in-gridview
Code: GridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
XML: android:listSelector="#00000000"
但是,这会影响gridview中的所有图标。
另请看一下:
答案 1 :(得分:0)
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()中尝试此代码。