将LinearLayout活动链接到片段

时间:2017-06-23 06:02:38

标签: java android android-layout android-fragments

我无法将我的MainActivity链接到任何片段 这是我的MainActivity.java:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

Button btn1,btn2,btn3,btn4,btn5;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn1=(Button)findViewById(R.id.btn1);
    btn2=(Button)findViewById(R.id.btn2);
    btn3=(Button)findViewById(R.id.btn3);
    btn4=(Button)findViewById(R.id.btn4);
    btn5=(Button)findViewById(R.id.btn5);

    btn1.setOnClickListener(this);
    btn2.setOnClickListener(this);
    btn3.setOnClickListener(this);
    btn4.setOnClickListener(this);
    btn5.setOnClickListener(this);
}

@Override
public void onClick(View v) {

    onItemSelected(v.getId());
    /*Fragment fragment=null;
    int id=v.getId();
    switch(id){
        case R.id.btn1:
            fragment=new Btn1Fragment();
            break;
        case R.id.btn2:
            fragment=new Btn2Fragment();
            break;
        case R.id.btn3:
            fragment=new Btn3Fragment();
            break;
        case R.id.btn4:
            fragment=new Btn4Fragment();
            break;
        case R.id.btn5:
            finish();
    }

    FragmentManager fm=getSupportFragmentManager();
    fm.beginTransaction().replace(R.id.main,fragment).commit();
    LinearLayout linear=(LinearLayout)findViewById(R.id.main);*/


}

private void onItemSelected(int id) {
    Fragment fragment=null;
    switch(id){
        case R.id.btn1:
            fragment=new Btn1Fragment();
            break;
        case R.id.btn2:
            fragment=new Btn2Fragment();
            break;
        case R.id.btn3:
            fragment=new Btn3Fragment();
            break;
        case R.id.btn4:
            fragment=new Btn4Fragment();
            break;
        case R.id.btn5:
            finish();
    }

    FragmentManager fm=getSupportFragmentManager();
    fm.beginTransaction().replace(R.id.main,fragment).addToBackStack(null).commit();
}

还有一个片段:

    public class Btn1Fragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view= inflater.inflate(R.layout.fragment_btn1,container,false);
    return view;
}

帮助我任何人。

1 个答案:

答案 0 :(得分:0)

您可以尝试以下代码: -

     public class MainActivity extends AppCompatActivity implements View.OnClickListener {

        Button btn1,btn2,btn3,btn4,btn5;
         Fragment fragment;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            btn1=(Button)findViewById(R.id.btn1);
            btn2=(Button)findViewById(R.id.btn2);
            btn3=(Button)findViewById(R.id.btn3);
            btn4=(Button)findViewById(R.id.btn4);
            btn5=(Button)findViewById(R.id.btn5);

            btn1.setOnClickListener(this);
            btn2.setOnClickListener(this);
            btn3.setOnClickListener(this);
            btn4.setOnClickListener(this);
            btn5.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch(v.getId()){
                case R.id.btn1:
                    fragment=new Btn1Fragment();
                    break;
                case R.id.btn2:
                    fragment=new Btn2Fragment();
                    break;
                case R.id.btn3:
                    fragment=new Btn3Fragment();
                    break;
                case R.id.btn4:
                    fragment=new Btn4Fragment();
                    break;
                case R.id.btn5:
                    finish();
            }

FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.main, fragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();
        }

     }

Btn1Fragment

   public class Btn1Fragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,
        ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_btn1, null);
return v;
}

使用此xml。

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.apkglobal.projectclick.MainActivity"
>


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

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/btn1"
    android:text="Black Screen"
    style="@style/textstyle"
    />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/btn2"
    android:text="Flashing Colors"
    style="@style/textstyle"
    android:layout_below="@+id/btn1"/>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/btn3"
    android:text="Back Button wali Screen"
    style="@style/textstyle"
    android:layout_below="@+id/btn2"/>
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/btn4"
    android:text="Jaado Dekhlo"
    style="@style/textstyle"
    android:layout_below="@+id/btn3"/>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/btn5"
    android:text="Exit"
    style="@style/textstyle"
    android:layout_below="@+id/btn4"/>


</LinearLayout>

    <LinearLayout
        android:id="@+id/main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
    />
</LinearLayout>