我正在尝试对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);
}
}
}
如果有人可以帮助我,那将很酷! :)
最佳答案
如文档所述,在视图上实现“ 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
关于android - CardView onTouch onClick,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53907723/