2016-08-14 67 views
1

像标题中所述我尝试在片段中使用OnItemClickListener(它不扩展ListFragment,它只是一个片段),但听众似乎没有回应。OnItemClickListener在片段与CustomAdapter不起作用

它是如何工作: 我创建了包含自定义ListViewItem的一个ListView片段,内容是从一个MySQL表下载与连接到PHP文件一类叫做下载和一类称为解析器,把特定的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,下载或解析器类让我知道,我会在这里发布,但我不确定这些是否有必要解决问题。

感谢您的关注!

回答

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

这似乎没有工作 “需要android.support.v4.widget.SwipeRefreshLayout 找到android.view.View” – ProgFroz

+0

@JustinN让我更新我的代码。 – Amir

+0

您的代码确实无误地工作,但我无法再刷新,当我尝试让该圈子继续在那里而没有做任何事情时。此外,它不能解决我的问题与onItemClickListener:/ – ProgFroz