拉动以刷新不使用firebaseRecyclerAdapter

时间:2017-02-05 08:18:58

标签: android firebase firebase-realtime-database firebaseui

我正在尝试使用向下滑动来刷新我的活动。我的活动与Firebase数据库同步,我使用FirebaseRecyclerAdapter从我的firebase帐户获取回收站中的所有数据。但是我的应用程序没有显示任何内容即使滑动动作也不会生成任何动画。

MainAcitivty.java

package com.example.admin.eventadmin;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.squareup.picasso.Picasso;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class MainActivity extends AppCompatActivity {

    private RecyclerView r_event_list;
    private DatabaseReference mDatabaseRef;

    private ProgressDialog progressDialog;
    private SwipeRefreshLayout swipeRefreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        progressDialog = new ProgressDialog(this);

        mDatabaseRef = FirebaseDatabase.getInstance().getReference().child("Event");

        r_event_list = (RecyclerView) findViewById(R.id.request_EventList);
        r_event_list.setHasFixedSize(true);
        r_event_list.setLayoutManager(new LinearLayoutManager(this));

        progressDialog = new ProgressDialog(this);
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeContainer);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
               refreshItems();
            }
        });

    }
    public void refreshItems(){
        loadItems();
    }


    public void loadItems(){

        FirebaseRecyclerAdapter<Event, RequestViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Event, RequestViewHolder>(
                Event.class,
                R.layout.event_list_row,
                RequestViewHolder.class,
                mDatabaseRef
        ) {
            @Override
            protected void populateViewHolder(RequestViewHolder viewHolder, Event model, int position) {

                viewHolder.setTitle(model.getTitle());
                viewHolder.setDesc(model.getDesc());
                viewHolder.setCategory(model.getCategory());
                viewHolder.setLocation(model.getLocation());
                viewHolder.setPrice(model.getPrice());
                viewHolder.setImageUrl(getApplicationContext(), model.getImageUrl());

                swipeRefreshLayout.setRefreshing(false);

                final String title = model.getTitle();
                final String desc = model.getDesc();
                final String image = model.getImageUrl();
                final String category = model.getCategory();
                final String price = model.getPrice();
                final String location = model.getLocation();

                final DatabaseReference aRef = FirebaseDatabase.getInstance().getReference();

                viewHolder.verify_button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        progressDialog.setMessage("Approving Event");
                        progressDialog.show();
                        String key = aRef.child("Approved Events").push().getKey();
                        Event event = new Event(title, image, price, category, location, desc);

                        Map<String, Object> eventValues = event.toMap();

                        Map<String, Object> childUpdates = new HashMap<>();

                        childUpdates.put("/ApprovedEvents/" + key, eventValues);

                        aRef.updateChildren(childUpdates);
                        progressDialog.dismiss();
                    }
                });
            }
        };
        r_event_list.setAdapter(firebaseRecyclerAdapter);
    }

    public static class RequestViewHolder extends RecyclerView.ViewHolder {
        View mView;
        public Button verify_button;


        public RequestViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
            verify_button = (Button) mView.findViewById(R.id.approve_button);

        }

        public void setTitle(String title) {
            TextView request_title = (TextView) mView.findViewById(R.id.request_title);
            request_title.setText(title);
        }

        public void setDesc(String desc) {
            TextView request_desc = (TextView) mView.findViewById(R.id.request_desc);
            request_desc.setText(desc);
        }

        public void setLocation(String location) {
            TextView request_desc = (TextView) mView.findViewById(R.id.request_location);
            request_desc.setText(location);
        }

        public void setCategory(String category) {
            TextView request_category = (TextView) mView.findViewById(R.id.request_category);
            request_category.setText(category);
        }
        public void setPrice(String price) {
            TextView request_price = (TextView) mView.findViewById(R.id.request_price);
            request_price.setText(price);
        }

        public void setImageUrl(Context ctx, String imageUrl) {
            ImageView request_image = (ImageView) mView.findViewById(R.id.request_image);
            Picasso.with(ctx).load(imageUrl).into(request_image);
        }
    }
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.admin.eventadmin.MainActivity">
<android.support.v4.widget.SwipeRefreshLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/swipeContainer">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/request_EventList"
        android:clickable="true"/>

</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>

3 个答案:

答案 0 :(得分:6)

FirebaseRecyclerAdapter永远不需要&#34;拉动刷新&#34;操作。它始终与数据库中的数据同步。如果数据库中发生了某些变化,只要您有网络连接,它就会自动反映在适配器中。

您应该假设FirebaseRecyclerAdapter始终显示最新数据,并且在使用时不要轻易刷新。

答案 1 :(得分:0)

我会看看这个页面 - &gt; https://developer.android.com/training/swipe/respond-refresh-request.html

我认为这是相关的代码行,您需要在代码中实现。

// Signal SwipeRefreshLayout to start the progress indicator
mySwipeRefreshLayout.setRefreshing(true);
// Start the refresh background task.
// This method calls setRefreshing(false) when it's finished.
myUpdateOperation();

答案 2 :(得分:0)

也许您可以使用 yourAdapter.notifyDataSetChanged();

解决此问题