如何获取在网格视图中单击的togglebutton的位置?

时间:2011-02-18 03:11:59

标签: android gridview togglebutton

如何获取在gridview中单击的android togglebutton的位置?

如果我使用以下示例代码,则不会收到该位置的Toast消息。

public class HelloGridView extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ToggleButtonAdapter(this));

    gridview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position,
                long id) {
            Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
        }

    });
}

}

切换适配器:

public class ToggleButtonAdapter extends BaseAdapter {
private Context mContext;

public ToggleButtonAdapter(Context c) {
    mContext = c;
}

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

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view;
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {  // reuse it if it already exists
        view = (View) inflater.inflate(R.layout.items, null);

    } else {
        view = (View) convertView;
    }

    ToggleButton toggleButton = (ToggleButton) view.findViewById(R.id.toggleButton1);
    TextView textView = (TextView) view.findViewById(R.id.textView1);
    textView.setText("Toggle Button " + position);

    /*toggleButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            ToggleButton t = (ToggleButton) v.findViewById(R.id.toggleButton1);

            Toast.makeText(mContext, Boolean.toString(t.isChecked()), Toast.LENGTH_SHORT).show();
        }
    });*/
    return view;
}

}

取消注释切换侦听器以播放该部分,并告诉我如何获得该位置。有没有办法在构建网格视图时将一些数据注入切换按钮,我以后可以参考?我找不到阅读ToggleButton javadoc的方法。

非常感谢!

编辑:

糟糕!这是布局:

main.xml中

<GridView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
    >



</GridView>

items.xml:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:orientation="horizontal">
    <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:orientation="vertical">

        <TextView 
            android:text="Some label" 
            android:id="@+id/textView1" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_margin="10dp"/>

        <ToggleButton 
            android:id="@+id/toggleButton1" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_margin="10dp"/>
    </LinearLayout>
</LinearLayout>

2 个答案:

答案 0 :(得分:3)

在getView方法中,定义 final int pos = position;

并在onClickListener中,

toggleButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            ToggleButton t = (ToggleButton) v.findViewById(R.id.toggleButton1);
            Toast.makeText(mContext, Boolean.toString(t.isChecked()) + " : " + String.valueOf(pos), Toast.LENGTH_SHORT).show();
        }
    });

工作正常,我检查过:)

这很有效,因为我们在getView方法中创建的OnClickListener是一个 Local Class ,它保留了它访问外层类的变量的本地副本(这就是我们需要的原因)声明pos为final)。该类的一个实例附带了从该方法返回的Toggle Button,其生命周期附加到Toggle Button的生命周期。

有关本地课程的更多信息here

答案 1 :(得分:0)

你好杰森    你可以在你的点击方法中尝试这个:    AdapterView.ContextMenuInfo info =(AdapterView.ContextMenInfo)view.getMenuInfo();

int position = info.position;