我不知道我在这里做错了什么。
我的适配器如下所示:
public class TestAdapter extends RecyclerView.Adapter<TestAdapter.ViewHolder> {
private final List<String> sourceData;
public TestAdapter(List<String> sourceData) {
this.sourceData = sourceData;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.spanitem_goods, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
String item = sourceData.get(position);
holder.textView.setText(item);
}
@Override
public int getItemCount() {
return sourceData.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.spanitem_imageView)
ImageView imageView;
@BindView(R.id.spanitem_textView)
TextView textView;
public ViewHolder(View v) {
super(v);
ButterKnife.bind(this, v);
}
}
}
在我的 Activity 中的某个地方,我正在处理具有相同上述适配器的不同 fragment 。它可以很好地交换它们,但是一旦我在适配器上调用它
adapter.notifyItemInserted(position);
并再次交换 fragment ,它不再加载任何布局。只是一个空白页,只有当我调用上述方法时才会发生这种情况。
我的抽象BaseFragment
在onCreateView
中有这个
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "... onCreateView() called");
View v = inflater.inflate(getLayoutResourceId(), container, false);
unbinder = ButterKnife.bind(this, v);
ButterKnife.setDebug(true);
return v;
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
Log.d(TAG, "... onDestroyView() called");
}
最佳答案
嗯,我发现了这个问题。似乎与入门问题无关,但我可能会帮助其他人。
我使用了Observer
模式,并在非ui线程中运行了update
方法。看来这就是问题所在,因为除非我手动滚动,否则 RecyclerView
永远不会更新。
@Override
public void update(final String epc) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
sourceData.add(epc);
notifyDataSourceChanged(sourceData.size() - 1);
}
});
}
关于java - 修改适配器后 Butterknife 未加载布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39164618/