当你单击ListView项目时,它会通过播放一些动画来响应,例如传播波,使项目背景灰色一段时间。从而用户可以看到点击了哪个项目。在我的RecyclerView上,我已经实现了,目前没有这样的动画。我实现了OnclickListener,当点击项目时,app会执行所有任务,但它缺少这个简单的动画。有人可以帮我这个吗?
MainActivity:
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerViewAdapter adapter;
RecyclerView.LayoutManager layoutManager;
String[] data = {"Cola", "Sprite", "Pizza", "Coffee", "Salt", "Pepper", "KitKat"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
recyclerView = (RecyclerView) findViewById(R.id.myRecyclerView);
adapter = new RecyclerViewAdapter(data, this);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
RecyclerView.ItemAnimator itemAnimator = new DefaultItemAnimator();
recyclerView.setItemAnimator(itemAnimator);
}
}
适配器类:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
private String[] data;
public Context context;
public RecyclerViewAdapter(String[] input, Context c) {
data = input;
context = c;
}
public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public MyOnItemClickListener listener;
public TextView mTextView;
public Context cont;
public MyViewHolder(View v, MyOnItemClickListener l, Context c) {
super(v);
mTextView = (TextView) v.findViewById(R.id.info_text);
listener = l;
cont = c;
v.setOnClickListener(this);
}
@Override
public void onClick(View v) {
listener.startActivity(cont);
}
public static interface MyOnItemClickListener {
public void startActivity(Context c);
}
}
@Override
public RecyclerViewAdapter.MyViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
final View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_view_item, parent, false);
MyViewHolder vh = new MyViewHolder(v, new MyViewHolder.MyOnItemClickListener() {
@Override
public void startActivity(Context c) {
Toast.makeText(c, "Item Pressed!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(c, ItemInformationActivity.class);
intent.putExtra("Text", ((TextView) v.findViewById(R.id.info_text)).getText().toString());
c.startActivity(intent);
}
}, context);
return vh;
}
@Override
public void onBindViewHolder(RecyclerViewAdapter.MyViewHolder holder, int position) {
holder.mTextView.setText(data[position]);
}
@Override
public int getItemCount() {
return data.length;
}
}
list_view_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="3dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp">
<ImageView
android:id="@+id/img_android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="20dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/info_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginTop="10dp"
android:layout_toRightOf="@+id/img_android"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
</android.support.v7.widget.CardView>
答案 0 :(得分:1)
您指的是RippleDrawable
RippleDrawable | Android Developers
您可以将其作为列表项的背景;只需确保/res/layout-v21
变体中的RippleDrawable
仅适用于Lollipop及其后续版本。
答案 1 :(得分:1)
我终于想出了如何进行触摸反馈。克里斯拉尔森说得对,我需要涟漪效应。但是我添加了这个:
android:focusable="true"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
我不需要定制涟漪。只有默认值。这现在工作正常!