Java listview.setOnItemClickListener不工作

Java listview.setOnItemClickListener不工作,java,android-layout,listview,android-fragments,android-studio,Java,Android Layout,Listview,Android Fragments,Android Studio,我的听众有问题。它不工作,甚至在listview中检测不到click-on元素。我有几乎相同的片段,这里是调用片段: public void onViewCreated(View view, Bundle savedInstanceState) { list = (ListView) view.findViewById(R.id.listAwards); list.setItemsCanFocus(false); list.setOnItemClickListene

我的听众有问题。它不工作,甚至在listview中检测不到click-on元素。我有几乎相同的片段,这里是调用片段:

   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();
        }
    });
public void clickFunction(View v) {
    // Do something
}
public void onViewCreated(查看视图,Bundle savedInstanceState){
list=(ListView)view.findViewById(R.id.listAwards);
list.setItemsCanFocus(false);
list.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
FragmentManager fm=getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
Fragment Fragment=新的AwardDetailFragment();
ft.replace(R.id.碎片布局,碎片);
ft.commit();
Toast.makeText(getContext(),“Test”,Toast.LENGTH_LONG.show();
}
});
这是xml文件:

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

完整活动代码:

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

        }
    });

}
}
公共类AwardListFragment扩展了片段{
//当片段应创建其视图对象层次结构时,调用onCreateView方法,
//动态地或通过XML布局扩展。
整数偏移=0;
int size=8;
私人SwipRefreshLayout SwipRefreshLayout;
私人ProgressDialog progressBar;
私人ArrayList listAwards;
私有列表视图列表;
@凌驾
创建视图上的公共视图(LayoutFlater充气器、视图组父级、捆绑保存状态){
视图根视图=充气机。充气(R.layout.u部分,父项,false);
listAwards=新的ArrayList();
swipyRefreshLayout=(swipyRefreshLayout)rootView.findViewById(R.id.swipe\u section\u swipe);
SwipRefreshLayout.setOnRefreshListener(新的SwipRefreshLayout.OnRefreshListener(){
@凌驾
公共void onRefresh(SwipRefreshLayoutDirection){
if(direction==swiperFreshLayoutDirection.BOTTOM){
offset++;
SwipRefreshLayout.setRefreshing(false);
showlist();
}
}
});
返回rootView;
}
//此事件在onCreateView()之后立即触发。
//任何视图设置都应在此处进行。例如,视图查找和附加视图侦听器。
@凌驾
已创建视图上的公共void(视图,捆绑保存状态){
list=(ListView)view.findViewById(R.id.listAwards);
list.setItemsCanFocus(false);
list.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
FragmentManager fm=getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
Fragment Fragment=新的AwardDetailFragment();
ft.replace(R.id.碎片布局,碎片);
ft.commit();
Toast.makeText(getContext(),“Test”,Toast.LENGTH_LONG.show();
}
});
//在此处设置任何句柄以查看对象
//EditText etFoo=(EditText)view.findViewById(R.id.etFoo);
showlist();
}
公开作废展示清单(){
progressBar=新建ProgressDialog(getActivity());
progressBar.setMessage(“Pričekajte molim…”);
progressBar.show();
最终int firstitemposition=0;
final int currentposition=list.getFirstVisiblePosition();
NetworkSDK.getInstance().getAwards(大小、偏移量、新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.issucess()){
if(response.body().size()==0){
Toast.makeText(getContext(),“Sve nagrade sučitane”,Toast.LENGTH_SHORT.show();
progressBar.setCancelable(假);
progressBar.disclose();
}否则
对于(int i=0;i
为什么不在片段
ListView
xml条目中添加
android:onClick=“clickFunction”
,然后像这样尝试:

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

为什么不在片段
ListView
xml条目中添加
android:onClick=“clickFunction”
,然后像这样尝试:

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

尝试在onCreateView而不是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();
        }
    });
list=(ListView)rootView.findviewbyd(R.id.listAwards);
list.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
FragmentManager fm=getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
Fragment Fragment=新的AwardDetailFragment();
ft.replace(R.id.碎片布局,碎片);
ft.commit();
Toast.makeText(getContext(),“Test”,Toast.LENGTH_LONG.show();
}
});
理想情况下,您应该在onCreateView中引用所有视图和小部件,以获取片段和活动的onCreate


onViewCreated在onCreate之后立即调用,因此应调用showlist()方法。

尝试将此代码粘贴到onCreateView中,而不是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();
        }
    });
list=(ListView)rootView.findviewbyd(R.id.listAwards);
list.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
FragmentManager fm=getFragmentManager();