OnClickListener在Fragment中不起作用

OnClickListener在Fragment中不起作用

我正在尝试将侦听器放置到片段视图中的列表视图中。我尝试了几种方法来执行此操作,但仍无法正常工作。我无法创建另一个视图focusable="false",因为列表视图上方有一个EditText。每次单击该项目时,它都会在我创建的JSONAdapter中调用getView。这是我的XML:

<?xml version="1.0" encoding="utf-8"?>

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

            <ImageView
                android:focusable="false"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher"/>

            <EditText
                android:id="@+id/searchquery"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:hint="Search"
                />
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@android:id/list">

    </ListView>

</LinearLayout>


这是我的片段:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

    mJSONAdapter = new JSONAdapter(getActivity(), inflater);

    View v = inflater.inflate(R.layout.header, container, false);
    return v;
}
@Override
public void onViewCreated(View view,Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    ListView lv = (ListView) getView().findViewById(android.R.id.list);
    // Set the ListView to use the ArrayAdapter
    lv.setAdapter(mJSONAdapter);//isi datany pk json adapter
    lv.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getActivity(),Integer.toString(position),Toast.LENGTH_SHORT).show();
            JSONObject jsonObject = (JSONObject) mJSONAdapter.getItem(position);//ambil item yg diclick
            String raceid = jsonObject.optString("id","");//ambil cover id yg diklik, argumen kedua itu klo null defaultny apa

            // create an Intent to take you over to a new DetailActivity
            Intent detailIntent = new Intent(getActivity(), DetailActivity.class);

            // pack away the data about the cover
            // into your Intent before you head out
            detailIntent.putExtra("raceid", raceid);
            // start the next Activity using your prepared Intent
            startActivity(detailIntent);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    new RetrieveFeedTask().execute();

}

最佳答案

您可以尝试此代码。

lv.setOnItemClickListener(new OnItemClickListener()
   {
      @Override
      public void onItemClick(AdapterView<?> adapter, View v, int position,
            long arg3)
      {

            // do your work
      }
   });

关于android - ListView OnClickListener在Fragment中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25607184/

10-09 03:50