2012-03-07 31 views
2

我的場景如下:
- 應用程序使用配置的操作欄顯示選項卡。
- 一個選項卡顯示包含多個動態替換爲片段的FrameLayouts的佈局。其中一些可以有ListViews。這裏的示例被簡化了,並且只有一個片段直接添加到xml佈局中。
- 第一次打開標籤時,一切正常
- 切換到另一個標籤,並返回將繪製ListViews,允許滾動,但沒有點擊事件。

如果我添加其他控件,如列表下的按鈕,它們將按預期工作。此外,使用這些控件後,列表也將開始獲取點擊事件。
只有在操作欄按鈕打開顯示其他片段的片段時,纔可以重現。如果他們打開列表片段,一切都按預期工作。

的重要代碼是:從開發指南
標籤監聽器:
ListView不會收到點擊事件

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

該選項卡的佈局,這裏被簡化了:從動作條打開片段

public static class TabListener<T extends Fragment> implements ActionBar.TabListener { 
    private Fragment mFragment; 
    private final Activity mActivity; 
    private final String mTag; 
    private final Class<T> mClass; 

    public TabListener(Activity activity, String tag, Class<T> clz) { 
     mActivity = activity; 
     mTag = tag; 
     mClass = clz; 
    } 

    public void onTabSelected(Tab tab, FragmentTransaction ft) {   
     if (mFragment == null) {    
      mFragment = Fragment.instantiate(mActivity, mClass.getName()); 
      ft.add(android.R.id.content, mFragment, mTag); 
     } else {    
      ft.attach(mFragment); 
     } 
    } 

    public void onTabUnselected(Tab tab, FragmentTransaction ft) { 
     if (mFragment != null) {    
      ft.detach(mFragment); 
     } 
    } 

    public void onTabReselected(Tab tab, FragmentTransaction ft) {   
    } 
} 


onCreateView方法僅顯示片段

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

    <fragment 
     android:id="@+id/runModesListFragment" 
     android:name="tab.fail.TabFragment" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </fragment> 

</LinearLayout> 

最後,列表

public class TabFragment extends ListFragment { 
    String[] listContent = { 
     "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", 
     "17" 
    }; 

    View view = null; 

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

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), 
       android.R.layout.simple_list_item_1, listContent); 
     getListView().setAdapter(adapter); 
    } 
} 

當選擇了選項卡再次onCreateView和onActivityCreated沒有得到任何電話。與操作欄按鈕打開列表的情況相比,這是我發現的唯一區別。不過,我不知道爲什麼這件事很重要,也不知道如何解決。

任何建議非常感謝:)

回答

1

我實施了一種解決方法。不好,並沒有解釋爲什麼只有在ListView中的選擇不工作,而滾動和其他視圖罰款,但它的工作。因爲onCreateView和onActivityCreated在這種情況下沒有被調用,所以我強制框架在片段上使用attach/detach來完成它。沒有任何明顯的性能影響,但我確信存在更好的解決方案。

僅供參考,代碼:

Fragment frag = getFragmentManager().findFragmentById(R.id.tabFragment); 
    if (frag == null) { 
     frag = new TabFragment(); 
     final FragmentTransaction ft = getFragmentManager().beginTransaction(); 
     ft.replace(R.id.tabFragment, frag); 
     ft.commit(); 
    } else { 
     final FragmentTransaction ft = getFragmentManager().beginTransaction(); 
     ft.detach(frag); 
     ft.attach(frag); 
     ft.commit(); 
    } 

我會因爲被張貼沒有別的想法標誌着這是公認的。