简体   繁体   English

仅在单击ListView中的列表项后,切换按钮才会进入焦点

[英]Toggle Button Comes in to Focus Only after Clicking the List Item in the ListView

I have a Listview With two Textviews and toggle Button and i have ontoggleChangeListener for the Toggle Button. 我有一个带有两个Textviews和切换按钮的Listview,并且我有ToggleChangeListener作为切换按钮。 but if i click on the toggle button without clicking the list item it is not coming in to focus (that is listener is not Called ) but comes in to focus after clicking a listitem in the Listview 但是如果我在不单击列表项的情况下单击切换按钮,它将不会进入焦点(即未调用listener),但是在Listview中单击一个列表项后就会进入焦点

MY CODE SNIPPET : 我的代码片段:

alarmList.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View v, final int pos,
            long arg3) {

        System.out.println("Item On click is Called");

        toggle = (ToggleButton) v.findViewById(R.id.tg);
        toggle.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                System.out
                        .println("On click of the Toggle Button is called !!");
                if (toggle.isChecked()) {
                    System.out.println("Checked");
                } else {
                    System.out.println("Not Checked ");
                }
            }
        });
    }
});

MY LISVIEW XML CODE : 我的LISVIEW XML代码:

    android:id="@+id/alarmlist"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="#fff"
    android:dividerHeight="2dp" >

MY TOGGLE BUTTON XML CODE : 我的切换按钮XML代码:

    android:id="@+id/tg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:text="ToggleButton" 

要调用切换按钮监听器,您需要从onItemClick监听器中删除切换按钮代码,并将其放在监听器之外。

but if i click on the toggle button without clicking the list item it is not coming in to focus (that is listener is not Called ) but comes in to focus after clicking a listitem in the Listview 但是如果我在不单击列表项的情况下单击切换按钮,它将不会进入焦点(即未调用listener),但是在Listview中单击一个列表项后就会进入焦点

This is because the code inside your onItemClick() method is not run until it is clicked at least once. 这是因为onItemClick()方法中的代码直到至少被单击一次才运行。

You need to create a custom adapter and define the ToggleButton's behavior in the getView() method to have your button function in the manner you expect. 您需要创建一个自定义适配器,并在getView()方法中定义ToggleButton的行为,以使按钮功能达到您期望的方式。


Something like this: 像这样:

public class MyAdapter extends ArrayAdapter<String> {
    public MyAdapter(Context context, int textViewResourceId, List<String> objects) {
        super(context, textViewResourceId, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

        ToggleButton toggle = (ToggleButton) view.findViewById(R.id.tg);
        toggle.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                System.out
                        .println("On click of the Toggle Button is called !!");
                if (toggle.isChecked()) {
                    System.out.println("Checked");
                } else {
                    System.out.println("Not Checked ");
                }
            }
        });

        return view;
    }
}

(...But this particular example is not the most efficient method, please watch Android's Romain Guy discuss creating fast adapters for a wealth of information I cannot cram into the answer here.) (...但是这个特定示例并不是最有效的方法,请观看Android的Romain Guy 讨论如何为大量信息创建快速适配器 ,而我无法在此处进行解答。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM