如何在相同的片段布局中显示多个ListView?

时间:2018-08-17 15:18:38

标签: android listview android-fragments

我有2个片段类,每个片段类创建一个不同的ListView,

如何通过按按钮从片段1转到片段2

android:onClick="clickBtn"

反之亦然

Fragment_1.java

    public class Fragment_1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_fragment_1, container, false);

        ArrayList<String> names = new ArrayList<>();
        names.add("1");
        names.add("2");
        names.add("3");
        names.add("4");

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, names);

        ListView mylist = (ListView) v.findViewById(R.id.list);

        mylist.setAdapter(adapter);

        return v;
    }
}

Fragment_2.java

    public class Fragment_2 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_fragment_2, container, false);

        ArrayList<String> names = new ArrayList<>();
        names.add("A");
        names.add("B");
        names.add("C");
        names.add("D");

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, names);

        ListView mylist =(ListView) v.findViewById(R.id.list);

        mylist.setAdapter(adapter);

        return v;
    }
}

activity_main.xml

<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="vertical"
tools:context=".MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
        android:id="@+id/btn_1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="clickBtn"
        android:text="F 1" />

    <Button
        android:id="@+id/btn_2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="clickBtn"
        android:text="F 2" />
</LinearLayout>

<fragment
    android:name="com.app.mai.simplefragmentbybuttons.Fragment_1"
    android:id="@+id/toBeReplaced"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></fragment>

MainActivity.java

 public void clickBtn(View view) {

    if (view == findViewById(R.id.btn_1)) {

        Fragment fragmentObject = new Fragment_1();

        getFragmentManager().beginTransaction().replace(R.id.toBeReplaced, fragmentObject).commit();

    }

    if (view == findViewById(R.id.btn_2)) {
        Fragment fragmentObject = new Fragment_2();
        getFragmentManager().beginTransaction().replace(R.id.toBeReplaced, fragmentObject).commit();
    }
}

1 个答案:

答案 0 :(得分:0)

我会给你一个解决方案:

activity_main.xml

<!DOCTYPE html>
<html lang = "en">

<head>
  <title>Games</title>
  <meta charset = "utf-8">
  <meta name = "viewport" content = "width = device-width, initial-scale = 1.0">
  <link rel="stylesheet" href="../../css/styleMain.css">
  <link rel="stylesheet" href="breakout/breakout.css">
</head>

<body id = "gamesBody">

  <div class = "topNav" id = "indexTopNav">
    <h1 class = "pageHeaders" id = "gamesPageHeader">These are the games</h1>

    <a class = "navButton hvr-float-shadow" href = "../../index.html">Home</a>
    <a class = "navButton hvr-float-shadow" href = "../sports/sports.html">Sports</a>
    <a class = "navButton hvr-float-shadow" href = "../investing/investing.html">Investing</a>
    <a class = "active navButton hvr-float-shadow" href = "games.html">Games</a>
  </div>

  <div class = "container">
    <canvas id = "breakoutCanvas" style =" width:100% "></canvas>
    <input type = "range" min = "2" max = "30" value = "10" class = "slider" id = "ballSizeSlider">
    <input type = "range" min = "1" max = "8" value = "3" class = "slider" id = "rowSlider">
  </div>

  <script src="breakout/breakout.js"></script>

</body>
</html>

MainActivity.java

<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="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="clickBtn"
            android:text="F 1" />

        <Button
            android:id="@+id/btn_2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="clickBtn"
            android:text="F 2" />
    </LinearLayout>

    <FrameLayout
        android:id="@+id/toBeReplaced"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

尝试一下!

相关问题