android-fragments LinearLayout中的片段- Android

9lowa7mx  于 2022-11-14  发布在  Android
关注(0)|答案(2)|浏览(263)

我对片段不熟悉,我尝试使用这些片段。我的活动中的xml是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Loguin"
tools:ignore="MergeRootFrame">

    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Fragment Test"
    android:id="@+id/btn_FragmentTest"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="20dp"
    android:layout_marginBottom="0dp" />

    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:id="@+id/frgContainer"
    android:layout_marginTop="20dp">

    <fragment
        android:id="@+id/frgLoguin"
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="habitatprojects.hubbuildings.Loguin$PlaceholderFragment"
        tools:layout="@layout/fragment_loguin" />

    </LinearLayout>


</LinearLayout>

我想改变LinearLayout里面的片段,我的mainActivity类是:

public class Loguin extends Activity {

private int num = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_loguin);
    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }

    Button btnBoton1 = (Button)findViewById(R.id.btn_FragmentTest);

    btnBoton1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0)
        {
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

            if(num==0)
            {
                fragmentTransaction.replace(R.id.frgContainer, new PlaceholderFragment())
                        .commit();

                num++;
            }
            else{
                if(num==1)
                {
                    fragmentTransaction.replace(R.id.frgContainer, new PlaceholderFragment2())
                            .commit();

                    num++;
                }
                else{
                    fragmentTransaction.replace(R.id.frgContainer, new PlaceholderFragment3())
                            .commit();

                    num = 0;
                }
            }
        }
    });
 }

我的占位符函数是:

public static class PlaceholderFragment extends Fragment {

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

public static class PlaceholderFragment2 extends Fragment {

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

public static class PlaceholderFragment3 extends Fragment {

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

为什么我一按按钮就不工作了?我做错什么了?为我的英语道歉,提前感谢!
如果你需要更多的代码或信息,请告诉我,谢谢!
PD 1:当我调试我的设备时,日志猫只会说我:
11-18 11:33:30.775 6065-6065/居住项目.枢纽建筑I/ViewRootImpl ▪ ViewRoot的触摸事件:11-18 11:33:30.835 6065-6065/居住项目. hubbuilding I/ViewRootImpl﹕ ViewRoot的触摸事件:补漆

yhxst69z

yhxst69z1#

如此简单:

您的主要活动很可能如下所示。activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:id="@+id/your_vertical_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    </LinearLayout>
</ScrollView>

假设你有一个片段OneRow
下面是添加三行的方法。

private void addRows() {
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.add(R.id.your_vertical_list, new OneRow());
    ft.add(R.id.your_vertical_list, new OneRow());
    ft.add(R.id.your_vertical_list, new OneRow());
    ft.commit();
}

请注意,在.add中,您只需传入布局id R.id.your_vertical_list
通常您需要行,因此

public class MainActivity extends AppCompatActivity {

    ArrayList<OneRow> rows = new ArrayList<OneRow>();

    private void addRows() {
        FragmentTransaction ft =
          getSupportFragmentManager().beginTransaction();
        for (int i = 0; i < 20; i++) {
            OneRow r = new OneRow();
            rows.add(r);
            ft.add(R.id.your_vertical_list, r);
        }
        ft.commit();
    }

但是,请注意,通常最好为每个项目提交,特别是在设置任何文本标签等时。例如

private void addRows() {
        for (int i = 0; i < 20; i++) {
            FragmentTransaction ft =
              getSupportFragmentManager().beginTransaction();
            OneRow r = new OneRow();
            r.set
            rows.add(r);
            ft.add(R.id.your_vertical_list, r);
            ft.commit();
        }
    }
soat7uwm

soat7uwm2#

解决了我只需要改变我的线性布局为框架布局!谢谢大家!

相关问题