- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在从 Firebase 数据库中获取一些数据,我正在尝试填充 RecyclerView
适配器。经过适配器的notifyDataSetChanged()
被调用,屏幕闪烁,没有任何 react ,我什至无法在 onBindViewHolder 中捕获断点。
这是我的代码:
POJO类:
public class Result2 implements Serializable {
private int score;
private String userName;
public Result2(int score, String userName) {
this.score = score;
this.userName = userName;
}
public int getScore() {
return score;
}
public String getUserName() {
return userName;
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="USERNAME"
android:textColor="#000"
android:textSize="16sp"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="SCORE"
android:textColor="#000"
android:textSize="16sp"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000"
/>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingTop="10dp"
android:scrollbars="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="@+id/username"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:textSize="14sp"/>
<TextView
android:id="@+id/score"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:textSize="14sp"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:background="#4545"/>
public class ScoreAdapter extends RecyclerView.Adapter<ScoreAdapter.ScoreViewHolder> {
private List<Result2> results = new ArrayList<>();
public void setResults(List<Result2> results) {
this.results.clear();
this.results.addAll(results);
notifyDataSetChanged();
}
@Override
public ScoreAdapter.ScoreViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.score_view_holder, parent, false);
return new ScoreAdapter.ScoreViewHolder(view);
}
@Override
public void onBindViewHolder(ScoreAdapter.ScoreViewHolder holder, int position) {
Result2 result = getResult(position);
if (result != null) {
holder.setUsername(result.getUserName() != null ? result.getUserName() : "-");
holder.setScore(String.valueOf(result.getScore()));
}
}
private Result2 getResult(int position) {
return !results.isEmpty() ? results.get(position) : null;
}
@Override
public int getItemCount() {
return results.size();
}
public class ScoreViewHolder extends RecyclerView.ViewHolder {
private TextView username;
private TextView score;
public ScoreViewHolder(View itemView) {
super(itemView);
username = itemView.findViewById(R.id.username);
score = itemView.findViewById(R.id.score);
}
public void setUsername(String username) {
this.username.setText(username);
}
public void setScore(String score) {
this.score.setText(score);
}
}
}
public class Results extends AppCompatActivity {
private DatabaseReference mDatabase;
private ScoreAdapter scoreAdapter;
private RecyclerView recyclerView;
private List<Result2> results = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_results);
recyclerView = findViewById(R.id.recycler_view);
scoreAdapter = new ScoreAdapter();
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(scoreAdapter);
mDatabase = FirebaseDatabase.getInstance().getReference();
loadResults();
}
private void loadResults() {
mDatabase.child("Users").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
Result2 result = childSnapshot.getValue(Result2.class);
if (result != null) {
results.add(result);
}
}
Toast.makeText(Results.this, String.valueOf(results.size()), Toast.LENGTH_SHORT).show();
scoreAdapter.setResults(results);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
}
);
}
}
最佳答案
您的代码中有几个问题。第一个是您正在调用 setResults()
方法并通过您的results
列出您的ScoreAdapter
类(class)和不是 给 build 者。为了解决这个问题,改变你的setResults()
方法成为这样的构造函数:
public ScoreAdapter(List<Result2> results) {
this.results.clear();
this.results.addAll(results);
notifyDataSetChanged();
}
RecyclerView
回调里面。所以要解决这个问题,只需移动以下代码行:
ScoreAdapter scoreAdapter = new ScoreAdapter(result);
recyclerView.setAdapter(scoreAdapter);
Toast.makeText(Results.this, String.valueOf(results.size()), Toast.LENGTH_SHORT).show();
scoreAdapter.setResults(results);
.
result
列表到构造函数。
public no-argument constructor
给您的
Result2
类(class)。请看
here 更多细节。
关于android - 在 RecyclerView 适配器 notifyDataSetChanged() 之后未调用 onBindViewHolder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51741809/
我正在开发我的应用程序,它要求电影海报的比例为 1:2。我找到了一种方法来做到这一点就是运行 movieAdapterViewHolder.posterview.requestLayo
我在 CardAdapter 中使用 onBindViewHolder。它包括 checkedTextView 的 onClick 方法。我试图获取这些文本并将其放入 Arraylist(shared
在适配器 submitList 到 DiffUtil 之后,调用 onBindViewHolder 并且出于某种原因它总是将列表滚动到顶部。 我尝试了 SO 中关于在 recyclerview 更新列
我收到错误:“FlickrRecyclerViewAdapter”类必须声明为抽象类或在“适配器”中实现方法抽象方法 onBindViewHolder(VH, int) 虽然这个错误应该是 self
我有一个 recyclerview 显示我的数据。在我的适配器类中,在 onBindViewHolder() 中,我为一个按钮创建了一个监听器,该按钮在单击时删除了一行。第一次工作得很好,但如果我尝试
如果图像不存在,我将尝试下载它们;如果已经下载,我将尝试保留并重新使用它们。 下载和展示作品时,不会重复使用它们。似乎 mValues 在不同的调用中不是相同的 mValues。 这是我的代码 pub
我已经将第三方库用于进度条,一切正常,但在 onBindViewHolder 方法中,当我编写 holder.donutprogress.setFinishedStrokeColor(colorsda
ViewPager 发布 1.0.0 版本 对于普通的 RecyclerView,holder.itemView给我当前绑定(bind)/渲染的 View 。 但是,FragmentStateAdap
我想在下一类 ChildViewHolder 的 onBindViewHolder 中访问 DayOfWeek,这样我就可以在 if 语句中使用它,如 ChildRecyclerAdapter 中所示
我在recycview中有很多按钮我只是想将它们添加到数组中,然后让它们添加一些工作。 首先我定义了数组名称“buttons”ToggleButton 按钮[] = new ToggleButton[
当我调用notifyDataSetChanged()时,它不会调用我的onBindViewHolder,我不知道它为什么这样做,我当前正在使用自定义布局,不知道这是否与它有关。 有人有什么想法吗?这是
我有一个TestActivity: public class TestActivity extends AppCompatActivity { @Override protected
我想填写诸如用户名、全名之类的内容,显示 follow_btn 的可见性。最大的问题是我不能在 onbindviewholder 中使用。它用红色下划线标出。你能帮我吗? 错误是:error: can
我正在尝试使用 RecyclerView 显示一个左侧有图像、右侧有标题的列表。 通过 for 循环,我在 ArrayList 中添加了标题和图像 String[] mtitles = {"Title
您好,当 recyclerview 第一次创建 OnbindViewHolder 时,我遇到了一个奇怪的问题,直到 recyclerview 中的最后一个项目被调用,然后当我滚动时OnbindView
onBindViewHolder 在数据绑定(bind)到 View 后的第一个位置和最后一个位置意外调用。这造成了问题,因为当我尝试向列表中添加项目时,最后一项的 ImageViews(相似和不同)
设置 我有一个带有自定义适配器和自定义 ViewHolder 的 RecyclerView。 我理解 onCreateViewHolder() 方法在 RecyclerView 用完 ViewHold
我知道这个问题已被多次询问与 ListViews 有关,但我一直无法真正理解解决方案。我知道 RecyclerViewAdapter 中 onBindViewHolder() 的位置参数类似于 lis
我已经将 RececlerView 与 ViewHolder 模式结合使用了一段时间。我正在实现自定义 Adapter。 我不是在我的代码中搜索特定的错误帮助。 我只是想知道,如果它是正常的,onBi
当我在我的 RecyclerView 上调用 notifyItemChanged() 时,相应的 Views(在本例中为按钮)没有按预期更新.我调用他们的原因是数据已经改变,这会导致他们的背景颜色改变
我是一名优秀的程序员,十分优秀!