我有一个活动和3个片段。在主要活动的底部,我有3个按钮用于切换活动片段。每个片段都包含一个ListView,你认为这是一种很好的开发方式吗?我已经开发了第一个从服务器上下载json的Fragment,解析并添加元素到ListView,但是当我按下按钮切换到Fragment2时,第一个保持不变。带有ListView的片段保留
这是主要活动,带有listView的片段在某些TextView和一个ImageView内使用自定义适配器。如果我删除listView,碎片更改没有麻烦。
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
public class Home extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_home);
}
public void selectFrag(View view) {
Fragment fr;
int stringTitleID = R.string.title_fragment1;
if(view == findViewById(R.id.imgBtn3))
{
fr = new FragmentThree();
stringTitleID = R.string.title_fragment3;
}
else if(view == findViewById(R.id.imgBtn2))
{
fr = new FragmentTwo();
stringTitleID = R.string.title_fragment2;
}
else
{
fr = new FragmentOne();
stringTitleID = R.string.title_fragment1;
}
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.commit();
// I change the title in the top_bar
TextView textViewActivityTitle = (TextView) findViewById(R.id.textViewActivityTitle);
textViewActivityTitle.setText(getResources().getString(stringTitleID).toString());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
/*
* This functions clears the app cache. The json responses are stored in che cacheDir or cacheExternalDir.
* I clean them when the app is started. I only use the cache between the Fragments
*/
}
碎片解决方案是一个很好的解决方案吗?为什么带有listview的片段保持不变? 我应该使用ListFragment吗?
非常感谢。
我的问题是在XML。我正在使用 而不是