旋转设备后,ListView不再可单击

时间:2013-04-20 22:12:23

标签: android listview android-fragments android-activity

我有一个包含两个片段的活动,一个使用适配器显示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;
    }
}

详细视图片段代码:

public class AppDetailsFragment extends Fragment {     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;
}

}

1 个答案:

答案 0 :(得分:0)

好的,我在阅读完这篇文章后设法弄明白了:Why use 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();
    }
}