就像标题中描述的那样,我尝试在 fragment 中使用 OnItemClickListener (它不扩展 ListFragment,它只是一个 fragment ),但监听器似乎没有响应。
它是如何工作的: 我创建了一个 Fragment,其中包含一个带有自定义 ListViewItem 的 ListView,内容是从 mysql 表下载的,其中有一个名为 Downloader 的类连接到 PHP 文件,还有一个名为 Parser 的类,它将给定的 JSON 结果转换为字符串。有一个 CustomAdapter,它获取结果并使用解析器的结果更改自定义 ListViewItem 的 TextView。
ListView 创建没有问题,内容得到显示。 fragment 代码:
public class BiologyFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
String url = "URL FOR PHP - just cut that out :D";
private SwipeRefreshLayout ll;
private ListView lv;
private boolean isRefreshing;
private Handler refreshhandler = new Handler();
public BiologyFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
ll = (SwipeRefreshLayout)inflater.inflate(R.layout.fragment_biology, container, false);
//set up listener for pull to refresh
ll.setOnRefreshListener(this);
//Listview for the to be populated
lv = (ListView) ll.findViewById(R.id.lv);
//download on opening
new Downloader(getContext(), url, lv).execute();
//PROBLEM HERE !
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getContext(), "CLICKED " + position, Toast.LENGTH_SHORT).show();
}
});
lv.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
boolean enable = false;
if (lv != null && lv.getChildCount() > 0) {
// check if the first item of the list is visible
boolean firstItemVisible = lv.getFirstVisiblePosition() == 0;
// check if the top of the first item is visible
boolean topOfFirstItemVisible = lv.getChildAt(0).getTop() == 0;
// enabling or disabling the refresh layout
enable = firstItemVisible && topOfFirstItemVisible;
}
ll.setEnabled(enable);
}
});
return ll;
}
@Override
public void onRefresh() {
new Downloader(getContext(), url, lv).execute();
refreshhandler.post(refreshing);
}
Runnable refreshing = new Runnable() {
@Override
public void run() {
if(isRefreshing){
// re run the verification after 1 second
}else{
// stop the animation after the data is fully loaded
ll.setRefreshing(false);
}
}
};
}
fragment 的 XML:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/lv"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="false"
/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_menu_slideshow" />
</FrameLayout>
如果您需要 ListViewItem XML 或 CustomAdapter、下载器或解析器类,请告诉我,我会将其发布在这里,我只是不确定这些是否是解决问题所必需的。
感谢您的关注!
最佳答案
您不应该将根目录转换为SwipeRefreshLayout
,因此不要使用:
ll = (SwipeRefreshLayout)inflater.inflate(R.layout.fragment_biology, container, false);
用途:
View rootView = inflater.inflate(R.layout.fragment_biology, container, false);
lv = (ListView) rootView.findViewById(R.id.lv);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getContext(), "CLICKED " + position, Toast.LENGTH_SHORT).show();
}
});
此外,您没有正确使用 SweapRefreshLayout
,建议您这样做:
<!-- rest of layout -->
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/listView">
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
<!-- rest of layout-->
关于java - 具有 CustomAdapter 的 Fragment 中的 OnItemClickListener 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38939395/