从Firebase Firestore提取数据时RecyclerView上的动画

时间:2018-10-27 14:12:12

标签: android firebase android-recyclerview google-cloud-firestore android-animation

我正在尝试将已从Firestore数据库中提取的数据(文档集合)作为事件的ArrayList加载到RecylerView中。当它们实际加载时,动画没有显示,并且我收到一条日志,指出 E/RecyclerView: No adapter attached; skipping layout。我不确定这是否是为什么我的动画无法加载的原因。如果有人知道如何纠正我的错误(如果我犯了任何错误),以便将来可能遇到与我相同的问题的任何人都可以在这个问题中找到答案,我将不胜感激。

以下是我的活动:

package com.washedup.anagnosti.ergo.otherHomePossibilities;

import android.content.Context;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;
import com.washedup.anagnosti.ergo.R;
import com.washedup.anagnosti.ergo.eventPerspective.Event;

import java.util.ArrayList;

public class ChooseEventForPerspectiveActivity extends AppCompatActivity {

    private RecyclerView rv;
    private LinearLayoutManager rLayoutManager;
    private FirebaseFirestore  db = FirebaseFirestore.getInstance();
    private CollectionReference eventsRef = db.collection("events");
    private ArrayList<Event> events = new ArrayList<>();
    private FirebaseAuth mAuth = FirebaseAuth.getInstance();
    private FirebaseUser user = mAuth.getCurrentUser();
    private TextView tv;
    private ProgressBar pb;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_event_for_perspective);
        tv = findViewById(R.id.activity_cefp_tv);
        pb = findViewById(R.id.activity_cefp_pb);
        pb.getIndeterminateDrawable().setColorFilter(getResources().getColor(R.color.dirtierWhite), PorterDuff.Mode.MULTIPLY);
        rv = findViewById(R.id.activity_cefp_rv);
        rLayoutManager = new LinearLayoutManager(ChooseEventForPerspectiveActivity.this);
        rv.setLayoutManager(rLayoutManager);
        //refreshEventRV();
        eventsRef.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
            @Override
            public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                pb.setVisibility(View.VISIBLE);
                events.clear();
                tv.setText(R.string.loading_events);
                String userEmail = user.getEmail();
                for(QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots){
                    Event event = documentSnapshot.toObject(Event.class);
                    for(int i=0; i<event.getEmails_of_people().size();i++){
                        if(userEmail!=null){
                            if(event.getEmails_of_people().get(i).compareTo(userEmail)==0){
                                event.setEvent_id(documentSnapshot.getId());
                                events.add(event);
                                break;
                            }
                        }else{
                            Toast.makeText(ChooseEventForPerspectiveActivity.this, "[YERROR]: Email is null", Toast.LENGTH_SHORT).show();
                        }

                    }

                }

                if(events.isEmpty()){
                    pb.setVisibility(View.GONE);
                    tv.setText(R.string.no_events_to_choose_from);
                    tv.setVisibility(View.VISIBLE);
                }else{
                    pb.setVisibility(View.GONE);
                    tv.setVisibility(View.GONE);

                    runAnimation(rv, 0);

                }

            }
        });

                /*eRoleAdapter = new ChooseEventForPerspectiveRecyclerAdapter(events, ChooseEventForPerspectiveActivity.this);
                rv.setAdapter(eRoleAdapter);
                int resId = R.anim.layout_animation_from_right;
                LayoutAnimationController animation = AnimationUtils.loadLayoutAnimation(ChooseEventForPerspectiveActivity.this, resId);
                rv.setLayoutAnimation(animation);*/



    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshEventRV();
    }


    private void refreshEventRV(){

        eventsRef.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
            @Override
            public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                events.clear();
                tv.setText(R.string.loading_events);
                String userEmail = user.getEmail();
                for(QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots){
                    Event event = documentSnapshot.toObject(Event.class);
                    for(int i=0; i<event.getEmails_of_people().size();i++){
                        if(userEmail!=null){
                            if(event.getEmails_of_people().get(i).compareTo(userEmail)==0){
                                  event.setEvent_id(documentSnapshot.getId());
                                events.add(event);
                                break;
                            }
                        }else{
                            Toast.makeText(ChooseEventForPerspectiveActivity.this, "[YERROR]: Email is null", Toast.LENGTH_SHORT).show();
                        }

                    }

                }

                if(events.isEmpty()){
                    tv.setText(R.string.no_events_to_choose_from);
                    tv.setVisibility(View.VISIBLE);
                }else{
                    tv.setVisibility(View.GONE);

                }
            }
        });
    }

    private void runAnimation(RecyclerView rv, int type) {
        Context context = rv.getContext();
        LayoutAnimationController controller = null;

        if(type == 0){
            controller = AnimationUtils.loadLayoutAnimation(context, R.anim.layout_animation_fall_down);
            ChooseEventForPerspectiveRecyclerAdapter eRoleAdapter = new ChooseEventForPerspectiveRecyclerAdapter(events, rv.getContext());
            rv.setAdapter(eRoleAdapter);

            rv.setLayoutAnimation(controller);
            rv.getAdapter().notifyDataSetChanged();
            rv.scheduleLayoutAnimation();
        }

    }
}


package com.washedup.anagnosti.ergo.otherHomePossibilities;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.washedup.anagnosti.ergo.R;
import com.washedup.anagnosti.ergo.eventPerspective.Event;

import java.util.ArrayList;

