事件setOnItemClickListener在我的列表中不起作用

时间:2015-08-31 21:38:18

标签: android

我在列表OnItemClickListener中调用了事件lista,但它不起作用,Toast未显示。

低于我的代码fragment1.java

public class Fragment1 extends Fragment {
    Switch mySwitch;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){      
        //nome dos reles
        String[] atividades = new String[]{"lampada","tomada quarto","tomada cozinha","geladeira"};

        //interface
        View view=inflater.inflate(R.layout.layout_acionamento, container,false);       

        //lista
        ListView lista = (ListView)view.findViewById(R.id.mylistView);          

        //botão acionar
        Button button = (Button) view.findViewById(R.id.buttonAcionar);

        //adaptador de string para reles
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.modelo_lista_reles,atividades);
        lista.setAdapter(adapter);

        //evento do botão acionar
        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(getActivity(), "CUBANAJARRA", getId()).show();
            }
        });

        lista.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // TODO Auto-generated method stub
                Toast.makeText(getActivity(), "CUBANAJARRA_lista", getId()).show();
            }
        });

        return(view);
    }
}

我的modelo_lista_rele.xml代码(我使用它来创建带开关的列表):

<?xml version="1.0" encoding="utf-8"?>
<Switch xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"    
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:focusable="false"
    android:focusableInTouchMode="false"/>

我的layout_acionamento.xml代码(用于我在片段中返回的视图):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/mylistView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/buttonAcionar"
        android:layout_alignParentTop="true"
        android:clickable="true" >
    </ListView>

    <Button
        android:id="@+id/buttonAcionar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Acionar" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:2)

Toast.makeText(getActivity(), "CUBANAJARRA", getId()).show();

第三个参数是length而不是id。 尝试使用Toast.LENGTH_LONGToast.LENGTH_SHORT代替:

Toast.makeText(getActivity(), "CUBANAJARRA", Toast.LENGTH_LONG).show();

答案 1 :(得分:0)

Switch包裹你LinearLayout,并确保阻止descendantFocusability或将Switch的xml params focusable和focusableInTouchMode设置为false。这应该让事情有效。发生的事情是Switch正在使用触摸事件,因此ListView永远无法触发OnItemClickListener。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:descendantFocusability="blocksDescendants"
              android:layout_width="match_parent"
              android:layout_height="50dp">

    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

修改:根据评论:看一下这个YouTube video by Romain Guy on ListViews。它应该让你很好地了解下面发生了什么。

创建一个ViewHolder课程,以跟踪项目布局中的各个小部件:

public static class ViewHolder{
  public Switch mSwitch;
}

然后,在适配器中,覆盖getView。下面的代码一开始可能看起来很奇怪,但它是获得回收视图的好处的正确方法,因此您的列表可以快速滚动。

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if(convertView == null){
                convertView = mInflater.inflate(R.layout.modelo_lista_rele.xml, parent, false);
                holder = new ViewHolder(convertView);
                holder.switch = convertView.findViewById(R.id.my_switch);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder)convertView.getTag();
            }

            String item = getItem(position);

            //Do something with item here
            //holder.mSwitch.setText(item);
            //holder.mSwitch.setOnCheckedChangeListener(...)

            return convertView;
        }