这个问题已被问过几次,但这些答案并不适用于我。我想更一般地回答一般导致这个问题的原因。
我的活动布局中有一个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>
我在这个行结构中有几个要显示的项目。我正确设置了适配器。但是,当我运行我的应用程序时,屏幕上的文本视图不会显示,直到我向下滚动并再次向上滚动。基本上,为了显示那些文本视图,需要将它们从显示中丢弃并再次输入。这是我的适配器代码:
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);
}
}
}
编辑:我如何在活动上调用适配器的构造函数。
@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));
}
答案 0 :(得分:3)
我所看到的问题是,在UiThread中,notifyDataSetChanged不会出现问题,所以如果像这样运行你的setData方法就可以了。
this.runOnUiThread(new Runnable() {
public void run() {
mAdapter.setNewData(newDataListForAdapter);
mAdapter.notifyDataSetChanged();
}
});
答案 1 :(得分:2)
请试试这个。我希望它会有所帮助。
RecyclerView recyclerView;
FileViewAdapter adapter;
List<File> files = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file_viewer);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
adapter = new FileViewAdapter(this, files);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
@Override
public void onResume() {
super.onResume();
files = getFiles();//fetch files
adapter.notifyDataSetChanged();//update UI
}
public List<File> getFiles() {
//fetch files
//...
return files;
}
顺便说一句,我没有发现你的情况。您没有向我们展示的代码中可能存在其他一些错误。
答案 2 :(得分:2)
听起来很愚蠢,在为recyclerView设置数据后调用此代码行对我解决了这个问题:
# relational form widget
categories: # relationship name defined in the model
label: 'Associated categories'
span: full
required: 1
type: relation
nameFrom: title # db column name to fetch to show the display value
#taglist form widget
categories:
label: 'Associated categories'
span: full
mode: relation
required: 1
type: taglist
nameFrom: title
PS:我使用的可能与此有关的技术是:RJava,Retrofit2,NavigationUI,Fragments,LiveData和Databinding。
答案 3 :(得分:0)
如果您在构造函数上设置数据,则可能是适配器永远不会收到有关插入数据的通知。尝试在构造函数的底部或外部添加notifyDataSetChanged()(或调用setData方法)