CardView限量9件

时间:2017-12-10 18:52:43

标签: java android android-layout

我正在创建一个应用,其中一个活动在卡片视图中显示一些博客帖子数据。一切正常,直到我只有9个博客帖子显示,但当第十个帖子添加,然后卡片视图搞砸像第一个cardview项目和最后一个cardview项目中的10个帖子显示。 以下是填写Recyclerview Cardview的代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_transaction);
    LinearLayoutManager lm = new LinearLayoutManager(this);
    lm.setReverseLayout(true);
    lm.setStackFromEnd(true);
    recyclerView =  findViewById(R.id.blog_list);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(lm);
    mref = FirebaseDatabase.getInstance().getReference().child("IHIAUPDATES");

}

public static class bloglistviewholder extends RecyclerView.ViewHolder {
    static View mview;
    public bloglistviewholder(View itemView) {
        super(itemView);
        mview = itemView;
    }
    public static void setTitle(String title){
        TextView post_title = mview.findViewById(R.id.blog_title);
        post_title.setText(title);
    }
    public static void setPosteon(String postedon){
        TextView post_title = mview.findViewById(R.id.blog_postedon);
        post_title.setText(postedon);
    }
    public static void setPostby(String postedby){
        TextView post_title = mview.findViewById(R.id.blog_postedby);
        post_title.setText(postedby);
    }

    public static void setPost(String post){
        TextView post_content = mview.findViewById(R.id.blog_post);
        post_content.setText(post);
    }
    public static void setImageurl(Context ctx, String imageurl){
        ImageView post_image = mview.findViewById(R.id.blog_pic);
        Picasso.with(ctx).load(imageurl).into(post_image)
        ;
    }


}

@Override
protected void onStart() {
    super.onStart();

    FirebaseRecyclerAdapter<blog_list,bloglistviewholder> rAdapter = new FirebaseRecyclerAdapter<blog_list, bloglistviewholder>(

            blog_list.class,
            R.layout.blog_row,
            bloglistviewholder.class,
            mref.orderByKey()
    ) {
        @Override
        protected void populateViewHolder(bloglistviewholder viewHolder, blog_list model, int position) {
            bloglistviewholder.setTitle(model.getTitle());
            bloglistviewholder.setPost(model.getPost());
            bloglistviewholder.setPostby(model.getPostedby());
            bloglistviewholder.setPosteon(model.getPostedon());
            bloglistviewholder.setImageurl(getApplicationContext(),model.getImageurl());
        }
    };
    recyclerView.setAdapter(rAdapter);
}

Xml blog_row

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="8dp">

        <ImageView
            android:id="@+id/blog_pic"
            android:layout_width="120dp"
            android:layout_height="90dp"
            android:padding="4dp" />

        <TextView
            android:id="@+id/blog_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_toRightOf="@id/blog_pic"
            android:text="Apple MacBook Air Core i5 5th Gen - (8 GB/128 GB SSD/Mac OS Sierra)"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
            android:textColor="#000000" />

        <TextView
            android:id="@+id/blog_postedon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/blog_title"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:layout_toRightOf="@id/blog_pic"
            android:text="13.3 Inch, 256 GB"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Small" />

        <TextView
            android:id="@+id/blog_postedby"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/blog_postedon"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:layout_toRightOf="@id/blog_pic"
            android:background="@color/colorPrimary"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:text="4.7"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Small.Inverse"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/blog_post"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/blog_postedby"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:layout_toRightOf="@id/blog_pic"
            android:text="INR 56990"
            android:textAppearance="@style/TextAppearance.AppCompat" />

    </RelativeLayout>

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

</LinearLayout>

可能出现什么问题? 我做错了什么?

1 个答案:

答案 0 :(得分:0)

  

可能出错?

您将数据插入数据库时​​出现逻辑问题,导致第一项显示为第10项。但是,由于您没有显示代码,因此无法猜测。

您在调试代码方面做了什么?

  我做错了什么?

嗯,你有一个错误......所以......是的? :P