【发布时间】:2022-01-02 22:10:06
【问题描述】:
我正在编写我的 java 代码,因为我想在单击 mainactivity 工具栏中的未读按钮时将字体从正常更改为粗体。
当我尝试这个时:
public void Unread(int position) {
notifyItemChanged(position);
}
它不会在适配器的回收器视图中将字体从正常更改为粗体。
这是适配器的代码:
public class InboxAdapter extends RecyclerView.Adapter<InboxAdapter.ViewHolder> {
public InboxAdapter adapter;
public Fragment _fragment;
public RecyclerView recyclerView;
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
// change the font style depending on message read status
applyReadStatus(holder, inbox);
holder.actionView.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onClick(View v) {
/*GradientDrawable gd = new GradientDrawable();
gd.setColor(Color.parseColor("#e8f0fd"));
gd.setCornerRadius(40);
gd.setStroke(40, Color.parseColor("#e8f0fd"));
holder.layout1.setBackgroundColor(Color.parseColor("#e8f0fd"));
holder.layout1.setBackgroundDrawable(gd);*/
int idx = holder.getAdapterPosition();
String id = mInboxes.get(idx).getId();
String short_name = holder.tvIcon.getText().toString();
String subject1 = mInboxes.get(idx).getSubject();
String from = mInboxes.get(idx).getFrom();
String from_email = mInboxes.get(idx).getEmail();
String datetime = mInboxes.get(idx).getDate();
String isRead = mInboxes.get(idx).getRead();
String isImportant = mInboxes.get(idx).isImportant();
// set the read status
setReadStatus(holder, isRead);
openEmailActivity(mailbox, id, idx, color, short_name, subject1, from, from_email, datetime, isImportant);
/* gd.setColor(Color.parseColor("#FFFFFF"));
gd.setCornerRadius(40);
gd.setStroke(40, Color.parseColor("#FFFFFF"));
holder.layout1.setBackgroundColor(Color.parseColor("#FFFFFF"));
holder.layout1.setBackgroundDrawable(gd);*/
}
});
}
public void applyReadStatus(ViewHolder holder, inbox inbox) {
if (inbox.getRead().equals("read")) {
holder.from.setTypeface(null, Typeface.NORMAL);
holder.subject.setTypeface(null, Typeface.NORMAL);
holder.from.setTextColor(ContextCompat.getColor(mContext, R.color.subject));
holder.subject.setTextColor(ContextCompat.getColor(mContext, R.color.message));
holder.time.setTextColor(ContextCompat.getColor(mContext, R.color.timestamp));
holder.time.setTypeface(null, Typeface.NORMAL);
} else {
holder.from.setTypeface(null, Typeface.BOLD);
holder.subject.setTypeface(null, Typeface.BOLD);
holder.from.setTextColor(ContextCompat.getColor(mContext, R.color.from));
holder.subject.setTextColor(ContextCompat.getColor(mContext, R.color.subject));
holder.time.setTextColor(ContextCompat.getColor(mContext, R.color.timestamp));
holder.time.setTypeface(null, Typeface.BOLD);
}
}
public void Unread(int position) {
notifyItemChanged(position);
}
public void openEmailActivity(String mailbox, String id, int idx, int color, String short_name, String subject1, String from, String from_email, String datetime, String isImportant) {
Intent intent = new Intent(mContext, MainActivity5.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("mailbox", mailbox);
intent.putExtra("id", id);
intent.putExtra("idx", idx);
intent.putExtra("bg_color", color);
intent.putExtra("short_name", short_name);
intent.putExtra("subject", subject1);
intent.putExtra("from_sender", from);
intent.putExtra("from_email", from_email);
intent.putExtra("datetime", datetime);
intent.putExtra("is_important", isImportant);
_fragment.startActivityForResult(intent, MainActivity2.REQ_EMAIL_OPEN);
}
}
InboxFragment 的代码如下:
public class InboxFragment extends Fragment {
public static final int REQ_EMAIL_OPEN = 3456;
private RecyclerView mRecyclerView;
private SwipeRefreshLayout mSwipeLayout;
private InboxAdapter mInboxAdapter;
private List<inbox> mInbox = new ArrayList<>();
private inbox Inbox;
private Call<List<inbox>> call;
private Window window;
private Toolbar toolbar;
private FloatingActionButton fab;
private FloatingActionButton fab1;
private String contacts = null;
private String mailbox = null;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_inbox, container, false);
mRecyclerView = (RecyclerView) root.findViewById(R.id.recyclerView);
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQ_EMAIL_OPEN){
if(resultCode == Activity.RESULT_OK && data!= null){
boolean isEmailDeleted = data.getBooleanExtra("isEmailDeleted",false);
boolean isEmailUnread = data.getBooleanExtra("isEmailUnread",false);
boolean isEmailRead = data.getBooleanExtra("isEmailRead",false);
int emailId = data.getIntExtra("emailId",-1);
if (isEmailDeleted && emailId != -1) {
mInboxAdapter.removeItem(emailId);
}
else if (isEmailUnread && emailId != -1) {
mInboxAdapter.Unread(emailId);
}
else if (isEmailRead && emailId != -1) {
}
}
}
}
}
您能告诉我一个示例,我如何将from、subject 和time 的这些文本视图的字体从普通更改为粗体??
【问题讨论】:
标签: java android android-studio android-recyclerview android-adapter