Bor*_*ora 6 android android-recyclerview
这个问题已被问过几次,但这些答案不适用于我。我想要一个更一般的答案,说明是什么导致了这个问题。
我的活动布局中有一个 recyclerview。recyclerview 的行是一个具有一个 imageview 和 textview 的约束布局:
<?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"
android:layout_width="match_parent"
android:layout_height="50dp"
android:clickable="true">
<ImageView
android:id="@+id/file_icon"
android:layout_width="50dp"
android:layout_height="50dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:text="File"
android:id="@+id/file_name"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toRightOf="@+id/file_icon"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
我有几个项目要在这个行结构中显示。我正确设置了适配器。但是,当我运行我的应用程序时,屏幕上的文本视图不会显示,直到我向下滚动并再次向上滚动。基本上,为了显示这些文本视图,需要将它们从显示中丢弃并再次输入。这是我的适配器代码:
public class FileViewAdapter extends RecyclerView.Adapter<FileViewAdapter.Viewholder> {
private Context context;
private List<File> files;
public FileViewAdapter(Context context, List<File> files) {
this.context = context;
this.files = files;
}
public FileViewAdapter(Context context){
this.context = context;
}
@Override
public Viewholder onCreateViewHolder(ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(context);
View layout = inflater.inflate(R.layout.file_list_item, viewGroup, false);
return new Viewholder(layout);
}
@Override
public void onBindViewHolder(Viewholder viewholder, int i) {
File file = files.get(i);
if (file.isDirectory()) {
viewholder.fileIcon.setImageDrawable(
context.getResources().getDrawable(R.drawable.ic_folder_black_24dp));
viewholder.wholeThing.setOnClickListener(null);
} else {
viewholder.fileIcon.setImageDrawable(
context.getResources().getDrawable(R.drawable.ic_insert_drive_file_black_24dp));
viewholder.wholeThing.setOnClickListener(null);
}
viewholder.fileName.setText(file.getName());
}
@Override
public int getItemCount() {
return files.size();
}
public void clear() {
files.clear();
notifyDataSetChanged();
}
public void addAll(List<File> newFiles) {
files.addAll(newFiles);
notifyDataSetChanged();
}
class Viewholder extends RecyclerView.ViewHolder {
View wholeThing;
ImageView fileIcon;
TextView fileName;
public Viewholder(View itemView) {
super(itemView);
wholeThing = itemView;
fileIcon = (ImageView) itemView.findViewById(R.id.file_icon);
fileName = (TextView) itemView.findViewById(R.id.file_name);
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:我如何在活动中调用适配器的构造函数。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file_viewer);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
}
@Override
public void onResume() {
adapter = new Adapter(this, /*A list of File items*/);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
Run Code Online (Sandbox Code Playgroud)
我所看到的问题是,notifyDataSetChanged 不会出现在 UiThread 中,所以如果像这样运行你的 setData 方法,它将起作用。
this.runOnUiThread(new Runnable() {
public void run() {
mAdapter.setNewData(newDataListForAdapter);
mAdapter.notifyDataSetChanged();
}
});
Run Code Online (Sandbox Code Playgroud)
听起来很愚蠢,在为 recyclerView 设置数据后调用这行代码,帮助我解决了这个问题:
recyclerView.smoothScrollToPosition(0)
Run Code Online (Sandbox Code Playgroud)
PS:我使用的可能与此有关的技术是:RJava、Retrofit2、NavigationUI、Fragments、LiveData 和 Databinding。
如果您在构造函数上设置数据,则适配器可能永远不会收到有关插入数据的通知。尝试在构造函数的底部或外部添加一个notifyDataSetChanged()(或调用您的setData方法)
| 归档时间: |
|
| 查看次数: |
2899 次 |
| 最近记录: |