gpt4 book ai didi

java - 微光动画在加载 RecyclerView 时不会停止

转载 作者:行者123 更新时间:2023-12-04 12:16:40 24 4
gpt4 key购买 nike

我有一个从 firebase 获取数据的 Recycler View ,我想要做的是,当所有数据都被加载并且一旦 recycler View 被填充,那么微光动画应该停止并消失。我已经尝试过使用进度条进行相同的操作,并且没有任何问题。但如果是微光动画,它不会停止。我创建了两种方法,一种是启动动画,另一种是停止动画。请帮助我编写代码并建议我需要更改哪些内容才能达到预期的结果。我在 onBindViewHolder 中调用 stopLoading 函数。提前致谢。

public class buynow extends AppCompatActivity {

private RecyclerView recyclerView;
private DatabaseReference ProductRef;
private FirebaseRecyclerAdapter<Products, UsersViewHolder> firebaseRecyclerAdapter;
private String searchText = "";

public void showLoading(){
ShimmerFrameLayout shimmerFrameLayout = findViewById(R.id.shimmer_view);
shimmerFrameLayout.startShimmerAnimation();
}

public void stopLoading(){
ShimmerFrameLayout shimmerFrameLayout = findViewById(R.id.shimmer_view);
shimmerFrameLayout.setVisibility(View.GONE);
}


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buynow);
Firebase.setAndroidContext(buynow.this);

Bundle extras = getIntent().getExtras();
if(extras != null){
searchText = extras.getString("phone_name");
}

setActionBarTitle();

showLoading();

Toast.makeText(buynow.this,"Loading",Toast.LENGTH_SHORT).show();

ProductRef = FirebaseDatabase.getInstance().getReference().child("Products");

recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));

firebaseUserSearch(searchText);
}

private void firebaseUserSearch(String searchText){

Query firebaseSearchQuery = ProductRef.orderByChild("pname").startAt(searchText).endAt(searchText + "\uf8ff");

FirebaseRecyclerOptions<Products> options = new FirebaseRecyclerOptions.Builder<Products>()
.setQuery(firebaseSearchQuery,Products.class).build();

firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Products, UsersViewHolder>(options) {


@Override
protected void onBindViewHolder(@NonNull UsersViewHolder holder, int position, @NonNull Products model) {

holder.setDetails(getApplicationContext(), model.getPname(), model.getPprice(), model.getPmrp(), model.getPcondition(), model.getPimage());
stopLoading();
}

@NonNull
@Override
public UsersViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.buy_now_phones,parent,false);
return new UsersViewHolder(view);

}
};

recyclerView.setAdapter(firebaseRecyclerAdapter);
firebaseRecyclerAdapter.startListening();

}

@Override
protected void onStart() {
super.onStart();
firebaseRecyclerAdapter.startListening();
}

//View Holder Class
public static class UsersViewHolder extends RecyclerView.ViewHolder{

View mView;

private UsersViewHolder(@NonNull View itemView) {
super(itemView);
mView = itemView;
}

private void setDetails(Context context, String phoneName, String phonePrice, String phoneMrp, String phoneCondition, String phoneImage){

TextView phone_name = (TextView) mView.findViewById(R.id.product_name);
TextView phone_price = (TextView) mView.findViewById(R.id.product_price);
TextView phone_mrp = (TextView) mView.findViewById(R.id.product_mrp);
TextView phone_condition = (TextView) mView.findViewById(R.id.product_condition);
ImageView phone_image = (ImageView)mView.findViewById(R.id.product_image);

phone_name.setText(phoneName);

//fromatting phone price
double amount = Double.parseDouble(phonePrice);
DecimalFormat formatter = new DecimalFormat("#,###.00");
phone_price.setText("₹"+formatter.format(amount));

//fromatting phone mrp
double amount0 = Double.parseDouble(phoneMrp);
DecimalFormat formatter0 = new DecimalFormat("#,###.00");
phone_mrp.setText("MRP "+formatter.format(amount0));
phone_mrp.setPaintFlags(phone_mrp.getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG);
phone_condition.setText(phoneCondition);
Picasso.with(context).load(phoneImage).into(phone_image);

}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.action_bar,menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_search) {
Intent intent = new Intent(buynow.this,buynow_device_search.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}

public void setActionBarTitle() {
getSupportActionBar().setTitle("Used Phones");
}

}

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"
tools:context=".buynow">

<com.facebook.shimmer.ShimmerFrameLayout
android:id="@+id/shimmer_view"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<include
layout="@layout/place_holder_layout"/>

<include
layout="@layout/place_holder_layout"/>

<include
layout="@layout/place_holder_layout"/>

