如何让添加到firebase的新项目出现在适用于GridLayoutManager的Recyclerview的顶部?

时间:2017-02-23 00:49:01

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

我使用LinearLayoutManager来调整我的RecyclerView,当我添加mLayoutManager.setReverseLayout(true);mLayoutManager.setStackFromEnd(true);时,情况良好。但是当我更改为GridLayoutManager时,我无法使用mLayoutManager.setStackFromEnd(true);,这会让我新添加的项目显示在底部..

无论如何都要克服这个问题吗?

以下是我的Java代码:

public class HomeActivity extends Fragment {

private RecyclerView mBlogList;
private LinearLayoutManager mLayoutManager;

private FirebaseAuth mAuth;

private DatabaseReference mDatabase;
private DatabaseReference mDatabaseLike;
private DatabaseReference mUsername;

private boolean mProcessLike = false;

Activity context;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.activity_home, container, false);
    context=getActivity();

    mAuth = FirebaseAuth.getInstance();

    mDatabaseLike =  FirebaseDatabase.getInstance().getReferenceFromUrl("https://my-gg-app.firebaseio.com").child("Likes");
    mDatabaseLike.keepSynced(true);
    mUsername =  FirebaseDatabase.getInstance().getReferenceFromUrl("https://my-gg-app.firebaseio.com").child("Users");
    mUsername.keepSynced(true);
    mDatabase =  FirebaseDatabase.getInstance().getReferenceFromUrl("https://my-gg-app.firebaseio.com").child("Blog");;
    mDatabase.keepSynced(true);

    mBlogList = (RecyclerView)rootView.findViewById(R.id.blog_list);
    mBlogList.setHasFixedSize(true);

    mLayoutManager = new GridLayoutManager(context.getApplicationContext(),2);
    mLayoutManager.setReverseLayout(true);
    mLayoutManager.setStackFromEnd(true);

    mBlogList.setLayoutManager(mLayoutManager);

    FirebaseRecyclerAdapter<bLOG,BlogViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<bLOG, BlogViewHolder>(
            bLOG.class,
            R.layout.blog_row,
            BlogViewHolder.class,
            mDatabase

    ) {

        @Override
        protected void populateViewHolder(BlogViewHolder viewHolder, bLOG model, int position) {

            final String post_key = getRef(position).getKey();

            viewHolder.setTitle(model.getTitle());
            viewHolder.setDesc(model.getDesc());
            viewHolder.setUsername(model.getUsername());
            viewHolder.setImage(context.getApplicationContext(),model.getImage());
            viewHolder.setuserImage(context.getApplicationContext(),post_key);
            viewHolder.setLikeBtn(post_key);

            viewHolder.post_image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent singleblogintent = new Intent(context, SinglePost.class);
                    singleblogintent.putExtra("blog_id",post_key);
                    startActivity(singleblogintent);
                }
            });

            viewHolder.mLikebtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    mProcessLike = true;

                    mDatabaseLike.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            if (mProcessLike) {

                                if (dataSnapshot.child(post_key).hasChild(mAuth.getCurrentUser().getUid())) {

                                    mDatabaseLike.child(post_key).child(mAuth.getCurrentUser().getUid()).removeValue();
                                    mProcessLike = false;

                                } else {
                                    mDatabaseLike.child(post_key).child(mAuth.getCurrentUser().getUid()).setValue("RandomValue");
                                    mProcessLike = false;
                                }
                            }

                        }
                        @Override
                        public void onCancelled (DatabaseError databaseError){

                        }
                    });
                }
            });
        }
    };

    mBlogList.setAdapter(firebaseRecyclerAdapter);
    return rootView;
}


public static class BlogViewHolder extends RecyclerView.ViewHolder{

    View mView;
    ImageButton mLikebtn;
    TextView post_username;
    ImageView post_image;
    CircleImageView post_userimage;

    DatabaseReference mDatabaseLike,mUsername;
    FirebaseAuth mAuth;

    public BlogViewHolder(View itemView) {
        super(itemView);
        mView = itemView;

        post_image = (ImageView) mView.findViewById(R.id.postImage);
        post_userimage = (CircleImageView) mView.findViewById(R.id.userimage);
        mLikebtn = (ImageButton) mView.findViewById(R.id.likeBtn);
        post_username = (TextView)mView.findViewById(R.id.postName);

        post_username.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.v("MainACTIVITY", "Some Text");
            }
        });

        mDatabaseLike = FirebaseDatabase.getInstance().getReferenceFromUrl("https://my-gg-app.firebaseio.com").child("Likes");
        mDatabaseLike.keepSynced(true);
        mUsername =  FirebaseDatabase.getInstance().getReferenceFromUrl("https://my-gg-app.firebaseio.com").child("Users");
        mUsername.keepSynced(true);
        mAuth = FirebaseAuth.getInstance();

    }
    public void setLikeBtn(final String post_key){

        mDatabaseLike.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (dataSnapshot.child(post_key).hasChild(mAuth.getCurrentUser().getUid())) {

                    mLikebtn.setImageResource(R.drawable.like_red);

                } else {

                    mLikebtn.setImageResource(R.drawable.like_white);

                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    }

    public void setuserImage(final Context ctx, final String post_key){

        mUsername.child(post_key).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                String imagedata = (String) dataSnapshot.child("image").getValue();

                if (imagedata != null && imagedata.equals("default")) {
                    post_userimage.setImageResource(R.mipmap.profilepic);
                }
                else{
                }
                Picasso.with(ctx).load(imagedata).into(post_userimage);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    }

    public void setTitle(String title){
        TextView post_title = (TextView)mView.findViewById(R.id.postTitle);
        post_title.setText(title);
    }
    public  void setDesc(String desc){
        TextView post_desc = (TextView)mView.findViewById(R.id.postDescrip);
        post_desc.setText(desc);
    }
    public  void setUsername(String username){
        TextView post_username = (TextView)mView.findViewById(R.id.postName);
        post_username.setText(username);
    }
    public void setImage(Context ctx, String image){
        //ImageView post_image = (ImageView)mView.findViewById(R.id.postImage);
        Picasso.with(ctx).load(image).fit().centerCrop().into(post_image);
    }
}

0 个答案:

没有答案