Android Studio RecyclerView滞后滚动

时间:2019-03-04 14:54:11

标签: android-recyclerview

有人可以帮助我找到为什么recyclerview的滚动条太慢?在xml文件中,我添加了android:nestedScrollingEnabled="false",但滞后仍然存在。

任何人都可以指出可能导致此滚动滚动问题或建议的原因。我从适配器中带有固定器的列表视图切换到回收器视图,听说它更平滑,更好...但是滚动时似乎更糟...

这是适配器:

package com.example.gurahumorului.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.widget.CircularProgressDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.gurahumorului.R;
import com.example.gurahumorului.domain.Post;
import com.squareup.picasso.Picasso;

import java.util.List;

public class PostAdaptor extends RecyclerView.Adapter<PostAdaptor.PostViewHolder> {

    private Context context;
    private List<Post> posts;

    public PostAdaptor(Context context, List<Post> posts) {
        this.context = context;
        this.posts = posts;
    }

    public class PostViewHolder extends RecyclerView.ViewHolder {
        public TextView textViewTittle;
        public ImageView imageViewBanner;

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

            textViewTittle = itemView.findViewById(R.id.textViewTitlePost);
            imageViewBanner = itemView.findViewById(R.id.imageViewBannerPost);
        }
    }

    @NonNull
    @Override
    public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        View v = LayoutInflater.from(context).inflate(R.layout.post_item, parent, false);
        return new PostViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull PostViewHolder postViewHolder, int i) {
        Post post = posts.get(i);
        postViewHolder.textViewTittle.setText(post.getTitle());
        Picasso.get().load(post.getImageUrl()).placeholder(new CircularProgressDrawable(context)).into(postViewHolder.imageViewBanner);
    }

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

这是主类:

package com.example.gurahumorului.activity;

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.example.gurahumorului.adapter.PostAdaptor;
import com.example.gurahumorului.R;
import com.example.gurahumorului.domain.Post;
import com.example.gurahumorului.domain.User;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.List;

public class YourPostsActivity extends AppCompatActivity {

    private RecyclerView recyclerViewYourPosts;
    private PostAdaptor postAdaptor;

    private DatabaseReference databaseReference;
    private List<Post> posts;
    private User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_your_posts);

        user = getIntent().getParcelableExtra("user");

        recyclerViewYourPosts = findViewById(R.id.recyclerViewYourPosts);
        recyclerViewYourPosts.setHasFixedSize(true);
        recyclerViewYourPosts.setLayoutManager(new LinearLayoutManager(this));

        posts = new ArrayList<>();

        databaseReference = FirebaseDatabase.getInstance().getReference("posts");
        getPosts();
    }

    private void getPosts(){
        databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for(int i=user.getPosts().size()-1;i>=0;i--) {
                    Post post = dataSnapshot.child(user.getPosts().get(i)).getValue(Post.class);
                    if (post != null)
                        posts.add(post);
                }
                postAdaptor = new PostAdaptor(YourPostsActivity.this, posts);

                recyclerViewYourPosts.setAdapter(postAdaptor);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
            }
        });
    }
}

0 个答案:

没有答案