我正在尝试在Recyclerview中的TextView上设置FORForeground,但有时当我向上滚动向下或反之亦然时,它无法正常工作。有关详细信息,请参阅屏幕截图。
我的适配器类代码:
public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.MessageViewHolder> {
private List<FriendlyMessageModel> friendlyMessageModelList;
private String android_id;
public Context context;
public ChatAdapter(Context context, List<FriendlyMessageModel> friendlyMessageModelList) {
this.friendlyMessageModelList = friendlyMessageModelList;
FireBaseSetup fireBaseSetup = new FireBaseSetup();
android_id = fireBaseSetup.getUID();
this.context = context;
}
@NonNull
@Override
public MessageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_message, parent, false);
return new MessageViewHolder(itemView);
}
@SuppressLint("ClickableViewAccessibility")
@Override
public void onBindViewHolder(final MessageViewHolder holder, int position) {
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
FriendlyMessageModel friendlyMessageModel = friendlyMessageModelList.get(position);
String author = friendlyMessageModel.getName();
boolean isPhoto = friendlyMessageModel.getPhotoUrl()!=null;
if(author.equals(android_id))
{
params.gravity = Gravity.RIGHT;
params1.gravity = Gravity.RIGHT;
}
else{
params.gravity = Gravity.LEFT;
params1.gravity = Gravity.LEFT;
}
if (params.gravity == Gravity.RIGHT) {
if(isPhoto)
{
holder.ivPhoto.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.rounded_corner1));
holder.ivPhoto.setVisibility(View.VISIBLE);
holder.tvMessage.setVisibility(View.GONE);
Glide.with(holder.ivPhoto.getContext())
.load(friendlyMessageModel.getPhotoUrl())
.into(holder.ivPhoto);
}
else {
holder.tvMessage.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.rounded_corner1));
holder.ivPhoto.setVisibility(View.GONE);
holder.tvMessage.setVisibility(View.VISIBLE);
//holder.tvMessage.setLayoutParams(params);
holder.tvMessage.setText(friendlyMessageModel.getText());
}
}else{
if(isPhoto)
{
holder.ivPhoto.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.rounded_corner2));
holder.ivPhoto.setVisibility(View.VISIBLE);
holder.tvMessage.setVisibility(View.GONE);
Glide.with(holder.ivPhoto.getContext())
.load(friendlyMessageModel.getPhotoUrl())
.into(holder.ivPhoto);
}
else {
holder.tvMessage.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.rounded_corner2));
holder.ivPhoto.setVisibility(View.GONE);
holder.tvMessage.setVisibility(View.VISIBLE);
//holder.tvMessage.setLayoutParams(params);
holder.tvMessage.setText(friendlyMessageModel.getText());
}
}
holder.tvAuthor.setLayoutParams(params);
holder.tvAuthor.setText(friendlyMessageModel.getName());
// TODO: 21/2/18 added by kd
final int color = Color.parseColor("#FADA5E");
final Drawable drawable = new ColorDrawable(color);
if(!friendlyMessageModel.isHide()){
holder.confidemsg.setForeground(drawable);
holder.confidemsg.setLayoutParams(params1);
// TODO: 21/2/18 added by kd confide feature
holder.confidemsg.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN: {
holder.confidemsg.setForeground(null);
return true; // if you want to handle the touch event
}
case MotionEvent.ACTION_UP: {
// RELEASED
holder.confidemsg.setForeground(drawable);
return true; // if you want to handle the touch event
}
case MotionEvent.ACTION_CANCEL: {
holder.confidemsg.setForeground(drawable);
return true;
}
}
return false;
}
});
// TODO: 21/2/18 added by kd confide feature
holder.confideimg.setForeground(drawable);
holder.confideimg.setLayoutParams(params1);
holder.confideimg.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN: {
// PRESSED
holder.confideimg.setForeground(null);
return true; // if you want to handle the touch event
}
case MotionEvent.ACTION_UP: {
// RELEASED
holder.confideimg.setForeground(drawable);
return true; // if you want to handle the touch event
}
case MotionEvent.ACTION_CANCEL: {
holder.confideimg.setForeground(drawable);
return true;
}
}
return false;
}
});
}
// Calendar currnetDateTime = Calendar.getInstance();
// SimpleDateFormat df = new SimpleDateFormat("HH:mm a");
// String currentTime = df.format(currnetDateTime.getTime());
//
// holder.tvTime.setLayoutParams(params);
// holder.tvTime.setText(currentTime);
}
@Override
public int getItemCount() {
return friendlyMessageModelList.size();
}
public class MessageViewHolder extends RecyclerView.ViewHolder {
ImageView ivPhoto;
TextView tvMessage, tvAuthor, tvTime;
// TODO: 21/2/18 added by kd
//CardView confide;
FrameLayout confidemsg, confideimg;
public MessageViewHolder(View itemView) {
super(itemView);
ivPhoto = itemView.findViewById(R.id.photoImageView);
tvMessage = itemView.findViewById(R.id.messageTextView);
tvAuthor = itemView.findViewById(R.id.nameTextView);
tvTime = itemView.findViewById(R.id.time);
//todo added by kd
confidemsg = itemView.findViewById(R.id.confidemsg);
confideimg = itemView.findViewById(R.id.confideimg);
}
}
}
我的问题很简单,如何解决第二个截图中的问题。每当我向上和向下滚动它总会显示出来。
答案 0 :(得分:0)
我认为问题是当您滚动RecyclerView
拦截TouchEvent
时,您将无法收到ACTION_UP
。
在项目上设置TouchListener
的最佳方法是将ItemTouchHelper
设置为RecyclerView
本身覆盖onChildDraw
方法。
答案 1 :(得分:0)
当你在recycler视图中向上和向下滚动时,recyclelerview适配器每次调用onBindViewHolder()方法。在textView上的颜色设置放在onBindViewHolder()中。
@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
}
答案 2 :(得分:0)
我做了一些简单的更改,只检查了底色,它运行良好,尝试下面的代码并测试它,并正在努力不改变scolling时间..
public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.MessageViewHolder> {
private List<String> friendlyMessageModelList;
private String android_id;
public Context context;
public ChatAdapter(Context context, List<String> friendlyMessageModelList) {
this.friendlyMessageModelList = friendlyMessageModelList;
//FireBaseSetup fireBaseSetup = new FireBaseSetup();
//android_id = fireBaseSetup.getUID();
this.context = context;
}
@NonNull
@Override
public MessageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_message, parent, false);
return new MessageViewHolder(itemView);
}
@SuppressLint({"ClickableViewAccessibility", "ResourceAsColor"})
@Override
public void onBindViewHolder(final MessageViewHolder holder, int position) {
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
String friendlyMessageModel = friendlyMessageModelList.get(position);
// String author = friendlyMessageModel.getName(); // boolean isPhoto = friendlyMessageModel.getPhotoUrl()!= null;
if (friendlyMessageModel.equals("black")) {
holder.tvTime.setBackgroundColor(R.color.colorAccent);
}
// Calendar currnetDateTime = Calendar.getInstance(); // SimpleDateFormat df = new SimpleDateFormat(&#34; HH:mm a&#34;); // String currentTime = df.format(currnetDateTime.getTime()); // // holder.tvTime.setLayoutParams(params); // holder.tvTime.setText(currentTime); }
@Override
public int getItemCount() {
return friendlyMessageModelList.size();
}
class MessageViewHolder extends RecyclerView.ViewHolder {
ImageView ivPhoto;
TextView tvTime;
// TODO: 21/2/18 added by kd
//CardView confide;
public MessageViewHolder(View itemView) {
super(itemView);
tvTime = itemView.findViewById(R.id.timeData);
//todo added by kd
}
}
}