当前位置: 首页 > 知识库问答 >
问题:

无尽的回收器视图,多种视图类型,崩溃后OnscrollListener

霍建章
2023-03-14

我试图制作一个可在滚动条上生成项目的RecyclerView。方法

我试图实现的是使用循环生成一个无限的

示例:[示例_屏幕截图][1]

编辑:忘记在适配器构造函数中添加一些代码了!添加日志

public class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

List<Tipo> lista;
private int visibleThreshold = 14;
private int lastVisibleItem, totalItemCount;
private OnLoadMoreListener onLoadMoreListener;
private boolean loading;

public Adapter(List<Tipo> lista,RecyclerView recyclerView) {
    this.lista = lista;


//Forgot to add this part on the original question!

if(recyclerView.getLayoutManager() instanceof LinearLayoutManager){
final LinearLayoutManager linearLayoutManager =  
(LinearLayoutManager)recyclerView.getLayoutManager();
        recyclerView.addOnScrollListener(

                new RecyclerView.OnScrollListener() {

                    @Override
             public void onScrolled(RecyclerView recyclerView, int dx,int dy){
                      super.onScrolled(recyclerView, dx, dy);
                      totalItemCount = linearLayoutManager.getItemCount();
                      lastVisibleItem = 
                      linearLayoutManager.findLastVisibleItemPosition();
                      if(!loading && totalItemCount <= (lastVisibleItem + 
                      visibleThreshold)){
                         if(onLoadMoreListener != null){
                            onLoadMoreListener.onLoadMore();
                         }
                      loading = true;
                }
            }


        });
    }

//New part ended
    }
}



public void setLoaded() {
    loading = false;
}




public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener){
    this.onLoadMoreListener = onLoadMoreListener;
}

public interface OnLoadMoreListener {
    void onLoadMore();
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v;
    switch(viewType) {
        case 0:
            v = LayoutInflater.from(parent.getContext()).inflate(R.layout.elemento_destro,parent,false);
            return new ViewHolder1(v);
        case 1:
            v = LayoutInflater.from(parent.getContext()).inflate(R.layout.elemento_sinistro,parent,false);
            return new ViewHolder2(v);

    }
    return null;
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder,  
int position) {

    Tipo oggetto = lista.get(position);
    if(oggetto != null)
    {
        if(oggetto.getType())
        {((ViewHolder1)viewHolder).titolo1.setText(oggetto.getName());}
        else
        {((ViewHolder2)viewHolder).titolo2.setText(oggetto.getName()); }
    }
}

@Override
public int getItemCount() {
    if (lista == null)
        return 0;
    return lista.size();
}

public int getItemViewType(int position){
    if (lista.get(position).DIREZIONE)
    {
       return 0;
    }
    else {return 1;}
}
class ViewHolder1 extends  RecyclerView.ViewHolder implements View.OnClickListener{

private TextView titolo1;
public ViewHolder1(@NonNull View itemView) {
    super(itemView);
    titolo1 = itemView.findViewById(R.id.testoDestro);
}

@Override
public void onClick(View v) {

}
}

类ViewHolder2扩展了RecyclerView。ViewHolder实现了视图。OnClickListener{

private TextView titolo2;
public ViewHolder2(@NonNull View itemView) {
    super(itemView);
    titolo2 = itemView.findViewById(R.id.testoSinistro);
}

@Override
public void onClick(View v) {

}
}
public class MainActivity extends AppCompatActivity {

private List<Tipo> lista;
private List<String> lista2;
ImageView indietro;
Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    RecyclerView recyclerView;

    LinearLayoutManager linearLayoutManager;
    linearLayoutManager = new LinearLayoutManager(MainActivity.this);

    recyclerView = (findViewById(R.id.recycler));
    indietro = findViewById(R.id.indietro);

    recyclerView.setLayoutManager(linearLayoutManager);
    final Adapter adapter = new Adapter(Dati.getData(), recyclerView);
    recyclerView.setAdapter(adapter);


    recyclerView.setLayoutManager(linearLayoutManager);
    recyclerView.setAdapter(adapter);


