Android的 - 的ListView setOnItemClickListener事件片段不工作 [英] Android - setOnItemClickListener event of ListView not working in Fragment

查看:125
本文介绍了Android的 - 的ListView setOnItemClickListener事件片段不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好

我有一个的ListView 的是从来没有发射一个片段中的 setOnItemClickListener 事件的一个问题。

I have a problem with the setOnItemClickListener event of a ListView inside a Fragment which is never fired.

下面是的ListView 项目的code:

Here is the code of the item in listView:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="80dip" >

<ImageView
  android:id="@+id/url_foto"
  android:layout_width="100dip"
  android:layout_height="100dip" 
  android:src="@drawable/stub" 
  android:scaleType="centerCrop"/>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_toRightOf="@id/url_foto"
    android:orientation="vertical"
    android:paddingLeft="10sp" >

    <TextView
        android:id="@+id/nome"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textIsSelectable="true" />

    <TextView
        android:id="@+id/cognome"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textIsSelectable="true" />

下面是列表的XML code(也包括一些听众的EditText):

Here is the xml code of the list (that also includes a edittext with some listeners):

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

<EditText
    android:id="@+id/cerca_il_prof_edit_search"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="search" />

<ListView
    android:id="@+id/cerca_il_prof_list_result"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:cacheColorHint="#00000000"
    android:drawSelectorOnTop="true"
    android:focusable="true" />

和最后片段包含未触发事件:

and finally the fragment that contains the event which is not fired:

public class CercaIlProfFragment extends Fragment {
private DbAdapter dbHelper;
private Cursor cursor;
private Context ctx;
List<Professore> listProf;

private static final String TAG = "CercaIlProf - ";

private EditText string_search;
private ListView listViewProf;

int textlength = 0;

/**
 * The fragment argument representing the section number for this fragment.
 */
public static final String ARG_SECTION_NUMBER = "section_number";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.cerca_il_prof, container, false);

    ctx = getActivity();
    listProf = new ArrayList<Professore>();

    string_search = (EditText) rootView.findViewById(R.id.cerca_il_prof_edit_search);

    Date currDate = new Date();
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(currDate);
    calendar.add(Calendar.MONTH, 1);

    // get all prof
    getAllProf();

    listViewProf = (ListView) rootView.findViewById(R.id.cerca_il_prof_list_result);
    ProfessoreListAdapterWithCache professoreListAdapterWithCache = new ProfessoreListAdapterWithCache(ctx,
            R.layout.cerca_il_prof_list_result_item, listProf, this.getActivity());
    listViewProf.setAdapter(professoreListAdapterWithCache);

    listViewProf.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getActivity(), "Click ListItem Number " + position, Toast.LENGTH_LONG).show();
        }
    });

    listViewProf.setEnabled(true);

    string_search.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
            Toast.makeText(getActivity(), "Click ListItem Number ", Toast.LENGTH_LONG).show();
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            //do stuff
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //do stuff
    });

    return rootView;

}

有人可以帮我请??

Can someone help me please??

先谢谢了。

问候。

朱塞佩

推荐答案

要在@扩大阿什温的回答,请注意至少包含一个可聚焦项目的ListView的块点击
后代,但它不会使内容调焦到达呼叫
setItemsCanFocus(真)。一个解决办法是禁用的后代可聚焦性,采用

To expand upon @Ashwin's answer, note that the ListView blocks clicks of an item that contains at least one focusable descendant but it doesn’t make the content focus-reachable calling setItemsCanFocus(true). One workaround is by disabling focusability of descendants, using

android:descendantFocusability="blocksDescendants"

在布局定义你的清单项目,虽然你可以只是禁用,因为他前面所提到的,可以采取聚焦的孩子。 (第一次知道这个从我自己的http://cyrilmottier.com/2011/11/23/listview-tips-tricks-4-add-several-clickable-areas/,但是还有一些其他SO帖还指出这一点。)

in the layout defining your list item, although you can just disable children that can take focus as he mentioned earlier. (First learned of this myself from http://cyrilmottier.com/2011/11/23/listview-tips-tricks-4-add-several-clickable-areas/, but a few other SO posts also point this out.)

这篇关于Android的 - 的ListView setOnItemClickListener事件片段不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