我想知道如何禁用Gridview
中的一组项目的点击。
这是班级:
public class LevelSelectActivity extends Activity {
public GridView gridView;
public TextView Easy;
public TextView Medium;
public TextView Hard;
public Button mBackButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level_select);
Easy = (TextView) findViewById(R.id.EasyText);
Easy.setTypeface(ApplicationServices.typeFaceDoubleBlast);
Medium = (TextView) findViewById(R.id.MediumText);
Medium.setTypeface(ApplicationServices.typeFaceDoubleBlast);
Hard = (TextView) findViewById(R.id.HardText);
Hard.setTypeface(ApplicationServices.typeFaceDoubleBlast);
mBackButton = ( Button ) findViewById ( R.id. BackButton );
mBackButton.setOnClickListener(mBack_Clicked);
gridView = (GridView) findViewById(R.id.LevelSelectGrid);
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Sending image id to FullScreenActivity
Intent i = new Intent(getApplicationContext(), DoubleBlastGameActivity.class);
// passing array index
if(position >= 0 && position < 4)
{
i.putExtra("level", 1);
}
else if(position >= 4 && position < 8)
{
i.putExtra("level", 2);
}
else if(position >= 8 && position < 12)
{
i.putExtra("level", 3);
}
startActivity(i);
}
});
}
public View.OnClickListener mBack_Clicked = new View.OnClickListener() {
public void onClick(View v) {
Intent intend = new Intent(getBaseContext(), MainMenuActivity.class );
startActivity(intend);
}
};
public class ImageAdapter extends BaseAdapter {
private Context context;
public Integer[] mLevelIcon;
public String[] mLevelId;
public ImageAdapter(Context contextTemp) {
this.context = contextTemp;
mLevelId = new String[12];
mLevelIcon = new Integer[12];
for (int i = 0; i < mLevelId.length; i++) {
mLevelId[i] = Integer.toString(i + 1);
if(i + 1 <= ApplicationServices.gameConfigStructure.lastCompletedLevelInt )
mLevelIcon[i] = R.drawable.levelbutton;
else
mLevelIcon[i] = R.drawable.lock;
}
}
public int getCount() {
// TODO Auto-generated method stub
return mLevelId.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
gridView = inflater.inflate(R.layout.level_button, null);
TextView textview = new TextView(context);
textview = (TextView) gridView.findViewById(R.id.LevelID);
textview.setText(mLevelId[position]);
textview.setTypeface(ApplicationServices.typeFaceDoubleBlast);
if(position >= ApplicationServices.gameConfigStructure.lastCompletedLevelInt )
{
textview.setVisibility(View.INVISIBLE);
}
LevelSelectButton button = new LevelSelectButton(context);
button = (LevelSelectButton) gridView
.findViewById(R.id.levelSelectButton1);
button.setImageResource(mLevelIcon[position]);
} else {
gridView = convertView;
}
return gridView;
}
}
}
这是水平选择屏幕的屏幕截图:
正如您所看到的那样,第一级已解锁,理想情况下我只希望解锁级别可以点击。但目前允许进入任何级别。
如何关闭锁定级别的点击?
答案 0 :(得分:3)
您应该修改getView()
课程的ImageAdapter
。您需要将setEnabled(false)
或View.setFocusable(false)
和View.setClickable(false)
添加到项目视图中,即您的案例中的gridView。
if (position >= ApplicationServices.gameConfigStructure.lastCompletedLevelInt) {
textview.setVisibility(View.INVISIBLE);
gridView.setEnabled(false); // Set enable false to these views
// Or combination of below
// gridView.setFocusable(false);
// gridView.setClickable(false);
}
另一种方法是覆盖Adapter类的isEnabled()
,因为在他的回答中添加了user543,如
@Override
public boolean isEnabled(int position) {
if (position >= ApplicationServices.gameConfigStructure.lastCompletedLevelInt) {
return false;
}
return true;
}
答案 1 :(得分:2)
您可以在ImageAdapter中覆盖isEnabled(int position),例如:
@Override
public boolean isEnabled(int position) {
//check the position to be locked or not
//if yes then return true, else return false
return true/false;
}