2012-03-06 91 views
0

我想将我的整个项目从使用ActivityGroups转换为使用Fragments。将ActivityGroup转换为片段

这里是我的旧代码:

SettingsActivityGroup

public class SettingsActivityGroup extends ActivityGroup 
{ 
// Keep this in a static variable to make it accessible for all the nested activities, lets them manipulate the view 
public static SettingsActivityGroup group; 

// Need to keep track of the history if you want the back-button to work properly, don't use this if your activities requires a lot of memory. 
private ArrayList<View> history; 

// Window focus changed listener 
public OnActivityGroupViewChanged activityGroupViewChangedListener = null; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 

    // Allocate history 
    this.history = new ArrayList<View>(); 

    // Set group 
    group = this;    

    // Start root (first) activity 
    Intent myIntent = new Intent(this, SettingsActivity.class); // Change to the first activity of your ActivityGroup 
    myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    ReplaceView("SettingsActivity", myIntent); 
} 

SettingsActivity

public class SettingsActivity extends Activity 
{ 
String[] settingsLabels = {"Viderestilling", "Voicemail", "Vis nummer", 
      "Kø styring", "Optag samtaler", "Services" }; 
ListView lv; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.settings); 
    lv = (ListView) findViewById(R.id.SettingsLV); 

    lv.setTextFilterEnabled(true); 

    populateListView(); 


} 

private void populateListView() 
{ 

     lv.setAdapter(new ArrayAdapter<String>(this, R.layout.settings_items, R.id.settings_item_label, settingsLabels)); 
     lv.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, 
      int position, long id) 
     { 
      // SwitchActivity(position); 
     } 
     }); 
} 

如果我想延长ListFragment而不是活动 - 我需要做什么改变确保一切仍然有效?

回答

0

你看过关于Fragments的开发者指南文章吗?我认为它几乎描述了你的确切用例(一个片段中的数组支持列表和另一个片段中的细节)。请检查APIDemos中的完整示例实现。在API 4+ Support Demos中甚至有一个向后兼容的版本。