简体   繁体   中英

listview.setOnItemClickListener not working

I have a problem with my onItemClickListener. It's not working and it's not even detecting click on element in listview. I have almost same fragment related Here is the call in fragment:

   public void onViewCreated(View view, Bundle savedInstanceState) {
    list = (ListView) view.findViewById(R.id.listAwards);
    list.setItemsCanFocus(false);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            FragmentManager fm = getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            Fragment fragment = new AwardDetailFragment();
            ft.replace(R.id.fragmentlayout, fragment);
            ft.commit();
            Toast.makeText(getContext(),"Test",Toast.LENGTH_LONG).show();
        }
    });

this is the xml file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:background="@color/neonGreen"
    android:id="@+id/rewards_section_header"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="NAGRADE"
        android:id="@+id/textView11"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textColor="@color/lightgraylampica"
        android:textSize="20dp"
        android:layout_marginLeft="10dp"
        android:textStyle="bold" />
</RelativeLayout>
<com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/swipe_section_swipe"
    app:srl_direction="bottom"
    android:layout_below="@+id/rewards_section_header">
<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listAwards"
    android:layout_below="@+id/rewards_section_header"
    android:layout_alignParentLeft="true"
    android:focusable="false"
    android:clickable="true"
    android:focusableInTouchMode="false"
    android:layout_alignParentStart="true"
    android:contextClickable="true" />
</com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout>
</RelativeLayout>

Full activity code :

 public class AwardListFragment extends Fragment {
// The onCreateView method is called when Fragment should create its View object hierarchy,
// either dynamically or via XML layout inflation.
int offset = 0;
int size = 8;
private SwipyRefreshLayout swipyRefreshLayout;
private ProgressDialog progressBar;
private ArrayList<Awards> listAwards;
private  ListView list;


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

    View rootView = inflater.inflate(R.layout.rewards_section, parent, false);
    listAwards = new ArrayList<>();
    swipyRefreshLayout = (SwipyRefreshLayout) rootView.findViewById(R.id.swipe_section_swipe);
    swipyRefreshLayout.setOnRefreshListener(new SwipyRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh(SwipyRefreshLayoutDirection direction) {
            if (direction == SwipyRefreshLayoutDirection.BOTTOM) {
                offset++;
                swipyRefreshLayout.setRefreshing(false);

                showlist();

            }
        }

    });
    return rootView;
}

// This event is triggered soon after onCreateView().
// Any view setup should occur here.  E.g., view lookups and attaching view listeners.
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    list = (ListView) view.findViewById(R.id.listAwards);
    list.setItemsCanFocus(false);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            FragmentManager fm = getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            Fragment fragment = new AwardDetailFragment();
            ft.replace(R.id.fragmentlayout, fragment);
            ft.commit();
            Toast.makeText(getContext(),"Test",Toast.LENGTH_LONG).show();
        }
    });


    // Setup any handles to view objects here
    // EditText etFoo = (EditText) view.findViewById(R.id.etFoo);
    showlist();

}

public void showlist() {
    progressBar = new ProgressDialog(getActivity());
    progressBar.setMessage("Pričekajte molim...");
    progressBar.show();
    final int firstitemposition = 0;
    final int currentposition = list.getFirstVisiblePosition();
    NetworkSDK.getInstance().getAwards(size, offset, new Callback<List<Awards>>() {
        @Override
        public void onResponse(Call<List<Awards>> call, Response<List<Awards>> response) {
            if (response.isSuccess()) {
                if (response.body().size() == 0) {
                    Toast.makeText(getContext(), "Sve nagrade su učitane", Toast.LENGTH_SHORT).show();
                    progressBar.setCancelable(false);
                    progressBar.dismiss();

                } else
                    for (int i = 0; i < response.body().size(); i++)
                        listAwards.add(response.body().get(i));
                AwardsAdapter awardsAdapter = new AwardsAdapter(listAwards);
                list.setAdapter(awardsAdapter);
                awardsAdapter.notifyDataSetChanged();
                list.setSelectionFromTop(currentposition + 1, firstitemposition);

            }
            progressBar.setCancelable(false);
            progressBar.dismiss();
        }

        @Override
        public void onFailure(Call<List<Awards>> call, Throwable t) {

        }
    });

}
}

Why don't you add android:onClick="clickFunction" in your fragment ListView xml entry and try like this:

    <ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listAwards"
    android:layout_below="@+id/rewards_section_header"
    android:layout_alignParentLeft="true"
    android:focusable="false"
    android:clickable="true"
    android:focusableInTouchMode="false"
    android:layout_alignParentStart="true"
    android:onClick="clickFunction"
    android:contextClickable="true" />

and then in your fragment:

public void clickFunction(View v) {
    // Do something
}

Try pasting this code in your onCreateView rather than onViewCreated:

list = (ListView) rootView.findViewById(R.id.listAwards);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            FragmentManager fm = getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            Fragment fragment = new AwardDetailFragment();
            ft.replace(R.id.fragmentlayout, fragment);
            ft.commit();
            Toast.makeText(getContext(),"Test",Toast.LENGTH_LONG).show();
        }
    });

Ideally, you should reference all your view and widgets in your onCreateView for fragments & onCreate for activities. Try this code, i hope this solves your problem.

The onViewCreated is called immediately after onCreate so your showlist() method should be called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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