2013-04-20 13 views
3

我有一個活動有兩個片段後,一個顯示使用適配器項目的ListView,以及關於特定列表項,單擊時的第二顯示信息。ListView控件不再點擊旋轉裝置

一切工作正常,但只要我旋轉設備和活動重新創建,我的ListView是搞砸了,有時不滾動或可點擊(沒有點擊動畫,沒有任何反應,當我點擊)。

任何想法?我不想從禁用清單中的配置更改我知道這是不是一個好的做法。

活動碼:

public class MainActivity extends FragmentActivity implements AppListFragment.AppListFragmentCallback{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     AppListFragment fragment = new AppListFragment(); 
     fragment.setArguments(getIntent().getExtras()); 
     fragment.setRetainInstance(true); 
     getSupportFragmentManager().beginTransaction().add(R.id.main_frame, fragment).commit(); 
    } 

    @Override 
    public void onAppSelected(String app) { 
     AppDetailsFragment fragment = new AppDetailsFragment(); 
     Bundle args = new Bundle(); 
     args.putString(AppDetailsFragment.KEY_APP, app); 
     fragment.setArguments(args); 
     fragment.setRetainInstance(true); 

     getSupportFragmentManager().beginTransaction().replace(R.id.main_frame, fragment).addToBackStack(null).commit(); 
    } 
} 

列表片段編碼:

public class AppListFragment extends Fragment { 

    public interface AppListFragmentCallback { 
     void onAppSelected(String app); 
    } 

    private AppListFragmentCallback callback; 


    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 

     // This makes sure that the container activity has implemented 
     // the callback interface. If not, it throws an exception 
     try { 
      callback = (AppListFragmentCallback) activity; 
     } catch (ClassCastException e) { 
      throw new ClassCastException(activity.toString() 
        + " must implement AppListFragmentCallback"); 
     }  
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_app_list, container, false); 

     AppListAdapter adapter = new AppListAdapter(); 

     ListView lv = (ListView)view.findViewById(R.id.view_app_list); 
     lv.setAdapter(adapter); 
     lv.setOnItemClickListener(new OnItemClickListener() { 

      @SuppressWarnings("rawtypes") 
      @Override 
      public void onItemClick(AdapterView parent, View view, int position, long id) { 
       callback.onAppSelected((String)parent.getAdapter().getItem(position)); 
      } 
     }); 

     return view; 
    } 
} 

適配器代碼:

public class AppListAdapter extends BaseAdapter implements ListAdapter { 

    private ArrayList<String> apps; 

    public AppListAdapter() { 

     apps = new ArrayList<String>(); 
     for (Integer i = 0; i < 50; i++) 
     { 
      apps.add(i.toString()); 
     } 
    } 

    @Override 
    public int getCount() { 
     return apps.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return apps.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     TextView text = new TextView(parent.getContext()); 
     text.setText("App number " + apps.get(position)); 
     text.setPadding(10, 3, 10, 3); 
     text.setBackgroundResource(R.drawable.button_xml); 
     return text; 
    } 
} 

詳細查看片段代碼:

公共類AppDetailsFragment分機結束片段{ 最終靜態字符串KEY_APP = 「key_app」;

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_app_details, container, false); 

    TextView textName = (TextView)view.findViewById(R.id.text_app_name); 
    TextView textDesc = (TextView)view.findViewById(R.id.text_app_description); 

    Bundle args = getArguments(); 
    String app = args.getString(KEY_APP); 
    if (app == null) 
     return view; 

    textName.setText(app); 
    textDesc.setText("App Description"); 

    return view; 
} 

}

+0

也許活性重建和觀點並不:

我通過編輯活動的onCreate()檢查片段是否存在,創建一個新的人之前解決這個問題呢?通過引用舊的Activity(回調)保留視圖? – 2013-04-20 22:25:19

+0

我不知道你所說的「觀看」的意思,但如果你的意思是片段,然後它肯定是相關的。見答案 – tbkn23 2013-04-20 22:43:48

回答

0

好吧,我設法閱讀這篇文章後弄明白:Why use Fragment#setRetainInstance(boolean)?

顯然,設置fragment.setRetainInstance(true);意味着片段不被破壞,但該活動被破壞,並在onCreate()創建即使它不被破壞的片段的新實例。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    AppListFragment fragment = (AppListFragment)getSupportFragmentManager().findFragmentByTag(TAG_LIST_FRAGMENT); 
    if (fragment == null) { 
     fragment = new AppListFragment(); 
     fragment.setRetainInstance(true); 
     fragment.setArguments(getIntent().getExtras()); 
     getSupportFragmentManager().beginTransaction().add(R.id.main_frame, fragment, TAG_LIST_FRAGMENT).commit(); 
    } 
}