我正在学习Android编程。我在我的应用程序中使用BottomNavigationView使用片段过渡。以下是我遇到的问题。切换选项卡时,每次移动时,选项卡上的代码都会重新执行。某些选项卡对问题的响应非常慢。我要做的只是运行一次,并像活动的onCreate一样保持存储。我也想拥有一堆以前执行过的片段。附件是我的代码的一部分。 句子可能不太流畅,但希望您能理解。我可以随时为您服务。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="?attr/actionBarSize"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="@menu/menu_bottom"
app:labelVisibilityMode="unlabeled"
/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private FragmentManager fragmentManager = getSupportFragmentManager();
private menu_1 menu1Fragment = new menu_1();
private menu_2 menu2Fragment = new menu_2();
private menu_5 menu5Fragment = new menu_5();
private fragment_menu_4_like fragment_menu_4_like = new fragment_menu_4_like();
@SuppressLint("WrongViewCast")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation_view);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.frame_layout, menu1Fragment).commitAllowingStateLoss();
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
FragmentTransaction transaction = fragmentManager.beginTransaction();
switch (item.getItemId()) {
case R.id.navigation_menu1: {
transaction.replace(R.id.frame_layout, menu1Fragment).commitAllowingStateLoss();
break;
}
case R.id.navigation_menu2: {
transaction.replace(R.id.frame_layout, menu2Fragment).commitAllowingStateLoss();
break;
}
case R.id.navigation_menu3: {
transaction.replace(R.id.frame_layout, menu5Fragment).commitAllowingStateLoss();
break;
}
case R.id.navigation_menu4: {
transaction.replace(R.id.frame_layout, fragment_menu_4_like).commitAllowingStateLoss();
break;
}
case R.id.navigation_menu5: {
transaction.replace(R.id.frame_layout, menu5Fragment).commitAllowingStateLoss();
break;
}
}
return true;
}
});
}
public void replaceFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_layout, fragment).commit();
}
}
fragment_menu_1.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".menu_1">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/missionDetailAppbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/missionDetailCollapsingToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:background="#FFFFFF"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
>
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="10"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
>
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2" />
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:padding="10dp"
android:src="@drawable/supermarket" />
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5" />
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:padding="10dp"
android:src="@drawable/password" />
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5" />
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:text="FitMe"
android:gravity="center"
android:textColor="#000000"
android:textSize="35dp" />
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5" />
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2" />
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:padding="10dp"
android:src="@drawable/search" />
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5" />
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="10dp"
android:src="@drawable/menu" />
</LinearLayout>
</com.google.android.material.appbar.CollapsingToolbarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="80dp"
android:minHeight="80dp"
android:src="@drawable/test123123"
android:scaleType="fitXY"
android:contentDescription="TODO" />
</LinearLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/test_recyclerview"
android:layout_width="match_parent"
android:layout_height="450dp"
android:layout_marginBottom="15dp" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/main.scrollview.recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
menu_1.java
public class menu_1 extends Fragment {
RecyclerView test_image_recyclerview;
main_slide_image_recyclerview_adapter adapter;
ArrayList<recycerlveiw_array> array;
RecyclerView.LayoutManager layoutManager;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_menu_1, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
test_image_recyclerview = (RecyclerView) view.findViewById(R.id.test_recyclerview);
array = new ArrayList<>();
adapter = new main_slide_image_recyclerview_adapter(getActivity(), array);
layoutManager = new LinearLayoutManager(getActivity());
((LinearLayoutManager) layoutManager).setOrientation(LinearLayoutManager.HORIZONTAL);
SnapHelper snapHelper = new PagerSnapHelper();
test_image_recyclerview.setLayoutManager(layoutManager);
snapHelper.attachToRecyclerView(test_image_recyclerview);
test_image_recyclerview.setAdapter(adapter);
Uri uri = Uri.parse("android.resource://"+"com.example.testfitmelayouttest"+"/drawable/image2");
Uri uri2 = Uri.parse("android.resource://"+"com.example.testfitmelayouttest"+"/drawable/testimage_slide_1");
Uri uri3 = Uri.parse("android.resource://"+"com.example.testfitmelayouttest"+"/drawable/testimage_slide_2");
Uri uri4 = Uri.parse("android.resource://"+"com.example.testfitmelayouttest"+"/drawable/testimage_slide_3");
recycerlveiw_array array1 = new recycerlveiw_array(uri);
recycerlveiw_array array2 = new recycerlveiw_array(uri2);
recycerlveiw_array array3 = new recycerlveiw_array(uri3);
recycerlveiw_array array4 = new recycerlveiw_array(uri4);
array.add(array1);
array.add(array3);
array.add(array2);
array.add(array4);
final List<String> testStrings = new ArrayList<>();
for(int i = 0; i < 100; i++) {
testStrings.add(i + " 번째 item");
}
RecyclerView recyclerView = view.findViewById(R.id.main_scrollview_recyclerview);
recyclerView.setAdapter(new RecyclerView.Adapter<TestViewHolder>() {
@Override
public TestViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = View.inflate(getActivity(), android.R.layout.simple_list_item_1, null);
return new TestViewHolder(view);
}
@Override
public void onBindViewHolder(TestViewHolder holder, int position) {
holder.textView.setText(testStrings.get(position));
}
public int getItemCount() {
return testStrings.size();
}
});
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
}
public static class TestViewHolder extends RecyclerView.ViewHolder {
public TextView textView;
public TestViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(android.R.id.text1);
}
@Override
public String toString() {
return super.toString();
}
}
}