    adapter.setOnLoadMoreListener(new Adapter.OnLoadMoreListener() {
        @Override
        public void onLoadMore() {
            lista.add(null);
            adapter.notifyItemInserted(lista.size() - 1);
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    lista.remove(lista.size() - 1);
                    adapter.notifyItemRemoved(lista.size());
                    for (int i = 0; i < 15; i++) {
                        if (i % 2 == 1) {
                            lista.add(new Tipo("Example",false));
                            adapter.notifyItemInserted(lista.size());
                        }
                        else{
                            lista.add(new Tipo("Example",true));
                            adapter.notifyItemInserted(lista.size());
                        }

                    }
                    adapter.setLoaded();
                }
            }, 3000);
            System.out.println("load");
        }
    });



   }

}
2019-05-03 10:08:25.249 5254-5254/? I/o.myapplicatio: Late-enabling -Xcheck:jni
2019-05-03 10:08:25.469 5254-5254/com.sinfo.myapplication W/o.myapplicatio: JIT profile information will not be recorded: profile file does not exits.
2019-05-03 10:08:25.472 5254-5254/com.sinfo.myapplication I/chatty: uid=10122(com.sinfo.myapplication) identical 10 lines
2019-05-03 10:08:25.472 5254-5254/com.sinfo.myapplication W/o.myapplicatio: JIT profile information will not be recorded: profile file does not exits.
2019-05-03 10:08:25.499 5254-5254/com.sinfo.myapplication I/InstantRun: starting instant run server: is main process
2019-05-03 10:08:25.720 5254-5254/com.sinfo.myapplication W/o.myapplicatio: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
2019-05-03 10:08:25.722 5254-5254/com.sinfo.myapplication W/o.myapplicatio: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
2019-05-03 10:08:25.969 5254-5254/com.sinfo.myapplication D/OpenGLRenderer: Skia GL Pipeline
2019-05-03 10:08:26.073 5254-5272/com.sinfo.myapplication I/Adreno: QUALCOMM build                   : a570411, Id0218a7f5c
Build Date                       : 09/27/18
OpenGL ES Shader Compiler Version: EV031.25.03.00
Local Branch                     : 
Remote Branch                    : refs/tags/AU_LINUX_ANDROID_LA.UM.7.5.R1.09.00.00.464.025
Remote Branch                    : NONE
Reconstruct Branch               : NOTHING
2019-05-03 10:08:26.073 5254-5272/com.sinfo.myapplication I/Adreno: Build Config                     : S L 6.0.7 AArch64
2019-05-03 10:08:26.083 5254-5272/com.sinfo.myapplication I/Adreno: PFP: 0x005ff110, ME: 0x005ff066
2019-05-03 10:08:26.089 5254-5272/com.sinfo.myapplication I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
2019-05-03 10:08:26.089 5254-5272/com.sinfo.myapplication I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 1
2019-05-03 10:08:26.090 5254-5272/com.sinfo.myapplication I/OpenGLRenderer: Initialized EGL, version 1.4
2019-05-03 10:08:26.090 5254-5272/com.sinfo.myapplication D/OpenGLRenderer: Swap behavior 2
2019-05-03 10:08:26.127 5254-5254/com.sinfo.myapplication W/o.myapplicatio: Accessing hidden field Landroid/view/View;->mAccessibilityDelegate:Landroid/view/View$AccessibilityDelegate; (light greylist, reflection)
2019-05-03 10:08:46.624 5254-5254/com.sinfo.myapplication W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@e5efecf
2019-05-03 10:08:48.664 5254-5254/com.sinfo.myapplication D/AndroidRuntime: Shutting down VM
2019-05-03 10:08:48.672 5254-5254/com.sinfo.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sinfo.myapplication, PID: 5254
java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
    at com.sinfo.myapplication.MainActivity$1.onLoadMore(MainActivity.java:53)
    at com.sinfo.myapplication.Adapter$1.onScrolled(Adapter.java:43)
    at android.support.v7.widget.RecyclerView.dispatchOnScrolled(RecyclerView.java:4961)
    at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:5117)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
    at android.view.Choreographer.doCallbacks(Choreographer.java:761)
    at android.view.Choreographer.doFrame(Choreographer.java:693)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6718)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

共有2个答案

李甫
2023-03-14
lista = new Arraylist<>();

你需要在使用列表之前初始化它。

景永望
2023-03-14

我相信你的问题是如何通知适配器。如果你没有正确通知它,这将导致适配器和数据之间的不一致,应用程序将崩溃。

例如,如果您删除了所有项,但告诉适配器您添加了更多项,这将导致崩溃。

在你的代码中,你在添加项目,并告诉它两个不同的位置。

lista.add(null);
adapter.notifyItemInserted(lista.size() - 1);

lista.add(new Tipo("Example",false));
adapter.notifyItemInserted(lista.size());

你告诉适配器你在哪里插入了新项目,但在这些地方你告诉它两个不同的位置(.size())。大小()-1)。

 类似资料:
  • 我正在拼命地尝试在一个使用Kotlin的android应用上实现无休止的滚动。所有的教程要么是无用的,因为他们没有正确地解释事情。例如:https://github.com/chetdeva/recyclerView-bindings 它看起来很有前途,但作者使用了这样的短语,比如“把这个放在你的bindingadapter中”,所以我查看了这个是什么,我找到了一个java文件,但如果你在其中插入

  • 在旁观者(@NonNull视听者持有人,国际位置)上的公共空白{ 单击cardView时,Intent不会传递给其他活动。

  • 在其他回收器视图中有一个回收器视图。两者都需要垂直滚动。外部回收器视图滚动正常,但内部回收器视图滚动不正常。 这是代码: ViewAdapter如下: 我尝试了以下两种回收商观点,但都无法解决问题 也尝试了这个:

  • 我在我的应用程序中使用了具有多个视图的回收器视图。这工作正常,但我有一个问题,滚动这个回收视图!当我想上下滚动时,它有滞后现象。这是我的回收器视图适配器: 这个回收器视图有6种以上的视图类型,我必须显示,但是当回收器视图中的项目增加时,我在上下滚动时会有很大的延迟。我不知道为什么我在滚动上有延迟!我真的很感谢你的帮助。

  • 我正在尝试在我的 上实现 ,但我没有得到任何结果。 我遵循了这个教程和这个技巧,但是没有人为我工作。 我已经实现了: 但它不让我编译,因为它们是不同的< code > viewmoder ,因为我创建了两个< code > viewmoder 类,但它们< code >扩展了Recycler。ViewHolder所以我不明白... 我正在尝试这样做,因为我有一个,我希望当列表为空时,它会显示一个,

  • 我刚开始在firebase工作。我设法上传了文本和图像,但是,我无法检索要显示在回收器视图中的图像,只能检索文本。我用的是毕加索依赖。我已经包括了我的主要活动。java类,该类负责显示从问题“我的适配器”中的firebase检索的回收器视图项。java类和模型类。我相信,在我将图像URI上载到firebase存储时,我可能犯了没有存储图像URI的错误,因此适配器无法检索图像位置。我想这可能是因为我