Recyclerview + Firebase在某些设备上滚动缓慢

时间:2019-04-17 21:37:46

标签: java android firebase firebase-realtime-database android-recyclerview

我需要帮助来加快我的Android App中的RecyclerView速度。我有一个从Firbase,recyclerview适配器和recyclerview视图持有者检索数据列表的类。我的问题是,在某些设备上,recyclerview滚动太慢。我需要优化的方法。

我还有另一个问题是:E / RecyclerView:未连接适配器;跳过布局

我尝试开始一项新任务,但是滚动也很慢

这是我的recyclerview适配器:

public class PostAdapter extends RecyclerView.Adapter<PostViewHolder> {

    private List<Post> PostData;
    private Context context;
    private ItemClickListener mListener;

    public PostAdapter(List<Post> postData, Context context, ItemClickListener listener) {
        this.PostData = postData;
        this.context = context;
        this.mListener = listener;
    }

    @NonNull
    @Override
    public PostViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View itemView = inflater.inflate(R.layout.blog_row,viewGroup,false);
        return new PostViewHolder(itemView, mListener);
    }

    @Override
    public void onBindViewHolder(@NonNull PostViewHolder postViewHolder, int i) {
        postViewHolder.postImageview.setImageURI(PostData.get(i).getImage());
        postViewHolder.posttitletext.setText(PostData.get(i).getTitle());
        postViewHolder.postdesctext.setText(PostData.get(i).getDesc());


    }

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

这是我的recyclerview ViewHolder:

public class PostViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public TextView posttitletext;
    public TextView postdesctext;
    public SimpleDraweeView postImageview;
    private ItemClickListener mListener;

    public PostViewHolder(@NonNull View itemView, ItemClickListener listener) {
        super(itemView);

        posttitletext = itemView.findViewById(R.id.post_title);
        postdesctext = itemView.findViewById(R.id.post_desc);
        postImageview = itemView.findViewById(R.id.post_image);
        mListener = listener;
        itemView.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        mListener.onClick(v,getAdapterPosition());

    }

}

这是我的Firebase类,用于获取数据:

public class FirebaseGetData {
    FirebaseDatabase database = FirebaseDatabase.getInstance();

    private List<Post> postx = new ArrayList<>();
    private List<Product> prd = new ArrayList<>();
    private List<String> prdid = new ArrayList<>();



    public void readDataPost(final FirebaseCallbackPost firebaseCallback ){

        ValueEventListener eventListener =  new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
                postx.clear();
                for (DataSnapshot postSnapshot: snapshot.getChildren()) {
                    Post ps = postSnapshot.getValue(Post.class);
                    postx.add(ps);
                }
                firebaseCallback.onCallback(postx);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                System.out.println("The read failed: " + databaseError.getMessage());
            }
        };


        DatabaseReference post = database.getReference().child("Post");
        post.addListenerForSingleValueEvent(eventListener);

    }

    public interface FirebaseCallbackPost{
        void onCallback(List<Post> list);
    }

}

这就是我设置回收站视图的方式:

recycler_post = view.findViewById(R.id.myrecyclerview);
        recycler_post.setHasFixedSize(true);

        layoutmng = new LinearLayoutManager(getContext());
        recycler_post.setLayoutManager(layoutmng);

这就是我如何用数据显示recyclerview

private void LoadData() {


        new FirebaseGetData().readDataPost(new FirebaseGetData.FirebaseCallbackPost() {
            @Override
            public void onCallback(List<Post> list) {
                posts = list;
                Log.d("XXX", String.valueOf(posts.size()));
                listener = new ItemClickListener() {
                    @Override
                    public void onClick(View view, int position) {
                        Toast.makeText(getContext(), "Offertona!!", Toast.LENGTH_SHORT).show();
                    }
                };
                adapter = new PostAdapter(posts, getContext(), listener);
                recycler_post.setAdapter(adapter);
            }
        });

    }

0 个答案:

没有答案