【问题标题】:How to replace and open a fragment from another fragment in android by the click of a button?如何通过单击按钮替换和打开android中另一个片段的片段?
【发布时间】:2020-08-12 07:41:55
【问题描述】:

我无法删除我的第二个片段中的现有片段,同时尝试从第一个片段中替换它,同时尝试通过单击按钮从第一个片段中打开第二个片段。

这是我的代码

FirstFragment.java

package com.example.testanderase;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.design.widget.TextInputEditText;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.List;
import java.util.Objects;

public class FirstFragment extends Fragment implements View.OnClickListener{

    TextInputEditText inputEditText;
    Button enterButton;
    String getMessage;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.first_fragment,container,false);
        inputEditText = view.findViewById(R.id.enter_edit_txt_input);
        enterButton = view.findViewById(R.id.first_fragment_btn);
        enterButton.setOnClickListener(this);

        return view;
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public void onClick(View v) {
        getMessage = inputEditText.getText().toString();

        SecondFragment secondFragment = new SecondFragment();
        Bundle b = new Bundle();
        b.putString("data",getMessage);
        secondFragment.setArguments(b);

        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.second_fragment_layout,secondFragment);
        //transaction.add(R.id.second_fragment_layout,secondFragment);
        transaction.commit();

***下面我正在尝试更改代码 ***为了从第一个片段打开第二个片段 /断言 getFragmentManager() != null; 片段管理器 fm = getFragmentManager(); fm.beginTransaction().replace(R.id.second_fragment_layout,secondFragment).commit();/ //fm.beginTransaction().replace(SecondFragment.newInstance()); } }

first_fragment.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="vertical"
    android:gravity="center">

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

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginEnd="20dp">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/enter_edit_txt_input"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="@string/input"
            android:textAlignment="center"
            android:gravity="center_horizontal" />

    </android.support.design.widget.TextInputLayout>

    <Button
        android:id="@+id/first_fragment_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="@string/enter"
        android:background="@color/black"
        android:textColor="@color/white"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        />
    </LinearLayout>
</LinearLayout>

SecondFragment.java

package com.example.testanderase;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class SecondFragment extends Fragment implements View.OnClickListener{

    TextView textView;
    Button enterButton;
    String firstFragmentValue;
    Toast t;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.second_fragment,container,false);
        textView = view.findViewById(R.id.second_fragment_txt_view);
        enterButton = view.findViewById(R.id.second_fragment_btn);
        enterButton.setOnClickListener(this);

        //assert getArguments() != null;
        //firstFragmentValue = getArguments().getString("data");
        //textView.setText(firstFragmentValue);

        Bundle bundle = getArguments();
        if(bundle != null)
        {
            String name = bundle.getString("data");
            textView.setText(name);
            Toast.makeText(getContext(),"passed data "+name,Toast.LENGTH_SHORT).show();
        }

        //textView.setText(firstFragmentValue);

        return view;
    }

    @Override
    public void onClick(View v) {
        //
        ToastButton();
        /*assert getFragmentManager() != null;
        FirstFragment firstFragment = new FirstFragment();
        FragmentManager fragmentTransaction = getFragmentManager();
        fragmentTransaction.beginTransaction().replace(R.id.first_fragment_layout, firstFragment).commit();*/
    }

    public void ToastButton()
    {
        if(t!=null)
        {
            t.cancel();
        }
        else
        {
            Toast.makeText(getContext(),"Clicked",Toast.LENGTH_SHORT).show();
        }
    }
}

second_fragment.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="vertical"
    android:gravity="center">


    <TextView
        android:id="@+id/second_fragment_txt_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginEnd="20dp"
        android:text="@string/change_text"
        android:textAlignment="center"
        android:gravity="center_horizontal"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        />

    <Button
        android:id="@+id/second_fragment_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="@string/back_button_text"
        android:background="@color/black"
        android:textColor="@color/white"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        />
</LinearLayout>

SectionPageAdapter.java

package com.example.testanderase;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

public class SectionPageAdapter extends FragmentPagerAdapter {

    private final List<Fragment> fragmentList = new ArrayList<>();
    private final List<String> fragmentListTitle = new ArrayList<>();

    public void addFragment(Fragment fragment, String title)
    {
        fragmentList.add(fragment);
        fragmentListTitle.add(title);
    }

    public SectionPageAdapter(FragmentManager fm)
    {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        return fragmentList.get(i);
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return fragmentListTitle.get(position);
    }

    @Override
    public int getCount() {
        return fragmentList.size();
    }
}

MainActivity.java

package com.example.testanderase;

import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity{

    private SectionPageAdapter mSectionPageAdapter;
    private ViewPager mainViewPager;

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

            mSectionPageAdapter = new SectionPageAdapter(getSupportFragmentManager());

            mainViewPager = findViewById(R.id.container);
            setMainViewPager(mainViewPager);

            TabLayout tabLayout = findViewById(R.id.tabs);
            tabLayout.setupWithViewPager(mainViewPager);
    }

    public void setMainViewPager(ViewPager viewPager)
    {
        SectionPageAdapter adapter = new SectionPageAdapter(getSupportFragmentManager());
        adapter.addFragment(new FirstFragment(),"FIRST");
        adapter.addFragment(new SecondFragment(),"SECOND");
        viewPager.setAdapter(adapter);
    }
}

activity_main.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"
    android:gravity="center"
    tools:context=".MainActivity"
    android:id="@+id/main_layout"
    android:layout_margin="20dp">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>


</LinearLayout>

【问题讨论】:

    标签: android android-studio android-fragments android-button


    【解决方案1】:

    R.id.second_fragment_layout 在哪里

    无论如何将您的 second_fragment.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="vertical"
    android:gravity="center">
    <LinearLayout
        android:id="@+id/second_fragment_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center">
    
    <TextView
        android:id="@+id/second_fragment_txt_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginEnd="20dp"
        android:text="hello"
        android:textAlignment="center"
        android:gravity="center_horizontal"
    
    android:textAppearance="@style/TextAppearance.AppCompat.Large"
        />
    
    <Button
        android:id="@+id/second_fragment_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="back"
        android:background="#000"
        android:textColor="#fff"
    
    android:textAppearance="@style/TextAppearance.AppCompat.Large"
        />
    </LinearLayout>
    </LinearLayout>
    

    并通过单击按钮从第一个片段中打开第二个片段

    首先将 mainViewPager 设为静态

    静态ViewPager mainViewPager

    将其放在第一个片段 onclick

    int page =1; mainViewPager.setCurrentItem(page);

    回到第一个片段

    把这个放在第二个片段上

    int 页面 =0; mainViewPager.setCurrentItem(page);

    为我工作。!

    【讨论】:

      猜你喜欢
      • 2021-10-27
      • 2015-12-18
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-28
      • 1970-01-01
      相关资源
      最近更新 更多