2014-01-22 114 views
4

我想在我的應用程序中實現ActionBar-PullToRefresh。該活動中有一個片段,片段中有一個列表視圖。 listview的實現是使用自定義適配器。ActionBar-PullToRefresh與ListView和片段

我試圖用github上的QuickStart-ABS指南來實現它,但拉不起作用。我有一種感覺,我沒有正確初始化PullToRefresh。請各位看看下面我的代碼...

fragment_news_list.xml

<?xml version="1.0" encoding="utf-8"?> 
<uk.co.senab.actionbarpulltorefresh.extras.actionbarsherlock.PullToRefreshLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/ptr_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ListView 
     android:id="@+id/listview_news_list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </ListView> 

</uk.co.senab.actionbarpulltorefresh.extras.actionbarsherlock.PullToRefreshLayout> 

NewsListFragment.java

import java.util.ArrayList; 
import java.util.List; 

import uk.co.senab.actionbarpulltorefresh.extras.actionbarsherlock.PullToRefreshLayout; 
import uk.co.senab.actionbarpulltorefresh.library.ActionBarPullToRefresh; 
import uk.co.senab.actionbarpulltorefresh.library.listeners.OnRefreshListener; 
import android.app.ProgressDialog; 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.v4.util.LruCache; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 

import com.actionbarsherlock.app.SherlockFragment; 
import com.android.volley.RequestQueue; 
import com.android.volley.toolbox.ImageLoader; 
import com.android.volley.toolbox.JsonArrayRequest; 
import com.android.volley.toolbox.NetworkImageView; 
import com.android.volley.toolbox.Volley; 

public class NewsListFragment extends SherlockFragment implements 
     OnRefreshListener { 

    ProgressDialog pd; 
    ImageLoader imageLoader; 
    JsonArrayRequest jsArrayRequest; 
    Database db; 
    ListView listview; 
    List<NewsItem> newsItems = new ArrayList<NewsItem>(); 
    NewsAdapter adapter; 

    NewsDbAdapter mNewsDbAdapter; 

    private PullToRefreshLayout mPullToRefreshLayout; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     mNewsDbAdapter = DatabaseHelper.get(
       getActivity().getApplicationContext()).getNewsDbAdapter(); 

     // unrelated code removed 

     View view = inflater.inflate(R.layout.fragment_news_list, container, 
       false); 

     pd = new ProgressDialog(getActivity()); 
     pd.setMessage("Loading..."); 
     pd.show(); 
     pd.setCancelable(true); 

     newsItems = mNewsDbAdapter.getNewsTitles(); 
     adapter = new NewsAdapter(getActivity(), newsItems); 
     listview = (ListView) view.findViewById(R.id.listview_news_list); 
     listview.setAdapter(adapter); 

     pd.dismiss(); 

     return view; 
    } 

    public class NewsAdapter extends ArrayAdapter<NewsItem> { 
     // Adapter code goes in here... removed as not necessary 
    } 



    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 
     ViewGroup viewGroup = (ViewGroup) view; 

     // As we're using a ListFragment we create a PullToRefreshLayout manually 
     mPullToRefreshLayout = new PullToRefreshLayout(viewGroup.getContext()); 

     // We can now setup the PullToRefreshLayout 
     ActionBarPullToRefresh.from(getActivity()) 
       // We need to insert the PullToRefreshLayout into the Fragment's ViewGroup 
       .insertLayoutInto(viewGroup) 
       // Here we mark just the ListView and it's Empty View as pullable 
       .theseChildrenArePullable(android.R.id.list, android.R.id.empty) 
       .listener(this) 
       .setup(mPullToRefreshLayout); 

    } 



    @Override 
    public void onRefreshStarted(View view) { 
     // Hide the list 
     // setListShown(false); 
     listview.setVisibility(View.INVISIBLE); 

     /** 
     * Simulate Refresh with 4 seconds sleep 
     */ 
     new AsyncTask<Void, Void, Void>() { 

      @Override 
      protected Void doInBackground(Void... params) { 
       try { 
        Thread.sleep(5000); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
       return null; 
      } 

      @Override 
      protected void onPostExecute(Void result) { 
       super.onPostExecute(result); 

       // Notify PullToRefreshLayout that the refresh has finished 
       mPullToRefreshLayout.setRefreshComplete(); 

       if (getView() != null) { 
        // Show the list again 
        //setListShown(true); 
        listview.setVisibility(View.VISIBLE); 
       } 
      } 
     }.execute(); 
    } 

} 

我要去哪裏錯了?如果有人用帶有listview的片段的例子,請在這裏分享鏈接..

謝謝!

[編輯]

改變了這種

theseChildrenArePullable(android.R.id.list, android.R.id.empty) 

theseChildrenArePullable(R.id.listview_news_list, android.R.id.empty) 

和它的工作...

回答

5

我認爲,問題是,你不使用你的片段佈局的ListView,但默認一個從框架,試試這個:

// setup the PullToRefreshLayout 
    ActionBarPullToRefresh.from(getActivity()) 
      // We need to insert the PullToRefreshLayout into the Fragment's ViewGroup 
      .insertLayoutInto(viewGroup) 
      // Here we mark just the ListView and it's Empty View as pullable 
      .theseChildrenArePullable(R.id. listview_news_list, android.R.id.empty) 
      .listener(this) 
      .setup(mPullToRefreshLayout); 

如果它不工作,你也可以通過做嘗試在onCreateView方法如下:

mPullToRefreshLayout = ((PullToRefreshLayout) view.findViewById(R.id.ptr_layout)); 

,然後設置它像這樣在onActivityCreated或onViewCreated:

ActionBarPullToRefresh 
    .from(getActivity()) 
    .allChildrenArePullable() 
    .listener(this) 
    .setup(this.mPullToRefreshLayout); 

爲了記錄,我在我的應用程序中使用了片段內的PullToRefreshLayout,它工作得很好。讓我知道它是否有效。

+0

我是怎麼想的!感謝您的幫助..問題是我使用的是默認框架的listview,而不是我的listview。將android.R.id.list更改爲R.id.listview_news_list,讓它工作。再次感謝... –

+0

很高興我能幫忙;)快樂的編碼! – 2Dee

+0

就我而言,我是在沒有聽者的情況下進行測試。你必須有聽衆附加,否則它將無法正常工作。 – pimguilherme

-3

您必須初始化ActionBarPullToRefresh在onCreate方法你的活動,n你的片段的onViewCreated。初始化Activity中的庫,並將其傳遞給Fragment。