在显示 recyclerView 之前不会出现进度条
Progress Bar does not appear until recyclerView is displayed
我正在为 android 开发活动和遗产应用程序。
我有个问题。
当我单击类别时,会出现项目列表。但是在从数据库中获取数据时,progressBar 没有出现。
有人可以帮助我吗?
我的代码:
Item_Layout_Activity.java
public class Item_Layout_Activity extends Fragment {
RecyclerView recyclerView;
String hash,email,tipo_login="";
ArrayList<Hotel> array_hotel;
ArrayList<Patrimony> array,array_temp;
double latitudeUser,longitudeUser;
String tipo_categoria;
ProgressBar progressBar_app;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_list_others, container, false);
recyclerView = (RecyclerView) root.findViewById(R.id.rv);
progressBar_app=root.findViewById(R.id.progressBar_app);
progressBar_app.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.GONE);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
RecyclerView.LayoutManager rvLiLayoutManager = layoutManager;
recyclerView.setLayoutManager(rvLiLayoutManager);
tipo_categoria= DrawerActivity.categoria_tab_outros;
array=new ArrayList<>();
array_temp=new ArrayList<>();
if (tipo_categoria != null) {
switch (tipo_categoria) {
case "Hotel":
// will show the list of hotels
if(Controller.array_items_hotels!=null ) {
if (array_hotel != null)
array_hotel.clear();
if(array_hotel==null)
array_hotel=new ArrayList<>();
for (Hotel h: Controller.array_items_hotels) {
if (h.getLocalidade().equals(Controller.localidade)) {
array_hotel.add(h);
}
}
}
recyclerView.setVisibility(View.VISIBLE);
Adapter_Item adapter_item = new Adapter_Item(getContext(), array_hotel, "hotel");
recyclerView.setAdapter(adapter_item);
}// if the array does not yet exist then fetch the hotels a database
else
{
boolean res=Controller.VerificarConetividade();
if(res)
{
// search list of hotels at BD
buscarHoteisBD();
} else
Toast.makeText(getContext(), R.string.alerta_conexao_rede,Toast.LENGTH_LONG).show();
}
break;
}
}
// get list of hotels from the database
private void buscarHoteisBD(){
Retrofit.Builder builder = new Retrofit.Builder().baseUrl(WebAPI.BASE_URL).addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
WebAPI webApi = retrofit.create(WebAPI.class);
Call<ArrayList<Hotel>> call = webApi.get_Hotels(email_encrypt,tipo_login,hash,linguagem);
call.enqueue(new Callback<ArrayList<Hotel>>() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onResponse(Call<ArrayList<Hotel>> call, Response<ArrayList<Hotel>> response) {
array_hotel = response.body();
if (array_hotel == null) {
Toast.makeText(getContext(), R.string.alerta_falha_BD, Toast.LENGTH_SHORT).show();
} else {
Controller.array_items_hotels.addAll(array_hotel);
array_hotel.clear();
for (Hotel p:Controller.array_items_hotels) {
array_hotel.add(p);
}
}
}
recyclerView.setVisibility(View.VISIBLE);
Adapter_Item adapter_item = new Adapter_Item(getContext() ,array_hotel,"hotel");
recyclerView.setAdapter(adapter_item);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onFailure(Call<ArrayList<Hotel>> call, Throwable t) {
Toast.makeText(getContext(), R.string.alerta_falha_BD, Toast.LENGTH_SHORT).show();
}
});
}
}
fragment_list_others.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/fotolayout">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:id="@+id/progressBar_app"
style="?android:attr/progressBarStyle"
android:layout_width="50sp"
android:layout_height="50sp"
android:layout_gravity="center"
android:indeterminateDrawable="@drawable/circularprogress"
android:visibility="gone" />
</FrameLayout>
[![列表项][1]][1]
[1]: https://i.stack.imgur.com/DRJEU.jpg
方法 onCreateView 应该 return 一个视图,其中应该包含所有项目和一个您试图使其可见的进度条。
但是由于您同步添加了所有项目,在主 (UI) 线程中,方法将 return 只有在添加所有项目时才会显示视图(连同进度条)。
我会尝试做以下事情:
- 删除行“progressBar_app.setVisibility(View.VISIBLE);”
- 添加行“progressBar_app.setVisibility(View.GONE);”在 return 从方法 onCreateView
获取视图之前
- 使进度条默认可见(android:visibility="visible")-添加所有项目后它会隐藏。
我正在为 android 开发活动和遗产应用程序。 我有个问题。 当我单击类别时,会出现项目列表。但是在从数据库中获取数据时,progressBar 没有出现。
有人可以帮助我吗?
我的代码:
Item_Layout_Activity.java
public class Item_Layout_Activity extends Fragment {
RecyclerView recyclerView;
String hash,email,tipo_login="";
ArrayList<Hotel> array_hotel;
ArrayList<Patrimony> array,array_temp;
double latitudeUser,longitudeUser;
String tipo_categoria;
ProgressBar progressBar_app;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_list_others, container, false);
recyclerView = (RecyclerView) root.findViewById(R.id.rv);
progressBar_app=root.findViewById(R.id.progressBar_app);
progressBar_app.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.GONE);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
RecyclerView.LayoutManager rvLiLayoutManager = layoutManager;
recyclerView.setLayoutManager(rvLiLayoutManager);
tipo_categoria= DrawerActivity.categoria_tab_outros;
array=new ArrayList<>();
array_temp=new ArrayList<>();
if (tipo_categoria != null) {
switch (tipo_categoria) {
case "Hotel":
// will show the list of hotels
if(Controller.array_items_hotels!=null ) {
if (array_hotel != null)
array_hotel.clear();
if(array_hotel==null)
array_hotel=new ArrayList<>();
for (Hotel h: Controller.array_items_hotels) {
if (h.getLocalidade().equals(Controller.localidade)) {
array_hotel.add(h);
}
}
}
recyclerView.setVisibility(View.VISIBLE);
Adapter_Item adapter_item = new Adapter_Item(getContext(), array_hotel, "hotel");
recyclerView.setAdapter(adapter_item);
}// if the array does not yet exist then fetch the hotels a database
else
{
boolean res=Controller.VerificarConetividade();
if(res)
{
// search list of hotels at BD
buscarHoteisBD();
} else
Toast.makeText(getContext(), R.string.alerta_conexao_rede,Toast.LENGTH_LONG).show();
}
break;
}
}
// get list of hotels from the database
private void buscarHoteisBD(){
Retrofit.Builder builder = new Retrofit.Builder().baseUrl(WebAPI.BASE_URL).addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
WebAPI webApi = retrofit.create(WebAPI.class);
Call<ArrayList<Hotel>> call = webApi.get_Hotels(email_encrypt,tipo_login,hash,linguagem);
call.enqueue(new Callback<ArrayList<Hotel>>() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onResponse(Call<ArrayList<Hotel>> call, Response<ArrayList<Hotel>> response) {
array_hotel = response.body();
if (array_hotel == null) {
Toast.makeText(getContext(), R.string.alerta_falha_BD, Toast.LENGTH_SHORT).show();
} else {
Controller.array_items_hotels.addAll(array_hotel);
array_hotel.clear();
for (Hotel p:Controller.array_items_hotels) {
array_hotel.add(p);
}
}
}
recyclerView.setVisibility(View.VISIBLE);
Adapter_Item adapter_item = new Adapter_Item(getContext() ,array_hotel,"hotel");
recyclerView.setAdapter(adapter_item);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onFailure(Call<ArrayList<Hotel>> call, Throwable t) {
Toast.makeText(getContext(), R.string.alerta_falha_BD, Toast.LENGTH_SHORT).show();
}
});
}
}
fragment_list_others.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/fotolayout">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:id="@+id/progressBar_app"
style="?android:attr/progressBarStyle"
android:layout_width="50sp"
android:layout_height="50sp"
android:layout_gravity="center"
android:indeterminateDrawable="@drawable/circularprogress"
android:visibility="gone" />
</FrameLayout>
[![列表项][1]][1] [1]: https://i.stack.imgur.com/DRJEU.jpg
方法 onCreateView 应该 return 一个视图,其中应该包含所有项目和一个您试图使其可见的进度条。 但是由于您同步添加了所有项目,在主 (UI) 线程中,方法将 return 只有在添加所有项目时才会显示视图(连同进度条)。
我会尝试做以下事情:
- 删除行“progressBar_app.setVisibility(View.VISIBLE);”
- 添加行“progressBar_app.setVisibility(View.GONE);”在 return 从方法 onCreateView 获取视图之前
- 使进度条默认可见(android:visibility="visible")-添加所有项目后它会隐藏。