2014-02-25 64 views
1

目前,我正在使用Styles.xml與Java代碼結合來定義PullToRefresh自定義。我只想通過Java代碼來完成所有的定製,而不是用XML定義它們。我可以通過編程實現所有的自定義嗎?ActionBar - PullToRefresh以編程方式實現而不使用樣式

這裏是我的XML定製:

<!-- PullToRefresh Customizations --> 
<style name="Theme.Holo.CustomPtrHeader" parent="Theme.Sherlock"> 
    <item name="ptrHeaderStyle">@style/Widget.Custom.PtrHeader</item> 
</style> 

<style name="Widget.Custom.PtrHeader" parent="android:Widget"> 

    <!-- The background of the header view --> 
    <item name="ptrHeaderBackground">#FF69B4</item> 

    <!-- Color to tint the progress bar --> 
    <item name="ptrProgressBarColor">#FF69B4</item> 

    <!-- The strings to be displayed at the various states --> 
    <item name="ptrPullText">Pull To Refresh</item> 
    <item name="ptrRefreshingText">Refreshing News</item> 
    <item name="ptrReleaseText">Loading...</item> 
</style> 

Java代碼:

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

    mPullToRefreshLayout = new PullToRefreshLayout(activity); 
    ActionBarPullToRefresh 
      .from(activity) 
      .insertLayoutInto(viewGroup) 
      .theseChildrenArePullable(R.id.gridview_news_grid, 
        android.R.id.empty).listener(this) 
      .setup(mPullToRefreshLayout); 
} 


@Override 
public void onRefreshStarted(View view) { 

    if (Const.DEBUGGING) 
     Log.d(Const.DEBUG, "PullToRefresh Started"); 

    gridview.setVisibility(View.INVISIBLE); 

    new AsyncTask<Void, Void, Void>() { 

     @Override 
     protected Void doInBackground(Void... params) { 
      getDataFromServer(); 
      return null; 
     } 

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

      mPullToRefreshLayout.setRefreshComplete(); 
      if (getView() != null) { 
       gridview.setVisibility(View.VISIBLE); 
      } 
     } 
    }.execute(); 
} 

我想我可以使用選項()這樣做的,但不知道如何正確地做到這一點。有人可以幫我解決這個問題。

回答

9

OK ..所以我確實仔細研究過,這裏是我解決問題的方法。我們可以使用DefaultHeaderTransformer類完全控制「PullToRefresh」。我已經從Styles.xml中刪除了「PullToRefresh」定製,並使用下面的代碼來更改進度條的顏色和整體視圖背景。你可以根據你的需要進行試驗。如果您有任何問題,我可以回答他們。

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

    mPullToRefreshLayout = new PullToRefreshLayout(activity); 
    ActionBarPullToRefresh 
      .from(activity) 
      .options(Options.create().scrollDistance(.75f).build()) 
      .insertLayoutInto(viewGroup) 
      .theseChildrenArePullable(R.id.gridview_features_grid, 
        android.R.id.empty).listener(this) 
      .setup(mPullToRefreshLayout);  

    DefaultHeaderTransformer transformer = (DefaultHeaderTransformer) mPullToRefreshLayout 
      .getHeaderTransformer(); 
    transformer.getHeaderView().findViewById(R.id.ptr_text) 
      .setBackgroundColor(getResources().getColor(R.color.blue)); 
    transformer.setProgressBarColor(getResources().getColor(R.color.blue)); 
} 

截圖:

enter image description here

enter image description here

相關問題