Android 当我们导航并再次访问同一个片段时,如何保持ListView项处于选中状态?
我有一个列表视图,它在片段中。我正在单击它保持选中状态的项目,因为我在listselected.xml中使用了以下代码Android 当我们导航并再次访问同一个片段时,如何保持ListView项处于选中状态?,android,android-fragments,android-listview,selecteditem,Android,Android Fragments,Android Listview,Selecteditem,我有一个列表视图,它在片段中。我正在单击它保持选中状态的项目,因为我在listselected.xml中使用了以下代码 但是当我从一个片段转到另一个片段,当我再次访问同一个片段时,它就会被删除,我想这样做。请帮帮我。创建OnCreateView时,列表视图会动态填充。我以前也遇到过同样的问题。您可以像这样保存片段状态。在片段中保存视图 View myLayout=null; public View onCreateView(LayoutInflater inflater, ViewGr
但是当我从一个片段转到另一个片段,当我再次访问同一个片段时,它就会被删除,我想这样做。请帮帮我。创建OnCreateView时,列表视图会动态填充。我以前也遇到过同样的问题。您可以像这样保存片段状态。在片段中保存视图
View myLayout=null;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
if(myLayout==null){
//initialize your layout here.
myLayout = inflater.inflate(R.layout.frag_layout, container, false);
}
else{
try{
((ViewGroup)myLayout.getParent()).removeView(myLayout);
}catch(Exception e){
//an exception will be thrown when the very first time it trying to remove view from the parent
}
}
return myLayout;
}
是否希望列表视图显示为导航到另一个片段之前的状态?我的意思是你不希望它被删除?我每次在片段上设置列表视图适配器。因此,我存储了位置并每次更改颜色,但它选择了多个值。当我单击时。你能显示代码吗?我不太明白你到底想做什么。你正在正确地设置一个可绘制的自定义listview状态。您还需要保存每行项目的状态(选中或未选中)。如果您在getView()中发布代码,那么我可以为其提供示例代码。
View myLayout=null;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
if(myLayout==null){
//initialize your layout here.
myLayout = inflater.inflate(R.layout.frag_layout, container, false);
}
else{
try{
((ViewGroup)myLayout.getParent()).removeView(myLayout);
}catch(Exception e){
//an exception will be thrown when the very first time it trying to remove view from the parent
}
}
return myLayout;
}