【问题标题】:Convert ActivityGroup to Fragment将 ActivityGroup 转换为 Fragment
【发布时间】:2012-03-06 13:39:49
【问题描述】:

我正在尝试将我的整个项目从使用 ActivityGroups 转换为使用 Fragments。

这是我的旧代码:

设置活动组

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);
}

设置活动

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 而不是 Activity - 我需要进行哪些更改以确保一切正常?

【问题讨论】:

    标签: android listview android-fragments


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2020-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多