3

我有一个包含两个片段的活动,一个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 扩展片段 { final static String 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;
}

}

4

1 回答 1

0

好的,我在阅读这篇文章后设法弄清楚:为什么使用 Fragment#setRetainInstance(boolean)?

显然,设置fragment.setRetainInstance(true);意味着片段不被破坏,但是活动被破坏并onCreate()创建片段的新实例,即使它没有被破坏。

在创建新的之前,我通过编辑活动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();
    }
}
于 2013-04-20T22:43:09.907 回答