You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中如何通过同一按钮实现Fragment的显示与隐藏(仿亚马逊筛选按钮)

实现类似亚马逊筛选按钮的功能

功能需求:点击同一按钮,第一次点击显示筛选Fragment,再次点击隐藏该Fragment,同时支持Fragment向Activity传递数据。

核心实现思路

  • 用一个布尔变量flag标记Fragment的显示状态,初始为false(隐藏状态)
  • 点击按钮时,根据flag的值执行不同的FragmentTransaction操作:
    • 当flag为false时,通过add()方法将Fragment添加到布局容器中,同时把flag设为true
    • 当flag为true时,通过remove()方法移除该Fragment,同时把flag设为false
  • Fragment与Activity的通信:通过强转getActivity()获取Activity实例,调用Activity的公开方法传递数据

完整代码实现

1. 主Activity(business.java)

public class business extends AppCompatActivity {
    TextView t1, t2;
    Button hello;
    boolean flag = false;
    FragmentTransaction fragmentTransaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_business);
        hello = findViewById(R.id.hello);
        t1=findViewById(R.id.text1);
        t2=findViewById(R.id.textt2);
    }

    @Override
    protected void onStart() {
        super.onStart();
        hello.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager manager = getSupportFragmentManager();
                BusinessSort businessSort = new BusinessSort();
                fragmentTransaction = manager.beginTransaction();
                if (flag) {
                    fragmentTransaction.remove(businessSort);
                    flag=false;
                } else {
                    fragmentTransaction.add(R.id.frgmCont, businessSort);
                    flag=true;
                }
                fragmentTransaction.commit();
            }
        });
    }

    // 供Fragment调用的方法,用于更新Activity界面
    public void f1(String s1, String s2) {
        t1.setText(s1);
        t2.setText(s2);
    }
}

2. 主Activity布局(activity_business.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"
    tools:context=".business"
    android:orientation="vertical"
    android:id="@+id/businessLinearLayout">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="hello"
        android:id="@+id/hello" />

    <TextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="edit1"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="edit2"
        android:id="@+id/textt2"/>

    <!-- Fragment容器 -->
    <FrameLayout
        android:id="@+id/frgmCont"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>
</LinearLayout>

3. 筛选Fragment(BusinessSort.java)

public class BusinessSort extends Fragment {
    EditText editText1,editText2;
    Button submit;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // 加载Fragment布局
        View view =inflater.inflate(R.layout.fragment_business_sort, container, false);
        editText1=view.findViewById(R.id.edit1);
        editText2=view.findViewById(R.id.edit2);
        submit=view.findViewById(R.id.businessfragment_submit);
        
        // 提交按钮点击事件,将输入内容传递给Activity
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s1 = editText1.getText().toString();
                String s2 = editText2.getText().toString();
                business businessActivity = (com.example.project.business) getActivity();
                businessActivity.f1(s1,s2);
            }
        });
        return view;
    }
}

4. Fragment布局(fragment_business_sort.xml)

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".BusinessSort"
    android:orientation="vertical">

    <EditText
        android:id="@+id/edit1"
        android:layout_width="match_parent"
        android:layout_height="50dp"/>

    <EditText
        android:id="@+id/edit2"
        android:layout_width="match_parent"
        android:layout_height="50dp"/>

    <Button
        android:id="@+id/businessfragment_submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SUBMIT"/>

</LinearLayout>

小优化建议

如果想要Fragment隐藏时保留输入状态,可以把remove()换成hide()逻辑:比如提前初始化Fragment实例,第一次添加后,后续点击用show()和hide()切换,这样Fragment实例不会被销毁,输入内容也能保留~

内容的提问来源于stack exchange,提问作者Hemkant Trivedi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 21:02:33