0

我有一个活动和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吗?

非常感谢。

+0

我的问题是在XML。我正在使用而不是

回答

0

我觉得标签导航是更好,但你可以试试这个:在您的主要活动添加点击监听到你的按钮第一

1),然后用一个参数来区分:

button1.setOnClickListener(new View.OnClickListener(){ 
     @Override 
     public void onClick(View view){ 
      changeFragment(1) 
     } 
}; 

public void changeFragment(int whichfragment){ 
    switch(whichfragment){ 
     case 1: 
     fr=new FragmentOne(); 
     replaceFragment(); 
     break; 
     case 2: 
     fr=new FragmentTwo(); 
     replaceFragment(); 
     break; 
    } 

} 

2)现在,您可以取代你的片段

public void replaceFragment(){ 
    FragmentManager fragmentManager = getFragmentManager(); 
    FragmentTransaction transaction=fragmentManager.beginTransaction(); 
    transaction.replace(R.id.fragment_place, fr); 
transaction.commit(); 
} 
+0

是否有可能自定义按钮切换标签?你为什么认为更好?我会尽快测试你的代码,谢谢! –

+0

是的,你可以创建你自己的按钮。也许它有帮助: http://stackoverflow.com/questions/18074303/how-to-create-custom-shape-button-with-selector-in-android –