简体   繁体   English

Click不能在Listitem Listview android上运行

[英]Click is not working on the Listitem Listview android

I implemented the android listview with the ListActivity . 我用ListActivity实现了android listview Here I have the problem that when i click on the list item no action is performed when the flash color is also not coming that is the orange color. 在这里我遇到的问题是,当我点击列表项时,当闪光颜色也没有出现时,没有执行任何动作,即橙色。 So do you have any idea about this kindly answer to my question. 所以你对我的问题的这个答案有任何想法。

@Override
protected void onListItemClick(ListView l, View v, int position, long id) 
{
    super.onListItemClick(l, v, position, id);
    Toast.makeText(getApplicationContext(), "msg msg", Toast.LENGTH_SHORT)
            .show();

}

I put this code also into the Main ListActivity. 我把这段代码也放到了Main ListActivity中。

The first thing what you have to note here is, whenever there are Clickable elements like Buttons or ImageButtons present in your ListView element, they take the control of click events. 首先要注意的是,只要ListView元素中存在Buttons或ImageButtons等可点击元素,它们就会控制点击事件。 And so your ListView won't get the chance to accept the click event. 因此,您的ListView将无法接受click事件。

What you simply have to do is, set the focusable attribute to false for the Button or ImageButton you have in your ListView. 您只需要为ListView中的ButtonImageButton设置focusable属性为false。 But still they will work without any problem and also your ListView's onListItemClick will also work. 但是它们仍然可以正常工作,而且ListView的onListItemClick也可以工作。

Try this, 试试这个,

        <Button  android:id="@+id/textsize_increaser"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/back_button"
        android:focusable="false"
        android:text=" A + "/>

Here I have added this android:focusable="false" and it works fine. 在这里我添加了这个android:focusable="false" ,它工作正常。 try it. 试试吧。

Have you set the choice mode of ListView to SINGLE : 您是否将ListView的选择模式设置为SINGLE:

     listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

And if you have any clickable imageview or textview or button in the list item, then make them not focusable (in your Adapter class): 如果列表项中有任何可点击的imageview或textview或按钮,则使它们不可聚焦(在Adapter类中):

        yourButton.setFocusable(false);
        yourButton.setFocusableInTouchMode(false);

Are you using custom Adapter? 你在使用自定义适配器吗? and inflating layout with button or any view that eats away the list list view focus as child, then it won't work obviously. 并使用按钮或任何视图将列表列表视图焦点作为子项进行膨胀布局,然后它将无法正常工作。 make sure to set 一定要设置

    android:focusable="false"

to such view in xml file. 到xml文件中的这种视图。 hope this works for you. 希望这对你有用。

在listactivity java文件中设置它

listview1.setFocusable(false);

Eclipse suggested me to add textIsSelectable="true" to my TextViews in the layout xml which was used for list view. Eclipse建议我在用于列表视图的布局xml中将textIsSelectable="true"添加到我的TextViews。

Well, if you want to click the items in the list then you should not add those tags. 好吧,如果要单击列表中的项目,则不应添加这些标记。

make sure that you are 确保你是

  1. Not using Scroll View with List View 不使用列表视图的滚动视图
  2. Not using Scroll View in your row item layout for List View 不在列表视图的行项目布局中使用滚动视图

If Scroll View is present at any of above place remove it 如果滚动视图出现在上述任何位置,请将其删除

refer to this post for a solution: 参考这篇文章寻求解决方案:

Click is not working on the Listitem Listview android Click不能在Listitem Listview android上运行

View v = parent.getChildAt(position);
parent.requestChildFocus(v,view);
v.setBackground(res.getDrawable(R.drawable."Some drawable for clicked row"));

int count = parent.getChildCount();
for(int i=0; i<count; i++)
{
   if(i!=position)
   {
     v = parent.getChildAt(i);
     v.setBackground(res.getDrawable(R.drawable."some drawable for not clicked row));
   }
}
listview.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int pos,
                    long id) {
                Toast.makeText(v.getContext(), exm.get(pos).getDefinition(),
                        Toast.LENGTH_SHORT).show();


            }
        });

Actually there is a parameter meant for that to prevent children views from getting focus, just add the following in the parent layout: 实际上有一个参数用于防止子视图获得焦点,只需在父布局中添加以下内容:

android:descendantFocusability="blocksDescendants"

As the documentation explains: 正如文档所述:

The ViewGroup will block its descendants from receiving focus. ViewGroup将阻止其后代获得焦点。

listItemButton.setFocusable(false);
listItemButton.setFocusableInTouchMode(false);

Set the above in your adapter . 适配器中设置以上内容。 It's not working in XML 它不适用于XML

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

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