我有一个带有RecyclerView的片段,我想在从IntentService获取一些数据后更新。
服务使用Otto
EventBus完成并将数据发送到Fragment。我可以确认原因是我添加了一些日志消息并打印了一些值,例如:dataSet.get(0).getName().
之后我拨打adapter.notifyDataSetChanged()
,但屏幕上没有任何内容。
如果您认为它可以提供帮助,我可以为您提供自定义适配器的代码:
public class FilteredVideoListAdapter extends RecyclerView.Adapter<FilteredVideoListAdapter.ViewHolder> {
public static final String TAG = "FILTEREDVIDEO-ADAPTER";
public List<FilteredVideo> data;
public Context context;
public FilteredVideoListAdapter(List<FilteredVideo> data) {
this.data = data;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView tvTitle;
TextView tvSubtitle;
ImageView imageView;
public ViewHolder(View itemView) {
super(itemView);
tvTitle = (TextView) itemView.findViewById(R.id.tv_filtered_video_title);
tvSubtitle = (TextView) itemView.findViewById(R.id.tv_filtered_video_subtitle);
imageView = (ImageView) itemView.findViewById(R.id.iv_filtered_video_image);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
context = parent.getContext();
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_filtered_video, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
viewHolder.tvTitle.setText(data.get(position).getName());
viewHolder.tvSubtitle.setText(data.get(position).getDataUrl());
int dimen = context.getResources().getDimensionPixelSize(R.dimen.filtered_video_image_size);
Picasso.with(context)
.load(data.get(position).getThumbnailUrl())
.resize(dimen, dimen)
.into(viewHolder.imageView);
}
@Override
public int getItemCount() {
return data.size();
}
}
Fragment类的代码:
public class FilteredVideoListFragment extends BaseFragment {
private static final String TAG = "FILTEREDVLIST-FRAGMENT";
public RecyclerView recyclerView;
protected FilteredVideoListAdapter adapter;
protected RecyclerView.LayoutManager layoutManager;
public List<FilteredVideo> dataSet;
//first time there aren't any populated data
private boolean datasetIsNull = true;
public static FilteredVideoListFragment newInstance() {
return new FilteredVideoListFragment();
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dataSet = new ArrayList<>();
BusProvider.getInstance().register(this);
// startService here -
Intent intentService = new Intent(activity, GetMostPopularIntentService.class);
activity.startService(intentService);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_filtered_video_list, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.rv_filtered_video_list);
recyclerView.setHasFixedSize(true);
recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(),
DividerItemDecoration.VERTICAL_LIST));
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
adapter = new FilteredVideoListAdapter(dataSet);
recyclerView.setAdapter(adapter);
return view;
}
private BroadcastReceiver mostpopularReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String resultCode = intent.getStringExtra("resultCode");
if ("OK".equals(resultCode)) {
Toast.makeText(activity, "Most Popular Results Received!", Toast.LENGTH_SHORT).show();
}
}
};
/** Register/ unRegister Receiver */
@Override
public void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter(GetMostPopularIntentService.ACTION);
LocalBroadcastManager.getInstance(activity).registerReceiver(mostpopularReceiver, intentFilter);
}
@Override
public void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(activity).unregisterReceiver(mostpopularReceiver);
}
/**
* Receives Event from GetMostPopularIntentService when data retrieved
* successfully.
*/
@Subscribe
public void onMostPopularReceivedEvent(MostPopularReceivedEvent event) {
Log.i(TAG, "clickevent ");
dataSet = event.getFilteredVideoList();
Log.i(TAG, "NOTIFY ADAPTER SIZE == " + dataSet.size() );
Log.i(TAG, "NOTIFY ADAPTER SIZE == " + dataSet.get(0).getName() );
adapter.notifyDataSetChanged();
Log.i(TAG, "AFTER NOTIFY ADAPTER ");
}
@Subscribe
public void onToastRequest(ToastRequest request) {
Log.i(TAG, "onToastRequest ");
Toast.makeText(activity, request.message, request.duration).show();
}
}
答案 0 :(得分:0)
尝试 -
@Subscribe
public void onMostPopularReceivedEvent(MostPopularReceivedEvent event) {
adapter = new FilteredVideoListAdapter(event.getFilteredVideoList());
recyclerView.setAdapter(adapter);
}
答案 1 :(得分:-1)
试试这个适配器
public class FilteredVideoListAdapter extends RecyclerView.Adapter<FilteredVideoListAdapter.ViewHolder> {
public static final String TAG = "FILTEREDVIDEO-ADAPTER";
public List<FilteredVideo> data;
public Context mContext;
public FilteredVideoListAdapter(Context context, List<FilteredVideo> data) {
this.data = data;
this.mContext = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_filtered_video, null);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder customViewHolder, int i) {
FilteredVideo filteredVideo = data.get(i);
customViewHolder.tvTitle.setText(filteredVideo.getName());
customViewHolder.tvSubtitle.setText(filteredVideo.getDataUrl());
int dimen = mContext.getResources().getDimensionPixelSize(R.dimen.filtered_video_image_size);
Picasso.with(mContext)
.load(filteredVideo.getThumbnailUrl())
.resize(dimen, dimen)
.into(customViewHolder.imageView);
}
@Override
public int getItemCount() {
return (null != data ? data.size() : 0);
}
}
并像你一样打电话给你的片段
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_filtered_video_list, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.rv_filtered_video_list);
recyclerView.setHasFixedSize(true);
recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(),
DividerItemDecoration.VERTICAL_LIST));
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
adapter = new FilteredVideoListAdapter(getActivity(), dataSet);
recyclerView.setAdapter(adapter);
return view;
}