如何在recyclerview中显示加载消息 [英] How to show loading message in recyclerview

查看:49
本文介绍了如何在recyclerview中显示加载消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个回收站视图,我想在回收站填充来自 json 的数据时显示正在加载..."消息或旋转圆圈.这是我的回收站的代码:

 arrayList = new ArrayList<>();适配器 = new ListAdapterGrupat2(this, arrayList, Comanda.class);list.setAdapter(适配器);最终改造改造 = new Retrofit.Builder().baseUrl("http://mysite/").addConverterFactory(GsonConverterFactory.create()).建造();服务 = 改造.创建(API.class);如果(myuser.equals(1")){service.getComenzileMele(user).enqueue(listener);} 别的 {service.getComenziGrupat().enqueue(listener);}}回调<列表<自定义对象>>listener = new Callback>() {@覆盖public void onResponse(Call> call, Response> response) {arrayList.clear();arrayList.addAll(response.body());适配器.notifyDataSetChanged();}@覆盖public void onFailure(Call> call, Throwable t) {Toast.makeText(ComenziGrupatActivity.this, "ERROR", Toast.LENGTH_SHORT).show();}};

<块引用>

这是适配器:

public class ListAdapterGrupat2 extends RecyclerView.Adapter{public ListAdapterGrupat2(Context context, ArrayList arrayList, Class cls) {}@覆盖公共 ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {查看 v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_programarigrupat, parent, false);返回新的 ViewHolder(v);}@覆盖public void onBindViewHolder(ViewHolder holder, int position) {CustomObject obj = arrayList.get(position);holder.item2.setText(obj.getItem2());}

解决方案

添加 ProgressBar 到您的班级和布局(在您的 recicleView 附近).如果你想要文本:添加 LinearLayout 并放置在那里 ProgressBarTextView(触发 linearLayout 的可见性):

进度条;

在发出请求时显示进度条并隐藏 recicleView:

bar.setVisibility(VISIBLE);recicleView.setVisibility(GONE);service.getComenzileMele(user).enqueue(listener);

请求完成时隐藏progressBar并恢复recicleView:

Callback>listener = new Callback>() {@覆盖public void onResponse(Call> call, Response> response) {bar.setVisibility(GONE);recicleView.setVisibility(VISIBLE);}@覆盖public void onFailure(Call> call, Throwable t) {bar.setVisibility(GONE);recicleView.setVisibility(VISIBLE);}};

I have a recyclerview and i want to show "loading... " message or spinning circle while the recycler is populated with data from json. This is the code for my recycler :

 arrayList = new ArrayList<>();      
    adapter = new ListAdapterGrupat2(this, arrayList, Comanda.class);
    list.setAdapter(adapter);
    final Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://mysite/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    service = retrofit.create(API.class);  

    if (myuser.equals("1")) {
        service.getComenzileMele(user).enqueue(listener);

    } else {
        service.getComenziGrupat().enqueue(listener);
    }
}
Callback<List<CustomObject>> listener = new Callback<List<CustomObject>>() {

    @Override
    public void onResponse(Call<List<CustomObject>> call, Response<List<CustomObject>> response) {
        arrayList.clear();
        arrayList.addAll(response.body());
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onFailure(Call<List<CustomObject>> call, Throwable t) {
        Toast.makeText(ComenziGrupatActivity.this, "ERROR", Toast.LENGTH_SHORT).show();           
    }
};

This is the adapter :

public class ListAdapterGrupat2 extends RecyclerView.Adapter<ListAdapterGrupat2.ViewHolder> {   

public ListAdapterGrupat2(Context context, ArrayList<CustomObject> arrayList, Class<?> cls) {        
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_programarigrupat, parent, false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    CustomObject obj = arrayList.get(position);
    holder.item2.setText(obj.getItem2());

}

解决方案

Add ProgressBar to your class and layout (near your recicleView). If you want text: add LinearLayout and place there ProgressBar and TextView (trigger visibility on linearLayout):

ProgressBar bar;

Show progressBar when you're making a request and hide recicleView:

bar.setVisibility(VISIBLE);
recicleView.setVisibility(GONE);     
service.getComenzileMele(user).enqueue(listener);

Hide progressBar when request is finished and restore recicleView:

Callback<List<CustomObject>> listener = new Callback<List<CustomObject>>() {

    @Override
    public void onResponse(Call<List<CustomObject>> call, Response<List<CustomObject>> response) {
        bar.setVisibility(GONE);
        recicleView.setVisibility(VISIBLE);
    }

    @Override
    public void onFailure(Call<List<CustomObject>> call, Throwable t) {
        bar.setVisibility(GONE);
        recicleView.setVisibility(VISIBLE);         
    }
};

这篇关于如何在recyclerview中显示加载消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