【问题标题】:Why onBindViewHolder index isn't incrementing in Recycler View?为什么 onBindViewHolder 索引在 Recycler View 中没有增加?
【发布时间】:2019-07-23 06:21:15
【问题描述】:

infoOfPost.java

包 com.sakib23.task1; 公共类 infoOfPost { 字符串用户名; 字符串项目标题、项目描述、项目时间; 布尔项目标志; 公共 infoOfPost(字符串用户名,字符串项目标题,字符串项目描述,字符串项目时间,布尔项目标志){ 用户名 = 用户名; 项目标题 = 项目标题; 项目描述 = 项目描述; 项目时间 = 项目时间; 项目标志 = 项目标志; } 公共字符串 getUserName() { 返回用户名; } 公共无效 setUserName(字符串用户名){ 用户名 = 用户名; } 公共字符串 getProjectTitle() { 返回项目名称; } 公共无效 setProjectTitle(字符串项目标题){ 项目标题 = 项目标题; } 公共字符串 getProjectDescription() { 返回项目描述; } 公共无效 setProjectDescription(字符串项目描述){ 项目描述 = 项目描述; } 公共字符串 getProjectTime() { 返回项目时间; } 公共无效 setProjectTime(字符串项目时间){ 项目时间 = 项目时间; } 公共布尔 getProjectFlag() { 返回项目标志; } 公共无效 setProjectFlag(布尔项目标志){ 项目标志 = 项目标志; } }

MainActivity.java

package com.sakib23.task1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    ArrayList<infoOfPost> arrayList;

    myAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = findViewById(R.id.RecyclerViewID);
        arrayList = new ArrayList<>();

        arrayList.add(new infoOfPost("A B C", "B", "C", "01/02/12", true));
        arrayList.add(new infoOfPost("B A A", "B", "C", "06/02/12", true));
        arrayList.add(new infoOfPost("A B A", "B", "C", "01/03/12", false));
        arrayList.add(new infoOfPost("C A A", "B", "C", "02/03/12", false));
        arrayList.add(new infoOfPost("A A A", "B", "C", "01/09/12", true));
        arrayList.add(new infoOfPost("A L A", "B", "C", "11/12/12", false));

        adapter = new myAdapter(this, arrayList);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
    }
}

myAdapter.java

包 com.sakib23.task1; 导入android.content.Context; 导入android.support.annotation.NonNull; 导入 android.support.v7.widget.RecyclerView; 导入android.util.Log; 导入 android.view.LayoutInflater; 导入android.view.View; 导入android.view.ViewGroup; 导入 android.widget.TextView; 导入 android.widget.Toast; 导入 java.util.ArrayList; 公共类 myAdapter 扩展 RecyclerView.Adapter { 上下文上下文; 数组列表数组列表; 公共 myAdapter(上下文上下文,ArrayList arrayList){ this.context = 上下文; this.arrayList = 数组列表; Log.d("TAGG", "总大小:" + arrayList.size()); // 好的,没关系。 } @NonNull @覆盖 public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { 视图视图 = LayoutInflater.from(context).inflate(R.layout.sample_post, viewGroup, false); 返回新的 MyViewHolder(视图); } @覆盖 公共无效 onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) { Log.d("TAGG", "索引" + i); // 它不递增 myViewHolder.userName.setText(arrayList.get(i).getUserName()); myViewHolder.projectTitle.setText(arrayList.get(i).getProjectTitle()); myViewHolder.projectDescription.setText(arrayList.get(i).getProjectDescription()); myViewHolder.projectTime.setText(arrayList.get(i).getProjectTime()); if(arrayList.get(i).getProjectFlag() == false) myViewHolder.projectFlag.setText("未标记帖子"); 否则 myViewHolder.projectFlag.setText("标记的帖子"); } @覆盖 公共 int getItemCount() { Log.d("TAGG", "GETSIZE" + arrayList.size()); // 这也很好.. 返回 arrayList.size(); } 类 MyViewHolder 扩展 RecyclerView.ViewHolder { TextView 用户名、项目标题、项目描述、项目时间、项目标志; public MyViewHolder(@NonNull View itemView) { 超级(项目视图); 用户名 = itemView.findViewById(R.id.usernameID); projectTitle = itemView.findViewById(R.id.projecttitleID); projectDescription = itemView.findViewById(R.id.projectdesriptionID); projectTime = itemView.findViewById(R.id.projecttimeID); projectFlag = itemView.findViewById(R.id.projectflagID); } } }

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:padding="10dp"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <android.support.v7.widget.RecyclerView

        android:id="@+id/RecyclerViewID"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

示例帖子

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="UserName"
        android:id="@+id/usernameID"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="ProjectTitle"
        android:id="@+id/projecttitleID"
        />


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="ProjectDescription"
        android:id="@+id/projectdesriptionID"
        />


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="ProjectTime"
        android:id="@+id/projecttimeID"
        />


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Flag"
        android:id="@+id/projectflagID"
        />
</LinearLayout>

在 myAdapter.java 文件中,onBindViewHolder 索引没有增加。请帮帮我。

【问题讨论】:

  • 你可以通过垂直滚动查看你的项目吗?
  • 是的。我垂直定向。我编辑了帖子。您现在还可以查看 xml 文件..
  • 第一件事交换这两行。 recyclerView.setAdapter(adapter); recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this)); .. 然后检查。
  • 如果您能看到recyclerview的其他项目,请尝试滚动屏幕​​。
  • 好的。我得到了错误。在 sample_post.xml 中。我应该写的,layout_height="wrap_content".. 谢谢大家....

标签: java android android-recyclerview


【解决方案1】:

视图持有者中的视图项占据了整个布局高度。请改用“wrap_content”。 sample_post.xml中的父LinearLayout请使用android:layout_height = "wrap_content"

【讨论】:

    【解决方案2】:

    这是我在使用适配器时遇到的问题。建议使用 ViewHolder 的getAdapterPosition() 方法来获取更新的索引。对你来说,获取索引是myViewHolder.getAdapterPosition()。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-16
      • 2019-08-09
      • 2016-05-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多