2016-06-21 68 views
0

當您單擊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> 
+0

你膨脹的xml「list_view_item」,你創建了這個嗎?或者是android庫自帶的默認xml,如果你創建了這個,請把你的代碼 –

+0

@MaxPinto我創建一個自定義的list_item佈局。已添加 – Marat

回答

1

我終於想出瞭如何進行觸摸反饋。克里斯拉森說得對,我需要漣漪效應。不過我通過增加了這個:

android:focusable="true" 
android:clickable="true" 
android:foreground="?android:attr/selectableItemBackground" 

我並不需要定製波紋。只有默認值。這現在工作正常!

1

你是指一個RippleDrawableRippleDrawable | Android Developers

你可以把它作爲你的列表項目的背景;只要確保它在/res/layout-v21變種RippleDrawable只適用於棒棒糖和更高版本。

+0

謝謝!你給了我正確的方向 – Marat

相關問題