我有一个RecyclerView,在一个Fragment里显示ArrayList中的项目。
这些项目有一个必须每秒更新的字符串。
如何在每秒更新所有ArrayList项目中的字符串并立即在RecyclerView中显示更改?
RecyclerView显示的Fragment:
public class RecyclerViewFragment extends Fragment{
DbActivity db;
Cursor fullData;
View v;
ConstraintLayout addDeadline;
CardView empty;
RecyclerView recyclerView;
List<Deadline> deadlines;
DeadlineAdapter deadlineAdapter;
String id, summary, date, time, deadline, labels;
public RecyclerViewFragment() { // Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_recycler_view, container, false);
setBasicView();
addDeadline();
return v;
}
public void setBasicView(){
recyclerView = v.findViewById(R.id.recycler_view);
empty = v.findViewById(R.id.empty);
db = new DbActivity(getContext());
fullData = db.getAllData();
if (fullData.getCount() > 0){
if (fullData.moveToFirst()) {
deadlines = new ArrayList<>();
int i = 1;
do {
String check = fullData.getString(5);
if (!check.startsWith("co")){
id = fullData.getString(1);
summary = fullData.getString(2);
date = fullData.getString(3);
time = fullData.getString(4);
deadline = fullData.getString(5);
labels = fullData.getString(6);
deadlines.add(new Deadline(id, summary, date, time, deadline,
labels));
i++;
}
} while (fullData.moveToNext());
}
empty.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE);
}
else {
deadlines = new ArrayList<>();
recyclerView.setVisibility(View.GONE);
empty.setVisibility(View.VISIBLE);
}
deadlineAdapter = new DeadlineAdapter(getContext(), deadlines);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(deadlineAdapter);
}
public void addDeadline(){
addDeadline = v.findViewById(R.id.add_deadline);
addDeadline.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(getContext(), AddDeadlineActivity.class);
startActivityForResult(intent, Codes.INTENT_REQUEST_CODE);
}
});
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
mMessageReceiver,
new IntentFilter("AddDeadline"));
}
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
empty.setVisibility(View.GONE);
DbActivity db = new DbActivity(getContext());
Cursor newDeadline = db.getAllData();
int position = intent.getIntExtra("position", -1);
try {
if (position != -1){
newDeadline.moveToPosition(position);
id = newDeadline.getString(1);
summary = newDeadline.getString(2);
date = newDeadline.getString(3);
time = newDeadline.getString(4);
deadline = newDeadline.getString(5);
labels = newDeadline.getString(6);
deadlines.remove(position);
deadlines.add(position, new Deadline(String.valueOf(position), summary, date, time, deadline, labels));
}
else {
newDeadline.moveToLast();
id = newDeadline.getString(1);
summary = newDeadline.getString(2);
date = newDeadline.getString(3);
time = newDeadline.getString(4);
deadline = newDeadline.getString(5);
labels = newDeadline.getString(6);
deadlines.add(new Deadline(id, summary, date, time, deadline, labels));
}
}
catch (NullPointerException e){
newDeadline.moveToLast();
}
deadlineAdapter.notifyDataSetChanged();
recyclerView.setVisibility(View.VISIBLE);
}
};
@Override
public void onDestroy() {
// Unregister since the activity is about to be closed.
LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(mMessageReceiver);
super.onDestroy();
}
}
Custom Adapter:
public class DeadlineAdapter extends RecyclerView.Adapter<DeadlineAdapter.mViewHolder> {
Context context;
List<Deadline> deadlines;
public DeadlineAdapter(Context context, List<Deadline> deadlines) {
this.context = context;
this.deadlines = deadlines;
}
@Override
public mViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new mViewHolder(LayoutInflater.from(context).inflate(R.layout.recycler_view_item, parent, false));
}
@Override
public void onBindViewHolder(final mViewHolder holder, int position) {
holder.summary.setText(deadlines.get(position).getSummary());
holder.deadline.setText(deadlines.get(position).getDeadline());
holder.labels.setText(String.valueOf(deadlines.get(position).getLabels()));
holder.setItemClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
editDeadline(position);
}
});
}
private void editDeadline(int position) {
Intent intent = new Intent(context, AddDeadlineActivity.class);
intent.putExtra("id", deadlines.get(position).getId());
intent.putExtra("summary", deadlines.get(position).getSummary());
intent.putExtra("date", deadlines.get(position).getDate());
intent.putExtra("time", deadlines.get(position).getTime());
intent.putExtra("labels", deadlines.get(position).getLabels());
intent.putExtra("position", position);
context.startActivity(intent);
}
@Override
public int getItemCount() {
return deadlines.size();
}
public static class mViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
private ItemClickListener itemClickListener;
private TextView summary;
private TextView deadline;
private TextView labels;
public mViewHolder(View itemView) {
super(itemView);
summary = itemView.findViewById(R.id.customSummary);
deadline = itemView.findViewById(R.id.customDeadline);
labels = itemView.findViewById(R.id.customLabels);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
}
public void setItemClickListener(ItemClickListener itemClickListener){
this.itemClickListener = itemClickListener;
}
@Override
public void onClick(View v) {
itemClickListener.onClick(v, getAdapterPosition(), false);
}
@Override
public boolean onLongClick(View v) {
itemClickListener.onClick(v, getAdapterPosition(), true);
return true;
}
}
}
任何帮助都将不胜感激。
Timer
,并设置1秒的重复时间。在RecyclerViewAdapter中编写一个更新文本的函数,然后调用notifydatasetchanged
。 - mhsankar