在没有dataChange的情况下仅通过UI元素更新RecyclerView项

时间:2019-02-12 11:36:06

标签: java android android-recyclerview recycler-adapter

我有一个事件时间轴,想用一个活动,in_active和pre_active标记更新时间轴标记。问题是我必须“重新启动”片段以查看标记更改。是否有可能看到更改的实时信息?

事件具有时间戳和持续时间。如果当前事件处于活动状态,则我想显示active_marker,如果事件结束,则in_active标记,以后的事件将获得future_marker。

适配器:

@Override
public void onBindViewHolder(@NonNull TimeLineViewHolder holder, int position) {
    TimeLineData timeLineData = feedList.get(position);

    Calendar currentDate = Calendar.getInstance();

    Calendar endDate = (Calendar) timeLineData.getRealTime().clone();
    endDate.add(Calendar.MINUTE, 5);   // Event Duration 

    if (currentDate.after(endDate)) {
        holder.timelineView.setMarker(holder.itemView.getContext().getDrawable(R.drawable.ic_marker_inactive));
    }
    if (currentDate.after(timeLineData.getRealTime()) && currentDate.before(endDate)) {
        holder.timelineView.setMarker(holder.itemView.getContext().getDrawable(R.drawable.ic_marker_active));
    }
    if (currentDate.before(timeLineData.getRealTime())) {
        holder.timelineView.setMarker(holder.itemView.getContext().getDrawable(R.drawable.ic_marker_future));
    }

    holder.title.setText(timeLineData.getStartup1() + " VS " + timeLineData.getStartup2());
    holder.time.setText(timeLineData.getTime()); 
}

timeLineData.getTime是格式化的时间字符串。

timeLineData.getRealTime是日历对象。

片段:

public class TimeLineFragment extends Fragment {
private List<TimeLineData> dataList = new ArrayList<>();

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View timeLineView = inflater.inflate(R.layout.timeLine_recycler, container, false);
    RecyclerView recyclerView = timeLineView.findViewById(R.id.recyclerView);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), RecyclerView.VERTICAL, false);
    recyclerView.setLayoutManager(linearLayoutManager);
    setUpEntries(recyclerView);
    return timeLineView;
}
private void setUpEntries(RecyclerView recyclerView) {
     TimeLineData model1 = new TimeLineData();
     ...
     model1.setTime(2019, Calendar.FEBRUARY, 12, 10, 0);

setTime()获得诸如(年,月,日,小时,分钟)之类的数据

0 个答案:

没有答案