如何获取在网格视图中单击的切换按钮的位置?

发布于 2024-10-17 17:32:57 字数 3616 浏览 1 评论 0原文

如何获取在 gridview 中单击的 android 切换按钮的位置?

如果我使用以下示例代码,我不会收到该位置的 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>

How do I get the position of the android togglebutton that was clicked in a gridview?

If I use the following example code I dont get a Toast message of the position.

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();
        }

    });
}

}

And the toggle adapter:

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;
}

}

Uncomment the toggle listener to play with that part and show me how to get the position. Is there a way to inject some data into the toggle button as the grid view is being constructed that I could later reference? I couldn't find a way reading the ToggleButton javadoc.

Thanks much!

EDIT:

Oops! here are the layouts:

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

桜花祭 2024-10-24 17:32:57

在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 的原因)。此类的实例附加到从该方法返回的切换按钮,并且其生命周期附加到切换按钮的生命周期。

有关本地类的更多信息此处

In the getView method, define
final int pos = position;

and in the 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();
        }
    });

Works fine, I checked :)

This works because, the OnClickListener we create in the getView method is a Local Class, which retains a local copy of the variables that it accesses of the outer class (this is the reason we need to declare pos as final). An instance of this class is attached with the Toggle Button that is returned from the method, and its lifetime is attached to the lifetime of the Toggle Button.

More about local classes here

短暂陪伴 2024-10-24 17:32:57

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

int 位置 = info.position;

Hello Jayson
you could try this in your click method:
AdapterView.ContextMenuInfo info = (AdapterView.ContextMenInfo)view.getMenuInfo();

int position = info.position;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文