在我的主要活动中,我有一个 RecyclerView。假设我单击一个名为“Back to 80s”的项目,它会打开一个相应的片段(占据整个窗口)。但是,存在一个问题:当我单击片段内的任何空白区域时,RecyclerView 会短暂可见。此外,如果我单击当前片段后面的 RecyclerView 中存在项目的空白区域,则该特定片段会直接加载。
预期行为:
实际行为: 在当前片段上按下空格后,RecyclerView 会立即出现,然后加载与触摸的项目对应的片段。
这个问题在每个片段中都会出现,而不仅仅是“回到80年代”片段。
碎片
<RelativeLayout 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"
android:background="#e7e7e7"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
tools:context=".Buttons.backto80s_btn">
<LinearLayout
android:id="@+id/addtofavlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:orientation="vertical"
android:layout_margin="20dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/favorite" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Add to Favorite"/>
</LinearLayout>
<RelativeLayout
android:id="@+id/sucess_backto80s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_centerInParent="true"
android:layout_marginStart="20dp"
android:background="@drawable/border">
<LinearLayout
android:id="@+id/sucessScreen_backto80s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="20dp"
android:background="#216f45"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fontFamily="@font/calibriregular"
android:text="Blast from the Past"
android:textColor="@color/white"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fontFamily="@font/calibriregular"
android:text="80s Unlocked — iconic tunes, bold fashion, and classic video games. Rediscover the decade that made history cool."
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="20sp"
android:layout_marginTop="5dp"
android:textStyle="bold" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/continueBtn_backto80s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:backgroundTint="#cccccc"
android:fontFamily="@font/calibriregular"
android:text="Continue"
android:textAlignment="center"
android:textAllCaps="false"
android:textColor="#505050"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/layoutProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/backto80sClick"
android:layout_centerHorizontal="true"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"
android:layout_marginBottom="10dp"
android:visibility="gone"
android:background="@drawable/border">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#858386">
<TextView
android:layout_marginTop="10dp"
android:fontFamily="@font/calibriregular"
android:textAlignment="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Bringing back neon lighting....."/>
<ProgressBar
android:id="@+id/progressbar_backto80s"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:padding="30dp"
android:paddingBottom="20dp"
android:progressDrawable="@drawable/progressbar" />
</RelativeLayout>
</RelativeLayout>
<pl.droidsonroids.gif.GifImageView
android:id="@+id/backto80sGifSucess"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/sucess_backto80s"
android:scaleType="fitCenter"
android:layout_marginBottom="10dp"
android:layout_centerInParent="true"
android:layout_below="@+id/addtofavlayout"
android:src="@drawable/backto80sgifsucess"
android:visibility="gone" />
<ImageView
android:id="@+id/backto80sClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/backto80s" />
</RelativeLayout>
公共类 backto80s_btn 扩展 Fragment {
ImageView backto80sBtn;
ProgressBar progressBar;
boolean isPressed = false;
Handler handler = new Handler();
RelativeLayout layoutProgress, layoutSucess_backto80s;
AppCompatButton continueBtn;
MediaPlayer mp_success, mp;
private GifImageView backto80sGifSucess;
@SuppressLint("MissingInflatedId")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_backto80s_btn, container, false);
requireActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = requireActivity().getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.parseColor("#e7e7e7"));
}
backto80sGifSucess = view.findViewById(R.id.backto80sGifSucess);
backto80sGifSucess.setVisibility(View.GONE);
backto80sBtn = view.findViewById(R.id.backto80sClick);
progressBar = view.findViewById(R.id.progressbar_backto80s);
layoutProgress = view.findViewById(R.id.layoutProgress);
layoutProgress.setVisibility(View.GONE);
mp = MediaPlayer.create(getContext(), R.raw.mouseclick);
// Move MediaPlayer initialization here
mp_success = MediaPlayer.create(requireContext(), R.raw.sucess);
continueBtn = view.findViewById(R.id.continueBtn_backto80s);
layoutSucess_backto80s = view.findViewById(R.id.sucess_backto80s);
layoutSucess_backto80s.setVisibility(View.GONE);
backto80sBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mp = MediaPlayer.create(getContext(), R.raw.mouseclick);
mp_success = MediaPlayer.create(requireContext(), R.raw.sucess);
backto80sBtn.setEnabled(false);
// Toggle between pressed and unpressed images
isPressed = !isPressed;
mp.start();
if (isPressed) {
backto80sBtn.setImageResource(R.drawable.backto80spressed);
// Delayed switch back to unpressed image after 500 milliseconds
handler.postDelayed(new Runnable() {
@Override
public void run() {
backto80sBtn.setImageResource(R.drawable.backto80s);
layoutProgress.setVisibility(View.VISIBLE);
sucess(1);
progressBar.setMax(100);
}
}, 500);
} else {
backto80sBtn.setImageResource(R.drawable.backto80s);
}
}
});
continueBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
requireActivity().getSupportFragmentManager().popBackStack();
FragmentTransaction transaction = requireActivity().getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out,android.R.anim.fade_in, android.R.anim.fade_out);
transaction.replace(R.id.fragment_container, new backto80s_btn());
transaction.addToBackStack(null);
transaction.commit();
}
});
return view;
}
private void sucess(final float progress) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
progressBar.setProgress((int) progress);
sucess(progress + 0.5f); // Increase progress smoothly
if (progress == 100) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (mp_success != null) {
mp_success.start();
}
layoutProgress.setVisibility(View.GONE);
backto80sBtn.setVisibility(View.GONE);
layoutSucess_backto80s.setVisibility(View.VISIBLE);
backto80sGifSucess.setImageResource(R.drawable.backto80sgifsucess);
backto80sGifSucess.setVisibility(View.VISIBLE);
}
}, 100); // Delay the call to onProgressReached90
}
}
}, 20); // Adjust the delay as needed for smoother animation
}
@Override
public void onStop() {
super.onStop();
((MainActivity) requireActivity()).showToolbar();
}
@Override
public void onResume() {
super.onResume();
((MainActivity) requireActivity()).hideToolbar();
}
@Override
public void onPause() {
super.onPause();
releaseMediaPlayers();
}
private void releaseMediaPlayers() {
if (mp != null) {
mp.release();
mp = null;
}
if (mp_success!=null){
mp_success.release();
mp_success = null;
}
}
}
主要活动
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
BtnRecyclerViewAdapter btnRecyclerViewAdapter;
int[] arr = {R.drawable.image1, R.drawable.escratrace, R.drawable.delete, R.drawable.alttablife, R.drawable.givemespace,R.drawable.previewlife,R.drawable.pauselife,R.drawable.undo,R.drawable.gobacktochildhood,R.drawable.backto80s};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setSupportActionBar(findViewById(R.id.appToolbar));
getSupportActionBar().setTitle("");
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.parseColor("#e7e7e7"));
}
recyclerView = findViewById(R.id.btnRecyclerView);
layoutManager = new GridLayoutManager(this, 1);
recyclerView.setLayoutManager(layoutManager);
btnRecyclerViewAdapter = new BtnRecyclerViewAdapter(arr, getSupportFragmentManager(),this);
recyclerView.setAdapter(btnRecyclerViewAdapter);
}
public void showToolbar() {
getSupportActionBar().show();
}
public void hideToolbar() {
getSupportActionBar().hide();
}
}
预期行为,因为您有淡入淡出动画过渡。删除它或使用其他过渡