android获取gridview中的任何项目

时间:2014-08-01 13:48:01

标签: android gridview

我有一个带x个按钮,x行和4列的gridview。我试图删除或不可点击按钮甚至行..一行可能更好..我希望在我启动应用程序时进行此删除。我只希望gridview中的最后四个项目是可点击的,然后当点击其中一个项目时,我希望项目5,6,7,8只能被点击。我遇到了麻烦,因为我不明白如何在gridview中获取某个项目,所以我可以将它们设置为不可点击。到目前为止,我将向您展示代码:

Mainactivity:

public class MainActivity extends Activity {

private TextView text;
private GridView gridView;
private TextView timerValue;
private Boolean firstclick = false;
private long startTime = 0L;
private Handler customHandler = new Handler();
private Button buttonclicked;
Random r = new Random();
//private int[] rannumbers_array = new int[32];
private int[] shuffle_array = new int[4];
private String[] items = new String[8];

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    text = (TextView) findViewById(R.id.feedback);
    timerValue = (TextView) findViewById(R.id.timerValue);
    timerValue.setTextColor(Color.GREEN);
    int j = 0;
    int k = 0;
    int ranval = 0;
    for(int i = 0; i < 8; i++)
    {
        if ( (i % 4) == 0)
        {
            for(int x = 0; x < 4; x++)
            {
                shuffle_array[x] = 0;
            }
            do {
                ranval = r.nextInt(100);
            } while((ranval % 2) != 0);
        }
        else
        {
            do {
                ranval = r.nextInt(100);
            } while((ranval % 2) == 0); 
        }
        shuffle_array[j] = ranval;
        j++;
        if(j >= 4)
        {
            j = 0;
            shuffle(shuffle_array);
            do {
               items[k] = Integer.toString(shuffle_array[j]);
               k++;
               j++;
            } while(k <= i);
            j = 0;
        }
        //rannumbers_array[i] = r.nextInt(100);
        //items[i] = Integer.toString(rannumbers_array[i]);
    }
    k = 0;




    gridView = (GridView) this.findViewById(R.id.myGridView);
    final CustomGridAdapter gridAdapter = new CustomGridAdapter(MainActivity.this, items);
    gridView.setAdapter(gridAdapter);
    buttonclicked = gridAdapter.getbutton();
    gridView.post(new Runnable() {
        public void run() {
            gridView.setSelection(gridView.getCount() - 1);
        }
    });
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if(!firstclick)
            {
               customHandler.postDelayed(updateTimerThread, 0);
               firstclick = true;
            }
            gridAdapter.getItem(51); //can I make this not clickable or not visible?
            //view.invalidate();

我的CustomGridAdapter:

public class CustomGridAdapter extends BaseAdapter {

private Context context;
private String[] items;
private Button button;
LayoutInflater inflater;

public CustomGridAdapter(Context context, String[] items) {
    this.context = context;
    this.items = items;
    inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

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

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.cell, null);
    }
    button = (Button) convertView.findViewById(R.id.grid_item);
    button.setHeight(175);
    button.setText(items[position]);

    return convertView;
}

@Override
public int getCount() {
    return items.length;
}

@Override
public Object getItem(int position) {
    return items[position];
}

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

public Button getbutton()
{
    return button;
}

}

cell.xml:

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/grid_item"
android:layout_gravity="center"
android:maxHeight="175dip"
android:layout_marginLeft="20dip"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="05"
android:textSize="25sp"
android:clickable="false"
android:focusable="false"/>

activitymain.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame" >

<TextView
    android:id="@+id/timerValue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="2dp"
    android:text="@string/timerVal"
    android:textColor="#ffffff"
    android:textSize="30sp" />

<GridView
    android:id="@+id/myGridView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:columnWidth="90dp"
    android:horizontalSpacing="0dp"
    android:numColumns="4"
    android:verticalSpacing="0dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:id="@+id/feedback"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

有谁知道如何访问gridview中的任何项目或项目组?

0 个答案:

没有答案