就像在标题中描述的那样,我尝试在片段中使用OnItemClickListener(它不会扩展ListFragment,它只是一个片段)但是监听器似乎没有响应。
它是如何工作的: 我创建了一个Fragment,其中包含一个带有自定义ListViewItem的ListView,内容从mysql表中下载,其中一个名为Downloader的类连接到PHP文件,一个名为Parser的类将给定的JSON结果转换为字符串。有一个CustomAdapter,它接收结果并使用Parser的结果更改自定义ListViewItem的TextView。
创建ListView没有问题,显示内容。 片段代码:
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);
}
}
};
}
片段的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,Downloader或Parser Class只是让我知道,我会在这里发布,我只是不确定是否有必要解决这个问题。
感谢您的关注!
答案 0 :(得分:0)
你不应将你的根转换为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-->