listview.setOnItemClickListener不工作

时间:2016-05-16 11:10:46

标签: java android-layout listview android-fragments android-studio

我的onItemClickListener有问题。它不起作用,甚至没有检测listview中的click元素。我有几乎相同的片段相关这里是片段中的调用:

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

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

        }
    });

}
}

2 个答案:

答案 0 :(得分:0)

为什么不在片段android:onClick="clickFunction" xml条目中添加ListView并尝试这样:

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

然后在你的片段中:

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

答案 1 :(得分:0)

尝试在onCreateView而不是onViewCreated中粘贴此代码:

class TestModel(models.Model):
    cover = models.ImageField(upload_to='covers')

理想情况下,您应该在onCreateView中引用所有视图和小部件以获取片段和片段。 onCreate活动。试试这个代码,我希望这能解决你的问题。

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