如何以编程方式将多个片段添加到android中的活动

时间:2014-11-03 05:53:20

标签: android android-activity android-fragments

我想在线性布局中显示6行。我希望通过片段来实现这一点,因为内容将是动态的,行数也将在以后动态。我有以下代码,但屏幕上只显示一行。我有SettingsActivity.java,settings.xml ThemeRowFragment,java和theme_row_layout.xml。

SettingsActivity.java

//imports

public class SettingsActivity extends FragmentActivity {

private int NUM_THEMES = 7;

ThemeRowFragment[] view_themes;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings);

    view_themes = new ThemeRowFragment[NUM_THEMES];

    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager
            .beginTransaction();
    for (int i = 0; i < view_themes.length; i++) {
        view_themes[i] = new ThemeRowFragment(COLOR_MAP[i]);
        fragmentTransaction.add(R.id.theme_linear_layout, view_themes[i],
                "Row" + i);
    }
    fragmentTransaction.commit();

}

}

的settings.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="@+id/theme_linear_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/string_theme"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="@string/string_theme"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#cde21c" />

</LinearLayout>

ThemeRowFragment.java

public class ThemeRowFragment extends Fragment {

private int[] colors;

public ThemeRowFragment(int colors[]) {
    super();
    this.colors = colors;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.theme_row_layout, container,
            false);
    return view;
}

}

theme_row_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/pick_colors" >
</TextView>

</LinearLayout>

2 个答案:

答案 0 :(得分:2)

碎片会将自己膨胀到您添加到的视图中。所以你真的不能这样做。所以你需要有X个空容器,每个容器对应一个你要膨胀的片段。将每个片段添加到同一个容器中实际上将它们全部叠加在一起,这使得它们在屏幕呈现时很难看到和使用。

替代品: 您可以执行以下操作:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="@+id/theme_linear_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/string_theme"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="@string/string_theme"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#cde21c" />

 <FrameLayout android:id="@+id/fragment_container1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
 <FrameLayout android:id="@+id/fragment_container2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
 <FrameLayout android:id="@+id/fragment_container3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
 <FrameLayout android:id="@+id/fragment_container4"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
 <FrameLayout android:id="@+id/fragment_container5"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
 <!-- etc. -->
</LinearLayout>

或者只需通过FrameLayout LinearLayout以编程方式添加每个addView(),并为每个FrameLayout添加唯一ID。

LinearLayout layout = (LinearLayout)findViewById(R.id.linear);
FragmentTxn txn = getFragmentManager.beginTransaction();
int i = 1; // This seems really fragile though
for (Fragment f : fragments) {
      FrameLayout frame = new FrameLayout(this);
      frame.setId(i);
      layout.addView(frame);
      txn.add(i, f);
      i++;
 }
 txn.commit();

或者另一种方法是使用listView并以这种方式添加每一行。根本不使用碎片。

    

<TextView
    android:id="@+id/string_theme"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="@string/string_theme"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#cde21c" />

 <ListView android:id="@+id/listview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

</LinearLayout>

然后再做类似的事情:

ListView view = (ListView)findViewById(R.id.listview);
view.setAdapter(new ArrayAdapter(items) { // items is a collection of objects you are representing
     public View getView(int position, View view, ViewGroup parent) {
           view = LayoutInflator.from(parent.getContext()).inflate(R.layout.theme_row_layout, parent, false);
           // manipulate the view
           return view;
  });

答案 1 :(得分:0)

您正在创建ThemeRowFragment n次的实例。问题是您是将其创建为片段并尝试动态添加它。由于您实例化同一Fragment我建议您使用ListView并使用custom adapter并设置CustomView并覆盖适配器的getView方法以调整您的观点