【问题标题】:How to open and close fragment,clicking on the same button in android如何打开和关闭片段,点击android中的同一个按钮
【发布时间】:2020-06-09 15:54:18
【问题描述】:

如何在单击同一个按钮时打开和关闭片段。就像亚马逊过滤器按钮一样。 当我们第一次点击片段应该出现,然后我们再次点击该按钮片段应该消失。

业务活动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();
        }
    });

}


public void f1(String s1, String s2) {
    t1.setText(s1);
    t2.setText(s2);
}

}

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"/>

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

Fagment 文件 BusinessSort.java

public class BusinessSort extends Fragment {

EditText editText1,editText2;
Button submit;
 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this 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);
    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String s1 = editText1.getText().toString();
            String s2 = editText2.getText().toString();
            business business = (com.example.project.business) getActivity();
            business.f1(s1,s2);

        }
    });

    return view;
}

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>

【问题讨论】:

    标签: java android android-studio android-layout android-fragments


    【解决方案1】:

    尝试替换片段。

     fragmentTransaction.replace(R.id.frgmCont, businessSort);
     flag = true;
    

    我希望代码与我的方法大部分相似,所以如果问题仍然存在,请告诉我,以便我更深入地研究它。

    【讨论】:

      【解决方案2】:
      public static boolean fragmentState = false;
      
      onClick(){
          if(fragmentState){
                fragmentState = false;
                getActivity().getFragmentManager().beginTransaction().
                remove(SomeFragment.this).commit();
          }
          else{
               fragmentState = true;
               Fragment someFragment = new SomeFragment(); 
               FragmentTransaction transaction = 
                   getFragmentManager().beginTransaction();
               transaction.replace(R.id.fragment_container, someFragment );
               transaction.addToBackStack(null); 
               transaction.commit(); 
          }
      }
      

      请确保您的按钮不是该片段的一部分。 您需要保存片段的状态。

      【讨论】:

        猜你喜欢
        • 2015-12-18
        • 2020-08-12
        • 2021-10-28
        • 2015-11-23
        • 2014-07-28
        • 1970-01-01
        • 2011-06-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多