gpt4 book ai didi

java - listview.setOnItemClickListener 不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 12:36:45 25 4
gpt4 key购买 nike

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

   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>

完整 Activity 代码:

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

}
});

}
}

最佳答案

为什么不在片段 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" />

然后在你的片段中:

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

关于java - listview.setOnItemClickListener 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37252519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com