我有一个包含两个片段的活动,一个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;
}
}