<include
layout="@layout/place_holder_layout"/>

<include
layout="@layout/place_holder_layout"/>

<include
layout="@layout/place_holder_layout"/>

</LinearLayout>

</com.facebook.shimmer.ShimmerFrameLayout>


<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent">

</androidx.recyclerview.widget.RecyclerView>

</LinearLayout>

最佳答案

先声明 ShimmerFrameLayout 全局范围内。

private ShimmerFrameLayout shimmerLayout;

然后声明 onCreate() 部分
 shimmerLayout      =findViewById(R.id.shimmer_view);

对于展示案例,
  shimmerLayout.setVisibility(View.VISIBLE);
shimmerLayout.startShimmer();

对于停止情况,
 if(shimmerLayout.isShimmerVisible())
{
shimmerLayout.stopShimmer();
shimmerLayout.setVisibility(View.GONE);
}

代码编辑

尝试
public class buynow extends AppCompatActivity {

private RecyclerView recyclerView;
private DatabaseReference ProductRef;
private FirebaseRecyclerAdapter<Products, UsersViewHolder> firebaseRecyclerAdapter;
private String searchText = "";
ShimmerFrameLayout shimmerFrameLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buynow);
Firebase.setAndroidContext(buynow.this);

Bundle extras = getIntent().getExtras();
if(extras != null){
searchText = extras.getString("phone_name");
}

setActionBarTitle();

ProductRef = FirebaseDatabase.getInstance().getReference().child("Products");

recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));

shimmerFrameLayout = findViewById(R.id.shimmer_view);
shimmerFrameLayout.setVisibility(View.VISIBLE);
shimmerFrameLayout.startShimmer();
recyclerView.setVisibility(View.INVISIBLE);


Toast.makeText(buynow.this,"Loading",Toast.LENGTH_SHORT).show();


firebaseUserSearch(searchText);
}

private void firebaseUserSearch(String searchText){

Query firebaseSearchQuery = ProductRef.orderByChild("pname").startAt(searchText).endAt(searchText + "\uf8ff");

FirebaseRecyclerOptions<Products> options = new FirebaseRecyclerOptions.Builder<Products>()
.setQuery(firebaseSearchQuery,Products.class).build();

firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Products, UsersViewHolder>(options) {


@Override
protected void onBindViewHolder(@NonNull UsersViewHolder holder, int position, @NonNull Products model) {

holder.setDetails(getApplicationContext(), model.getPname(), model.getPprice(), model.getPmrp(), model.getPcondition(), model.getPimage());

}

@NonNull
@Override
public UsersViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.buy_now_phones,parent,false);
return new UsersViewHolder(view);

}
};

shimmerFrameLayout.setVisibility(View.INVISIBLE);
shimmerFrameLayout.stopShimmer();
recyclerView.setVisibility(View.VISIBLE);
recyclerView.setAdapter(firebaseRecyclerAdapter);
firebaseRecyclerAdapter.startListening();

}

@Override
protected void onStart() {
super.onStart();
firebaseRecyclerAdapter.startListening();
}

//View Holder Class
public static class UsersViewHolder extends RecyclerView.ViewHolder{

View mView;

private UsersViewHolder(@NonNull View itemView) {
super(itemView);
mView = itemView;
}

private void setDetails(Context context, String phoneName, String phonePrice, String phoneMrp, String phoneCondition, String phoneImage){

TextView phone_name = (TextView) mView.findViewById(R.id.product_name);
TextView phone_price = (TextView) mView.findViewById(R.id.product_price);
TextView phone_mrp = (TextView) mView.findViewById(R.id.product_mrp);
TextView phone_condition = (TextView) mView.findViewById(R.id.product_condition);
ImageView phone_image = (ImageView)mView.findViewById(R.id.product_image);

phone_name.setText(phoneName);

//fromatting phone price
double amount = Double.parseDouble(phonePrice);
DecimalFormat formatter = new DecimalFormat("#,###.00");
phone_price.setText("₹"+formatter.format(amount));

//fromatting phone mrp
double amount0 = Double.parseDouble(phoneMrp);
DecimalFormat formatter0 = new DecimalFormat("#,###.00");
phone_mrp.setText("MRP "+formatter.format(amount0));
phone_mrp.setPaintFlags(phone_mrp.getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG);
phone_condition.setText(phoneCondition);
Picasso.with(context).load(phoneImage).into(phone_image);

}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.action_bar,menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_search) {
Intent intent = new Intent(buynow.this,buynow_device_search.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}

public void setActionBarTitle() {
getSupportActionBar().setTitle("Used Phones");
}

}

关于java - 微光动画在加载 RecyclerView 时不会停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61344387/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com