FirebaseUI RecyclerView在卡中加载图像的速度非常慢,并且滚动太慢

时间:2018-12-13 08:12:18

标签: firebase firebase-realtime-database picasso android-cardview firebaseui

从Firebase数据库中获取图像并使用卡内的Picasso库将图像加载到imageview中,但是加载图像的时间太长,滚动速度非常慢,并且应用程序在设备上使用了太多的内存。

MainActivity.class

这是我使用FirebaseRecyclerAdapter和Picasso库加载图像的类。

public class MainActivity extends AppCompatActivity {

private DatabaseReference reference;
private FirebaseDatabase database;
private Query posts;
private RecyclerView recyclerView;
private FirebaseRecyclerAdapter<Post, PostViewHolder> adapter;


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

    reference = 
  FirebaseDatabase.getInstance().getReference("Posts").child("Feeding 
  Trends");

    FirebaseRecyclerOptions<Post> options =
            new FirebaseRecyclerOptions.Builder<Post>()
                    .setQuery(reference, Post.class)
                    .setLifecycleOwner(this)
                    .build();

   adapter = new FirebaseRecyclerAdapter<Post, PostViewHolder> 
   (options) {
        @Override
        protected void onBindViewHolder(@NonNull PostViewHolder 
   holder, int position, @NonNull Post model) {

            holder.pTitle.setText(model.getTitle());
            holder.pLang.setText(model.getLanguage());
            holder.pDuration.setText(model.getDuration());
            holder.pDate.setText(model.getDate());
            holder.pCname.setText(model.getCname());



   Picasso.with(getBaseContext()).load(model.getCover())
   .fit().into(holder.pCover);


            holder.setItemClickListener(new ItemClickListener() {
                @Override
    public void onClick(@NotNull View view, int position, boolean 
    isLongClick) {
    Intent productIntent = new Intent(MainActivity.this, 
    MediaPlayer.class);
    productIntent.putExtra("menuID", 
    adapter.getRef(position).getKey());
                    startActivity(productIntent);
                }
            });
        }

        @NonNull
        @Override
        public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.post_layout, parent, false);
            return new PostViewHolder(view);
            //return null;
        }
    };

    recyclerView = findViewById(R.id.recycler);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setHasFixedSize(true);
   // adapter.startListening();
    recyclerView.setAdapter(adapter);

}

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

}

@Override
protected void onStop() {
    super.onStop();
   // adapter.stopListening();
}
}

main_activity.xml

MainActivity的布局文件,其中添加了Recycler视图。

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <RelativeLayout
            android:id="@+id/rel"
            android:layout_width="match_parent"
            android:layout_height="match_parent">


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_centerVertical="true"
                android:text="Vocally"
                android:textSize="20sp"/>

            <ImageView
                android:id="@+id/search"
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:src="@drawable/ic_serach"
                android:layout_toStartOf="@+id/account"
                android:layout_marginEnd="30dp"
                android:layout_centerVertical="true" />

            <ImageView
                android:id="@+id/account"
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:src="@drawable/ic_account"
                android:layout_marginEnd="20dp"
                android:layout_alignParentEnd="true"
                android:layout_centerVertical="true" />
        </RelativeLayout>

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

</android.support.design.widget.AppBarLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical">

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

PostViewHolder.class

这是我在FirebaseRecyclerAdapter中使用的ViewHolder类

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

public TextView pTitle, pCname, pLang, pDuration, pDate;
public ImageView pCover;

private ItemClickListener itemClickListener;

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

    pTitle = itemView.findViewById(R.id.p_title);
    pCover = itemView.findViewById(R.id.cover_image);
    pCname = itemView.findViewById(R.id.c_name);
    pDate = itemView.findViewById(R.id.u_time);
    pDuration = itemView.findViewById(R.id.duration);
    pLang = itemView.findViewById(R.id.lang);

    itemView.setOnClickListener(this);
 }

 public void setItemClickListener(ItemClickListener itemClickListener) 
{
    this.itemClickListener = itemClickListener;
}

@Override
public void onClick(View v) {
    itemClickListener.onClick(v, getAdapterPosition(), false);
}
}

post_layout.xml

这是卡片布局,将在其中显示图像和值。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_margin="10dp"
app:cardCornerRadius="0dp"
app:cardElevation="5dp">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="200dp">

    <RelativeLayout
        android:id="@+id/rel"
        android:layout_width="match_parent"
        android:layout_height="100dp">

        <ImageView
            android:id="@+id/cover_image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:padding="5dp"
            android:scaleType="centerCrop" />

        <TextView
            android:id="@+id/lang"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentBottom="true"
            android:layout_marginStart="10dp"
            android:background="@color/overlayBackground"
            android:gravity="center"
            android:text="Hindi"
            android:textColor="@android:color/white"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/duration"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_marginEnd="10dp"
            android:background="@color/overlayBackground"
            android:gravity="center"
            android:text="00:00"
            android:textColor="@android:color/white"
            android:textSize="20sp" />

    </RelativeLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/rel"
        android:layout_marginTop="10dp"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/ch_image"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:src="@drawable/ft" />

            <TextView
                android:id="@+id/p_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginStart="5dp"
                android:text="Rang De Basanti"
                android:textSize="20sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginTop="10dp">

            <TextView
                android:id="@+id/c_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Feeding Trends "
                android:textSize="15sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=" . "
                android:textSize="15sp" />

            <TextView
                android:id="@+id/u_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=" 2 days ago "
                android:textSize="15sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=" . "
                android:textSize="15sp" />

            <TextView
                android:id="@+id/p_count"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=" 1M plays"
                android:textSize="15sp" />
        </LinearLayout>

    </LinearLayout>

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

0 个答案:

没有答案