當您單擊ListView項目時,它會通過播放一些動畫,如傳播波讓項目的背景灰色一段時間。從而用戶看到哪個項目被點擊。在我的RecyclerView中,我已經實現了,目前沒有任何這樣的動畫。我已經實現了OnclickListener,應用程序在單擊項目時執行所有任務,但缺少這種簡單的動畫。任何人都可以幫助我嗎?單擊RecyclerView項目時ListView中的波紋響應
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>
你膨脹的xml「list_view_item」,你創建了這個嗎?或者是android庫自帶的默認xml,如果你創建了這個,請把你的代碼 –
@MaxPinto我創建一個自定義的list_item佈局。已添加 – Marat