我正在尝试对CardView进行动画处理,但是onTouch方法不起作用。视图没有注意到触摸,但是在android studio中有显示以下信息:
自定义视图
MaterialCardView
调用了setOnTouchListener,但没有覆盖performClick less ...(Strg + F1) 如果覆盖onTouchEvent或使用OnTouchListener的View也没有实现performClick并在检测到点击时调用它,则该View可能无法正确处理可访问性操作。理想情况下,应该将处理单击动作的逻辑放在View#performClick中,因为某些可访问性服务应在发生单击动作时调用performClick。问题ID:ClickableViewAccessibility
我添加了以下内容: 所以我添加了这个东西:
@SuppressLint("ClickableViewAccessibility")
但是无论如何它都行不通,但这也许是错误的原因。
这是我的CardViewAdapter.class
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.support.annotation.NonNull;
import android.support.design.card.MaterialCardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
public class CardViewAdapter extends RecyclerView.Adapter<CardViewAdapter.CardViewHolder> {
private ArrayList<HashMap<String, String>> listItems;
private Context context;
private Resources resources;
public CardViewAdapter(ArrayList<HashMap<String, String>> listItems, Context context, Resources resources) {
this.listItems = listItems;
this.context = context;
this.resources = resources;
}
@NonNull
@Override
public CardViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int position) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_illness_card, parent, false);
return new CardViewHolder(layoutView);
}
@Override
public void onBindViewHolder(@NonNull CardViewHolder cardViewHolder, final int position) {
final HashMap<String, String> listItem = listItems.get(position);
int resID = resources.getIdentifier(listItem.get("image_1"), "drawable", context.getPackageName());
cardViewHolder.illnessTitle.setText(listItem.get("title_1"));
cardViewHolder.illnessSubtitle.setText(listItem.get("freetime_1"));
cardViewHolder.illnessEngagement.setText("");
cardViewHolder.illnesImage.setBackgroundResource(resID);
cardViewHolder.cardView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Toast.makeText(context, "Clicked!", Toast.LENGTH_SHORT).show();
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
v.setScaleX(0.95f);
v.setScaleY(0.95f);
} else if (action == MotionEvent.ACTION_UP) {
v.animate().cancel();
v.animate().scaleX(1f).setDuration(1000).start();
v.animate().scaleY(1f).setDuration(1000).start();
}
return false;
}
});
cardViewHolder.linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, ViewPagerPreviewActivity.class);
intent.putExtra("position", position);
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return listItems.size();
}
public class CardViewHolder extends RecyclerView.ViewHolder {
ImageView illnesImage;
TextView illnessTitle;
TextView illnessSubtitle;
LinearLayout linearLayout;
TextView illnessEngagement;
MaterialCardView cardView;
public CardViewHolder(@NonNull View itemView) {
super(itemView);
cardView = itemView.findViewById(R.id.materialCard);
illnesImage = itemView.findViewById(R.id.illness_image);
illnessTitle = itemView.findViewById(R.id.illness_title);
illnessSubtitle = itemView.findViewById(R.id.illness_description);
linearLayout = itemView.findViewById(R.id.linearLayout);
illnessEngagement = itemView.findViewById(R.id.illness_engagement);
}
}
}
如果有人可以帮助我,那将很酷! :)
答案 0 :(得分:0)
如文档所述在视图上实现“ OnTouchListener”时,应手动调用“ performClick()”方法;好?因此您将代码更改为此:
cardViewHolder.cardView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Toast.makeText(context, "Clicked!", Toast.LENGTH_SHORT).show();
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
v.setScaleX(0.95f);
v.setScaleY(0.95f);
} else if (action == MotionEvent.ACTION_UP) {
v.animate().cancel();
v.animate().scaleX(1f).setDuration(1000).start();
v.animate().scaleY(1f).setDuration(1000).start();
// Here for example
performClick();
}
return false;
}
});
何时调用点击就在您手中!在这里我说过,当action == MotionEvent.ACTION_UP
时调用click答案 1 :(得分:0)
public class CardViewHolder extends RecyclerView.ViewHolder implements View.OnTouchListener {
ImageView illnesImage;
TextView illnessTitle;
TextView illnessSubtitle;
LinearLayout linearLayout;
TextView illnessEngagement;
MaterialCardView cardView;
public CardViewHolder(@NonNull View itemView) {
super(itemView);
cardView = itemView.findViewById(R.id.materialCard);
illnesImage = itemView.findViewById(R.id.illness_image);
illnessTitle = itemView.findViewById(R.id.illness_title);
illnessSubtitle = itemView.findViewById(R.id.illness_description);
linearLayout = itemView.findViewById(R.id.linearLayout);
illnessEngagement = itemView.findViewById(R.id.illness_engagement);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// calling toast should be specific not for all the event's
//Toast.makeText(context, "Clicked!", Toast.LENGTH_SHORT).show();
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
Toast.makeText(context, "Clicked!", Toast.LENGTH_SHORT).show(); // adding toast here means user has pressed the card.
v.setScaleX(0.95f);
v.setScaleY(0.95f);
} else if (action == MotionEvent.ACTION_UP) {
v.animate().cancel();
v.animate().scaleX(1f).setDuration(1000).start();
v.animate().scaleY(1f).setDuration(1000).start();
}
return true; //this should be true to get further event's
}
}
我已设置onTouchListener inside ViewHolder
来保持代码干净。您从false
返回了onTouch()
,这是错误。一旦返回 false, 来自 onTouch()(即ACTION_DOWN
),那么您将不会再遇到其他事件,因此从onTouch()
return true 继续接受活动的举办。
注意:以下警告可以忽略,无需添加任何注释。
自定义视图MaterialCardView调用了setOnTouchListener,但是 不会覆盖performClick less ...(Strg + F1)如果 覆盖onTouchEvent或使用OnTouchListener也不 实现performClick并在检测到点击时调用它, 可能无法正确处理辅助功能。逻辑处理 理想情况下,应将点击动作作为某些内容放置在View#performClick中 可访问性服务在应执行单击操作时调用performClick 发生。问题ID:ClickableViewAccessibility