Java 在显示recyclerView之前,不会显示进度条
我正在为android开发一个事件和遗产应用程序。 我有个问题。 单击类别时,将显示项目列表。但是,当从数据库中提取数据时,progressBar不会出现 有人能帮我吗 我的代码: Item_Layout_Activity.javaJava 在显示recyclerView之前,不会显示进度条,java,android,android-recyclerview,progress-bar,Java,Android,Android Recyclerview,Progress Bar,我正在为android开发一个事件和遗产应用程序。 我有个问题。 单击类别时,将显示项目列表。但是,当从数据库中提取数据时,progressBar不会出现 有人能帮我吗 我的代码: Item_Layout_Activity.java 公共类项目\u布局\u活动扩展片段{ 回收视图回收视图; 字符串哈希,电子邮件,tipo_login=“”; ArrayList array_酒店; ArrayList数组,数组_temp; 双纬度,长纬度; 字符串tipo_categoria; ProgressB
公共类项目\u布局\u活动扩展片段{
回收视图回收视图;
字符串哈希,电子邮件,tipo_login=“”;
ArrayList array_酒店;
ArrayList数组,数组_temp;
双纬度,长纬度;
字符串tipo_categoria;
ProgressBar ProgressBar_应用程序;
@凌驾
创建视图时的公共视图(@NonNull LayoutInflater inflater、ViewGroup容器、Bundle savedInstanceState){
视图根=充气机。充气(R.layout.fragment\u list\u others,container,false);
recyclerView=(recyclerView)root.findViewById(R.id.rv);
progressBar\u app=root.findViewById(R.id.progressBar\u app);
progressBar_应用程序设置可见性(View.VISIBLE);
recyclerView.setVisibility(View.GONE);
LinearLayoutManager布局管理器=新的LinearLayoutManager(getActivity());
RecyclerView.LayoutManager rvLiLayoutManager=LayoutManager;
recyclerView.setLayoutManager(rvLiLayoutManager);
tipo_categoria=抽屉实用性。categoria_tab_outros;
数组=新的ArrayList();
array_temp=new ArrayList();
if(tipo_categoria!=null){
开关(tipo_分类){
案例“酒店”:
//将显示酒店列表
if(Controller.array\u items\u hotels!=null){
if(array_hotel!=null)
array_hotel.clear();
if(array_hotel==null)
array_hotel=新ArrayList();
用于(酒店h:控制器。阵列\u项目\u酒店){
if(h.getLocalidade().equals(Controller.localidade)){
array_hotel.add(h);
}
}
}
recyclerView.setVisibility(View.VISIBLE);
Adapter_Item Adapter_Item=新适配器_Item(getContext(),数组_hotel,“hotel”);
recyclerView.setAdapter(适配器项);
}//如果该阵列尚不存在,则从数据库中获取该阵列
其他的
{
boolean res=Controller.verifycarconetividade();
如果(res)
{
//屋宇署酒店一览表
buscarHoteisBD();
}否则
Toast.makeText(getContext(),R.string.alerta_conexao_rede,Toast.LENGTH_LONG).show();
}
打破
}
}
//从数据库中获取酒店列表
私有void buscarHoteisBD(){
reformation.Builder=new reformation.Builder().baseUrl(WebAPI.BASE_URL).addConverterFactory(GsonConverterFactory.create());
改装改装=builder.build();
WebAPI-WebAPI=reformation.create(WebAPI.class);
Call Call=webApi.get_Hotels(电子邮件加密、tipo登录、哈希、语言管理);
call.enqueue(新回调(){
@RequiresApi(api=Build.VERSION\u code.LOLLIPOP)
@凌驾
公共void onResponse(调用、响应){
array_hotel=response.body();
if(array_hotel==null){
Toast.makeText(getContext(),R.string.alerta_falha_BD,Toast.LENGTH_SHORT).show();
}否则{
控制器.array\u items\u hotels.addAll(array\u hotel);
array_hotel.clear();
用于(酒店p:Controller.array\u items\u hotels){
array_hotel.add(p);
}
}
}
recyclerView.setVisibility(View.VISIBLE);
Adapter_Item Adapter_Item=新适配器_Item(getContext(),数组_hotel,“hotel”);
recyclerView.setAdapter(适配器项);
}
@RequiresApi(api=Build.VERSION\u code.LOLLIPOP)
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Toast.makeText(getContext(),R.string.alerta_falha_BD,Toast.LENGTH_SHORT).show();
}
});
}
}
fragment_list_others.xml
[![列表项目][1][1]
[1]: https://i.stack.imgur.com/DRJEU.jpg方法onCreateView应返回一个视图,该视图应包含您试图使其可见的所有项目和进度条。 但由于您是同步添加所有项目的,所以在主(UI)线程中,方法将仅在添加所有项目时返回视图(连同进度条) 我会尽量做到以下几点:
- 删除行“progressBar_app.setVisibility(View.VISIBLE);”
- 在从方法onCreateView返回视图之前,添加行“progressBar_app.setVisibility(View.GONE);”
- 默认情况下使进度条可见(android:visibility=“visible”)-添加所有项目后,进度条将隐藏
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();
}
});
}
}
<?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>