我试图在左侧所选元素的右侧细节上创建一个带有两个片段的简单应用程序,一个是左边的一个项目列表。它工作正常,但当我改变我的设备的方向,然后列表的某些位置在背景上“冻结”。任何人都可以帮助我吗?
我的活动是onCreate方法:
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i("on ", "createActivity");
FragmentTransaction fTrans=getFragmentManager().beginTransaction();
fTrans.add(R.id.frame1, fragmentList);
fTrans.add(R.id.frame2, detailFragment);
//detailFragment.setData("name 0", "src 0", BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
fTrans.commit();
DetailFragment:
public class DetailFragment extends Fragment {
private TextView tv1;
private TextView tv2;
private ImageView iw;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.detail_fragment , container, false);
tv1=(TextView)view.findViewById(R.id.detailText1);
tv2=(TextView)view.findViewById(R.id.detailText2);
iw=(ImageView)view.findViewById(R.id.detailImage);
return view;
}
public void setData(String text1, String text2, Bitmap bmp) {
tv1.setText(text1);
tv2.setText(text2);
iw.setImageBitmap(bmp);
}
ListFragment:
public class FragmentList extends Fragment {
ArrayList<HashMap<String, Object>> list=new ArrayList<HashMap<String, Object>>();
String[] from={"name", "imageSource", "image"};
int[] to={R.id.text1,R.id.text2,R.id.imageView};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
HashMap<String, Object> m=new HashMap<String, Object>();
for(int i=0; i<=19; i++) {
m=new HashMap<String, Object>();
m.put("name", "name "+i);
m.put("imageSource", "src "+i);
m.put("image", BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
list.add(m);
}
Log.i("on ", "createView");
View view=inflater.inflate(R.layout.fragment_list, null);
MyAdapter myAdapter=new MyAdapter(getActivity(), list, R.layout.row, from, to);
ListView listView=(ListView)view.findViewById(R.id.listView);
listView.setAdapter(myAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
DetailFragment df=(DetailFragment)getFragmentManager().findFragmentById(R.id.frame2);
df.setData((String)list.get(i).get("name"), (String)list.get(i).get("imageSource"), (Bitmap)list.get(i).get("image"));
}
});
return view;
}
}
答案 0 :(得分:0)
你的意思是这样吗?
尝试将您的交易添加更改为替换:
FragmentTransaction fTrans=getFragmentManager().beginTransaction();
fTrans.replace(R.id.frame1, fragmentList);
当方向改变时,onCreate()将覆盖listview片段而不删除前一个。
答案 1 :(得分:-1)
android:configChanges="keyboard|orientation"
在应用程序代码中尝试清单:
<application
android:allowBackup="true"
android:configChanges="keyboard|orientation"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
由于我的英语,我没有详细说明。这是因为如果您想要在方向改变时不会发生任何变化。