gpt4 book ai didi

android - 旋转设备后 ListView 不再可点击

转载 作者:太空狗 更新时间:2023-10-29 15:11:33 25 4
gpt4 key购买 nike

我有一个包含两个 fragment 的 Activity ,一个使用适配器显示项目的 ListView,第二个在单击时显示有关特定列表项的信息。

一切正常,只是一旦我旋转设备并重新创建 Activity,我的 ListView 就乱七八糟,有时无法滚动或无法点击(没有点击动画,点击时没有任何反应)。

有什么想法吗?我不想从 list 中禁用配置更改,因为我知道这不是一个好的做法。

Activity 代码:

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();
}
}

列表 fragment 代码:

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;
}
}

详细查看 fragment 代码:

公共(public)类 AppDetailsFragment 扩展 fragment { 最终静态字符串 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;
}

最佳答案

好的,我在阅读这篇文章后设法弄明白了:Why use Fragment#setRetainInstance(boolean)?

显然,设置 fragment.setRetainInstance(true); 意味着 fragment 没有被销毁,但是 Activity 被销毁并在 onCreate() 中创建一个新实例 fragment 的一部分,即使它没有被破坏。

我通过编辑 Activity 的 onCreate() 来检查 fragment 是否存在,然后再创建新 fragment ,从而解决了这个问题:

@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();
}
}

关于android - 旋转设备后 ListView 不再可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16125724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com