0

早上好,

我对从未触发的 Fragment 内部的setOnItemClickListener事件有疑问。ListView

这是项目的代码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 代码(还包括带有一些侦听器的编辑文本):

<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" />

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

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;

}

有人能帮助我吗??

提前致谢。

问候。

朱塞佩

4

5 回答 5

2

只需放入android:focusable="false" android:clickable="false"布局。对于所有文本视图、按钮等。工作正常。

于 2013-12-09T06:22:11.783 回答
2

要扩展@Ashwin 的答案,请注意 ListView 阻止单击包含至少一个可聚焦后代的项目,但它不会使内容焦点可访问调用 setItemsCanFocus(true)。一种解决方法是禁用后代的可聚焦性,使用

android:descendantFocusability="blocksDescendants"

在定义列表项的布局中,尽管您可以像他之前提到的那样禁用可以集中注意力的子项。(我自己首先从http://cyrilmottier.com/2011/11/23/listview-tips-tricks-4-add-several-clickable-areas/了解到这一点,但其他一些 SO 帖子也指出了这一点。)

于 2014-04-15T14:37:04.663 回答
1

onViewCreated在Like 中声明你的 listview 和 itemclick 监听器,

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    final ListView listViewProf=(ListView)getActivity().findViewById(R.id.cerca_il_prof_list_result);
    listViewProf.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        }
    });
}
于 2013-05-30T12:00:48.847 回答
0

你会试试这个它会帮助你 lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(android.widget.AdapterView<?> arg0,
                    View arg1, int arg2, long arg3) {
                // TODO Auto-generated method stub


            }
        });
于 2014-01-31T08:49:57.277 回答
0

我找到了解决方案。如果您将列表项的(您在列表视图适配器中设置的布局)布局子视图更改为

 android:clickable="false"

 android:focusable="false"

有用。

于 2019-07-30T05:42:03.267 回答