在将数据加载到Recycler视图之前加载视图

时间:2016-02-19 13:20:48

标签: android firebase android-recyclerview loadview

我想显示一个加载视图,即一个不确定的进度条,直到我使用从Firebase获取的数据加载我的回收站视图。我需要知道在Recycler View加载数据后会立即触发的事件。

我扩展了这个类并用作适配器

https://github.com/mmazzarolo/firebase-recyclerview/blob/master/app/src/main/java/com/example/matteo/firebase_recycleview/FirebaseRecyclerAdapter.java

我的扩展课程是

public class TrendingRecycler extends FirebaseRecyclerAdapter
    <TrendingRecycler.ViewHolder, DataStuff> {


public static class ViewHolder extends RecyclerView.ViewHolder {

    TextView Data;


    public ViewHolder(View view) {
        super(view);

        Data= (TextView) view.findViewById(R.id.text_card_trending);
    }
}

public TrendingRecycler(Query query, Class<DataStuff> itemClass) {
    super(query, itemClass);
}


@Override public TrendingRecycler.ViewHolder
onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.trending_card_view, parent, false);

    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(TrendingRecycler.ViewHolder holder,
                                       int position) {


    final DataStuff item = getItem(position);
    holder.Data.setText(item.Data);
    final Context context =holder.itemView.getContext();

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Toast.makeText(MyApplication.getAppContext(),
                    item.Tags.toString(), Toast.LENGTH_LONG).show();
            TempShowStore.setDataStuff(item);
            context.startActivity(
                    new Intent(context, ShowComplete.class)
            );
        }
    });
}

@Override
protected void itemAdded(DataStuff item, String key, int position) {
    Log.d("ADDED",String.valueOf(position));
}

@Override
protected void itemChanged(DataStuff oldItem, DataStuff newItem, String key, int position) {
    Log.d("CHANGED",String.valueOf(position));
}

@Override
protected void itemRemoved(DataStuff item, String key, int position) {
    Log.d("REMOVED",String.valueOf(position));
}

@Override
protected void itemMoved(DataStuff item, String key, int oldPosition, int newPosition) {
    Log.d("MOVED",String.valueOf(oldPosition) + " to " +String.valueOf(newPosition));
}

}

3 个答案:

答案 0 :(得分:5)

您必须尝试覆盖适配器中的onDataChanged():

mProgressDialog.show();
mFirebaseAdapter = new FirebaseRecyclerAdapter(...) {

@Override
protected void populateViewHolder(...) {...}

@Override
protected void onDataChanged() {
    if (mProgressDialog != null && mProgressDialog.isShowing()) {
        mProgressDialog.dismiss();
    }
  }

};

答案 1 :(得分:1)

AdapterdataObserver注册到RecyclerView.Adapter

protected RecyclerView.AdapterDataObserver observer= new RecyclerView.AdapterDataObserver() {
    @Override
    public void onChanged() {
        //called When the data in the adapter is changed
    }
};

yourAdapter.registerAdapterDataObserver(observer);

答案 2 :(得分:0)

我发现onItemRangeInserted方法有效。我用它来检测第一个项目何时被添加到RecyclerView

private void setupProgress() {
    vProgress.show();
    mAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {

        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
            super.onItemRangeInserted(positionStart, itemCount);
            vProgress.hide();
            mAdapter.unregisterAdapterDataObserver(this);
        }
    });
}