2016-05-12 97 views
1

我在我的活动(我它们之间进行浏览使用片段交易)两个片段:Android的碎片两次

  1. 科目
  2. 主题详细名单 - 名单等级

主题片段布局:

<android.support.v4.widget.SwipeRefreshLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/refresher" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

</android.support.v4.widget.SwipeRefreshLayout> 

swipeRefreshLayout的侦听器是活动。它触发一个AsyncTask,它下载新数据,通知数据已更改的片段并隐藏swipeRefreshLayout。

的onPostExecute方法的问题的一部分:

... 

try { 
    formatData(); 
} catch (JSONException e) { 
    e.printStackTrace(); 
} 

View refresher = findViewById(R.id.refresher); 
((SwipeRefreshLayout) refresher).setRefreshing(false); 

marksFragment.notifyUpdate(activity); 
subjectsFragment.notifyUpdate(activity); 

它正常工作,但不是在这种情况下:

  • 我拉来刷新数据
  • 我浏览到主题细节(使用 碎片交易)
  • setRefreshing方法在 碎片交易期间被调用

在这种情况下,主题片段只是挂在那里,两个片段都在活动中。

SubjectFragment的代码:

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 
    ((SwipeRefreshLayout) view.findViewById(R.id.refresher)).setOnRefreshListener((SwipeRefreshLayout.OnRefreshListener) activity); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.subjects_fragment, null); 
} 

setRefreshing(false)上refreshLayout和如何避免让受试者片段两次?

+0

你可以张贴乌尔第一片段的onCreate()? –

+0

@PhanDinhThai我更新了这个问题。 – cuddlecheek

回答

1

我发现了一个类似的问题,以便您的代码,参考答案:

When switch fragment with SwipeRefreshLayout during refreshing, fragment freezes but actually still work

我使用程序兼容性-V7:23.3.0。这个答案适用于我:

这个问题似乎仍然发生在appcompat 22.1.1。包装 FrameLayout中的SwipeRefreshLayout为我解决了这个问题。

所以我的题目片段布局是这样的:

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v4.widget.SwipeRefreshLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/refresher" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <ListView 
      android:id="@android:id/list" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"/> 

    </android.support.v4.widget.SwipeRefreshLayout> 
</FrameLayout> 
0

更改上stack overflow

public class YourFragment extends Fragment { 
    SwipeRefreshLayout swipeLayout; 

    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 
     String[] values = new String[] { "Android List View", 
       "Adapter implementation", 
       "Simple List View In Android", 
       "Create List View Android", 
       "Android Example", 
       "List View Source Code", 
       "List View Array Adapter", 
       "Android Example List View", 
       "Android List View", 
       "Adapter implementation", 
       "Simple List View In Android", 
       "Create List View Android", 
       "Android Example", 
       "List View Source Code", 
       "List View Array Adapter", 
       "Android Example List View", 
       "Android List View", 
       "Adapter implementation", 
       "Simple List View In Android", 
       "Create List View Android", 
       "Android Example", 
       "List View Source Code", 
       "List View Array Adapter", 
       "Android Example List View" 
     }; 

     final SwipeRefreshLayout swipeView = (SwipeRefreshLayout)view. findViewById(R.id.refresher); 

     swipeView.setEnabled(false); 
     ListView lView = (ListView) view.findViewById(R.id.your_list_view); 
     ArrayAdapter<String> adp = new ArrayAdapter<String>(view.getContext(), 
       android.R.layout.simple_list_item_1, android.R.id.text1, values); 
     lView.setAdapter(adp); 

     swipeView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 
      @Override 
      public void onRefresh() { 
       swipeView.setRefreshing(true); 
       (new Handler()).postDelayed(new Runnable() { 
        @Override 
        public void run() { 
         swipeView.setRefreshing(false); 

        } 
       }, 3000); 
      } 
     }); 

     lView.setOnScrollListener(new AbsListView.OnScrollListener() { 
      @Override 
      public void onScrollStateChanged(AbsListView absListView, int i) { 

      } 

      @Override 
      public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 
       if (firstVisibleItem == 0) 
        swipeView.setEnabled(true); 
       else 
        swipeView.setEnabled(false); 
      } 
     }); 

    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.subjects_fragment, null); 
    } 


    } 
+0

谢谢,我一定会尝试。但为什么要把听众从活动中移动到片段上呢? – cuddlecheek

+0

它没有工作。它仍然造成同样的问题。 – cuddlecheek

+0

cuddlecheek检查答案..我编辑我的答案... –