缓慢且滞后的自定义listView

问题描述 投票:0回答:3

我有一个带有许多文本和图像的自定义列表视图(大约60个图像),当滚动时,它非常滞后。我在我的代码中添加了ViewHolder,但它仍然滞后。我知道解决方案可能是使用后台线程,但我不知道如何做到这一点。你们能帮助我吗?

custom adapter.Java:

public class CustomAdapter extends RecyclerView.Adapter<CustomViewHolder> {

    //Attributi:
    private Context context;
    private int[] immagineDio;
    private String[] nomeDio;

    //Costruttori:
    public CustomAdapter(Context context, int[] immagineDio, String[] nomeDio){
        this.context = context;
        this.immagineDio = immagineDio;
        this.nomeDio = nomeDio;
    }

    //Metodi di istanza:
    @NonNull
    @Override
    public CustomViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return CustomViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.listview_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull CustomViewHolder holder, int position) {
        holder.bind(immagineDio[position], nomeDio[position]);
    }

    @Override
    public int getItemCount() {
        return nomeDio.length;
    }

}

custom view holder.Java:

 public class CustomViewHolder extends RecyclerView.ViewHolder {

     ImageView mFlag;
     TextView mName;

     public CustomViewHolder(@NonNull View itemView) {
         super(itemView);
         mFlag = itemView.findViewById(R.id.imageView);
         mName = itemView.findViewById(R.id.textView);
     }

     //binding data with UI
     void bind(int imageId, String name) {
         mFlag.setImageResource(imageId);
         mName.setText(name);
     } }

ListView activity.Java:

 public class ListViewActivity extends AppCompatActivity {
     @Override
     protected void onCreate(@Nullable Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.listview_layout);

         String[] nomeDei = {"Baldr","Borr","Bragi","Dagr","Dellingr","Eir","Eostre","Forseti","Freya","Freyr","Frigg","Fulla","Gefjun","Gerðr","Gullveig","Heimdallr","Hel","Hermóðr","Höðr","Hœnir","Iðunn","Itreksjóð","Jǫrð","Kvasir","Lóðurr","Lofn","Logi","Lýtir","Máni","Mímir","Móði","Nanna","Njörun","Njörðr","Nótt","Óðr","Rán","Ríg","Sága","Sif","Signe","Sigyn","Sinfjötli","Sjöfn","Skaði","Skirnir","Snotra","Sól","Syn","Thor","Týr","Ullr","Váli","Vár","Ve","Viðarr","Víli","Vör"};
         int[] immagineDei = {
                 R.drawable.profilo_baldr, 
                 R.drawable.profilo_borr,
                 R.drawable.profilo_bragi,
                 R.drawable.profilo_dagr,
                 R.drawable.profilo_dellingr,
                 R.drawable.profilo_eir, 
                 R.drawable.profilo_eostre,
                 R.drawable.profilo_forseti, 
                 R.drawable.profilo_freya, 
                 R.drawable.profilo_freyr,
                 R.drawable.profilo_frigg,
                 R.drawable.profilo_fulla,
                 R.drawable.profilo_gefjun,
                 R.drawable.profilo_geror,
                 R.drawable.profilo_gullveig,
                 R.drawable.profilo_heimdallr,
                 R.drawable.profilo_hel, 
                 R.drawable.profilo_hermoor,
                 R.drawable.profilo_hoor,
                 R.drawable.profilo_hoenir,
                 R.drawable.profilo_iounn,
                 R.drawable.profilo_itreksjoo,
                 R.drawable.profilo_joro,
                 R.drawable.profilo_kvasir,
                 R.drawable.profilo_loourr,
                 R.drawable.profilo_lofn,
                 R.drawable.profilo_logi,
                 R.drawable.profilo_lytir,
                 R.drawable.profilo_mani,
                 R.drawable.profilo_mimir,
                 R.drawable.profilo_modi,
                 R.drawable.profilo_nanna,
                 R.drawable.profilo_njorun,
                 R.drawable.profilo_njoror,
                 R.drawable.profilo_nott,
                 R.drawable.profilo_oor,
                 R.drawable.profilo_ran,
                 R.drawable.profilo_rig,
                 R.drawable.profilo_saga,
                 R.drawable.profilo_sif,
                 R.drawable.profilo_signe,
                 R.drawable.profilo_sigyn,
                 R.drawable.profilo_sinfjotli,
                 R.drawable.profilo_sjofn,
                 R.drawable.profilo_skaoi,
                 R.drawable.profilo_skirnir,
                 R.drawable.profilo_snotra,
                 R.drawable.profilo_sol,
                 R.drawable.profilo_syn,
                 R.drawable.profilo_thor,
                 R.drawable.profilo_tyr,
                 R.drawable.profilo_ullr, 
                 R.drawable.profilo_vali,
                 R.drawable.profilo_var,
                 R.drawable.profilo_ve,
                 R.drawable.profilo_vidar,
                 R.drawable.profilo_vili,
                 R.drawable.profilo_vor,
         };

         ListView listViewReference = findViewById(R.id.listView);
         CustomAdapter customAdapter = new CustomAdapter(ListViewActivity.this, immagineDei, nomeDei);
         listViewReference.setAdapter(customAdapter); //this line gives an error
     } }

listview_layout.xml:

 <?xml version="1.0" encoding="utf-8"?>
 <android.support.constraint.ConstraintLayout
 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=".HomeActivity">


     <android.support.v7.widget.RecyclerView
         android:id="@+id/listView"
         android:layout_width="match_parent"
         android:layout_height="match_parent" /> </android.support.constraint.ConstraintLayout>
android multithreading listview lag custom-lists
3个回答
0
投票

如果您的应用需要显示基于大数据集(或经常更改的数据)的元素滚动列表,则应使用RecyclerView


0
投票

您可以将其转换为RecyclerView。这是如何做。

CustomAdapter.java

public class CustomAdapter extends RecyclerView.Adapter<CustomViewHolder> {

    private Context context;
    private int[] immagineDio;
    private String[] nomeDio;

    public CustomAdapter(Context context, int[] immagineDio, String[] nomeDio){
        this.context = context;
        this.immagineDio = immagineDio;
        this.nomeDio = nomeDio;
    }

    @NonNull
    @Override
    public CustomViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return CustomViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.listview_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull CustomViewHolder holder, int position) {
        holder.bind(immagineDio[position],nomeDio[position]);
    }

    @Override
    public int getItemCount() {
        return nomeDio.length;
    }
}

CustomViewHolder.java

public class CustomViewHolder extends RecyclerView.ViewHolder {

    ImageView mFlag;
    TextView mName;

    public CustomViewHolder(@NonNull View itemView) {
        super(itemView);
        mFlag = itemView.findViewById(R.id.imageView);
        mName = itemView.findViewById(R.id.textView);
    }

    // binding data with ui
    void bind(int imageId, String name) {
        mFlag.setImageResource(imageId);
        mName.setText(name);
    }
}

0
投票

如果你已经在使用ViewHolder,那么ListViewRecyclerView之间应该没有太多性能差异。

相反,你应该像Glide一样加载另一个线程。代码应该非常简单,只需要替换

imageView.setImageResource(R.drawable.some)

Glide.with(activity).load(R.drawable.some).into(imageView)

更多信息:

要使用Glide,您需要将Glide库添加到应用程序的build.gradle文件中:

repositories {
  mavenCentral()
  google()
}

dependencies {
  implementation 'com.github.bumptech.glide:glide:4.9.0'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
}
© www.soinside.com 2019 - 2024. All rights reserved.