2016-08-14 103 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