public class ChooseEventForPerspectiveRecyclerAdapter extends RecyclerView.Adapter<ChooseEventForPerspectiveRecyclerAdapter.ChooseEventForPerspectiveRecyclerViewHolder> {

    private ArrayList<Event> events;
    private Context context;

    //private int lastPosition = -1;

    public ChooseEventForPerspectiveRecyclerAdapter(ArrayList<Event> events, Context context){
        this.events=events;
        this.context=context;
    }

    @Override
    public ChooseEventForPerspectiveRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.child_activity_choose_event_for_perspective, parent,false);
        itemView.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
        return new ChooseEventForPerspectiveRecyclerViewHolder(itemView);
    }



    @Override
    public void onBindViewHolder(ChooseEventForPerspectiveRecyclerViewHolder holder, int position) {

        Event tempEvent = events.get(holder.getAdapterPosition());
        holder.child_activity_cefp_et.setText(tempEvent.getEvent_name());
        //setAnimation(holder.itemView,position);

    }

   /* private void setAnimation(View itemView, int position) {
        if(position > lastPosition){
            Animation animation = AnimationUtils.loadAnimation(context, R.anim.item_animation_from_right);
            itemView.startAnimation(animation);
            lastPosition = position;
        }
    }*/

    /*@Override
    public void onViewDetachedFromWindow(ChooseEventForPerspectiveRecyclerViewHolder holder) {

        (holder).clearAnimation();
    }*/

    @Override
    public int getItemCount() {
        return events.size();
    }

    public class ChooseEventForPerspectiveRecyclerViewHolder extends RecyclerView.ViewHolder {

        CardView child_activity_cefp_cv;
        EditText child_activity_cefp_et;
        ImageView child_activity_cefp_iv;
       // FrameLayout child_activity_cefp_container;

        public ChooseEventForPerspectiveRecyclerViewHolder(View itemView) {
            super(itemView);

            child_activity_cefp_cv = itemView.findViewById(R.id.child_activity_choose_event_for_perspective_cv);
            child_activity_cefp_et = itemView.findViewById(R.id.child_activity_choose_event_for_perspective_et);
            child_activity_cefp_iv = itemView.findViewById(R.id.child_activity_choose_event_for_perspective_iv);
           // child_activity_cefp_container = itemView.findViewById(R.id.child_activity_choose_event_for_perspective_item_layout_container);

        }

       /* public void clearAnimation() {
            child_activity_cefp_container.clearAnimation();
        }*/
    }
}

这些是我的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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background"
    tools:context=".otherHomePossibilities.ChooseEventForPerspectiveActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/activity_cefp_rv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:paddingBottom="1dp" />

        <TextView
        android:id="@+id/activity_cefp_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:fontFamily="@font/roboto_light"
        android:textColor="@color/dirtyWhite"
        android:textSize="30sp"
        android:text="@string/loading_events"/>

    <ProgressBar
        android:id="@+id/activity_cefp_pb"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/activity_cefp_tv"
        android:layout_centerHorizontal="true"
        android:visibility="gone"/>

</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">



        <android.support.v7.widget.CardView
            android:id="@+id/child_activity_choose_event_for_perspective_cv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            app:cardBackgroundColor="@color/colorAccent"
            app:cardCornerRadius="16dp">

            <android.support.constraint.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingBottom="8dp"
                android:paddingEnd="8dp"
                android:paddingStart="8dp"
                android:paddingTop="8dp">

                <ImageView
                    android:id="@+id/child_activity_choose_event_for_perspective_iv"
                    android:layout_width="60dp"
                    android:layout_height="0dp"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="8dp"
                    app:layout_constraintBottom_toBottomOf="@+id/child_activity_choose_event_for_perspective_linlay"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <LinearLayout
                    android:id="@+id/child_activity_choose_event_for_perspective_linlay"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="16dp"
                    android:layout_marginTop="8dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginBottom="8dp"
                    android:orientation="vertical"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toEndOf="@+id/child_activity_choose_event_for_perspective_iv"
                    app:layout_constraintTop_toTopOf="parent">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:fontFamily="@font/roboto_light"
                        android:paddingStart="20dp"
                        android:paddingEnd="0dp"
                        android:text="@string/slider_info_event_name"
                        android:textColor="@color/dirtyWhite"
                        android:textSize="15sp" />

                    <EditText
                        android:id="@+id/child_activity_choose_event_for_perspective_et"
                        android:layout_width="match_parent"
                        android:layout_height="40dp"
                        android:background="@drawable/et_bg"
                        android:focusable="false"
                        android:fontFamily="@font/roboto_light"
                        android:inputType="textCapWords|textAutoCorrect"
                        android:paddingStart="15dp"
                        android:paddingEnd="15dp"
                        android:textColor="@color/dirtyWhite"
                        android:textSize="15sp" />

                </LinearLayout>

            </android.support.constraint.ConstraintLayout>

        </android.support.v7.widget.CardView>



</RelativeLayout>


<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
    android:animation="@anim/item_animation_from_right"
    android:animationOrder="normal"
    android:delay="10%" />


<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@integer/anim_duration_long">

    <translate
        android:fromXDelta="100%p"
        android:interpolator="@android:anim/decelerate_interpolator"
        android:toXDelta="0" />

    <alpha
        android:fromAlpha="0.5"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:toAlpha="1" />

</set>

希望我能提供您所需的一切!

0 个答案:

没有答案