【问题标题】:Android - setOnItemClickListener event of ListView not working in FragmentAndroid - ListView 的 setOnItemClickListener 事件在 Fragment 中不起作用
【发布时间】:2019-07-30 13:42:03
【问题描述】:

早上好,

我在一个从未触发的片段中遇到ListViewsetOnItemClickListener 事件的问题。

这是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;

}

有人可以帮帮我吗?

提前致谢。

问候。

朱塞佩

【问题讨论】:

  • 如果您的片段从未被触发,您怎么会遇到问题?

标签: android listview


【解决方案1】:

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

android:descendantFocusability="blocksDescendants"

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      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) {
      
              }
          });
      }
      

      【讨论】:

      • 我已经理解了这个问题:它在项目详细信息的 xml 文件中。
      • 适用于子视图点击,但不适用于 onItemClick
      【解决方案4】:

      你会试试这个它会帮助你 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
      
      
                  }
              });
      

      【讨论】:

        【解决方案5】:

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

         android:clickable="false"
        

         android:focusable="false"
        

        它有效。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-10-12
          • 2017-07-07
          • 1970-01-01
          相关资源
          最近更新 更多