8

में स्पर्श पर कोई लहर प्रभाव नहीं है मेरे पास चार ग्रिड तत्वों (2 * 2) के साथ एक पुनर्नवीनीकरण है जो मेनू की तरह काम करता है। हालांकि, जब मैं उन पर क्लिक करता हूं तो कोई लहर प्रभाव नहीं दिखाया जाता है। यह मुझे किसी भी दृश्य पुष्टि के बिना अगली गतिविधि में ले जाता है कि दृश्य दबाया गया था। क्या कोई मदद कर सकता है?पुनर्नवीनीकरण

MainActivity

public class MainActivity extends AppCompatActivity implements MainMenuAdapter.OnItemClickListener { 

    Toolbar toolbar; 
    private static List<ViewModel> tileItems = new ArrayList<>(); 

    static { 
     tileItems.add(new ViewModel("Activity", "#3F51B5", R.drawable.activity)); 
     tileItems.add(new ViewModel("Profile", "#E91E63", R.drawable.profile)); 
     tileItems.add(new ViewModel("Training", "#FF5722", R.drawable.training)); 
     tileItems.add(new ViewModel("Diet", "#4CAF50", R.drawable.diet)); 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView); 
     recyclerView.setLayoutManager(new GridLayoutManager(this, 2)); 
     MainAdapter adapter = new MainAdapter(tileItems, MainActivity.this); 
     recyclerView.setAdapter(adapter); 
     adapter.setOnItemClickListener(this); 


     // Toolbar 
     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

    } 

    @Override 
    public void onBackPressed() { 
     this.moveTaskToBack(true); 
//  this.finishAffinity(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override public void onItemClick(View view, ViewModel viewModel) { 
    } 
} 

MainAdapter

public class MainAdapter extends RecyclerView.Adapter<MainMenuAdapter.ViewHolder> implements View.OnClickListener { 

    private List<ViewModel> items; 
    private OnItemClickListener onItemClickListener; 
    private Context context; 

    // Adapter constructor 
    public MainAdapter(List<ViewModel> items, Context context) { 
     this.items = items; 
     this.context = context; 
    } 

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) { 
     this.onItemClickListener = onItemClickListener; 
    } 

    @Override 
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

     View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_item, parent, false); 
     return new ViewHolder(v); 
    } 

    @Override 
    public void onBindViewHolder(ViewHolder viewHolder, int position) { 

     final ViewModel dataItem = items.get(position); 
     viewHolder.colorBlock.setBackgroundColor(dataItem.getColor()); 
     viewHolder.menuName.setText(dataItem.getName()); 
     viewHolder.menuIcon.setImageResource(dataItem.getImage()); 
     viewHolder.itemView.setTag(dataItem); 
     if (dataItem.getActivity() != null) { 
      viewHolder.colorBlock.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        Intent i = new Intent(context, dataItem.getActivity()); 
        context.startActivity(i); 

       } 
      }); 
     } 
    } 

    @Override 
    public int getItemCount() { 
     return items.size(); 
    } 

    @Override public void onClick(final View v) { 
     // Give some time to the ripple to finish the effect 
     if (onItemClickListener != null) { 
      new Handler().postDelayed(new Runnable() { 
       @Override public void run() { 
        onItemClickListener.onItemClick(v, (ViewModel) v.getTag()); 
       } 
      }, 200); 
     } 
    } 

    /** This is our ViewHolder class */ 
    public static class ViewHolder extends RecyclerView.ViewHolder { 

     public TextView menuName; 
     public View colorBlock; 
     public ImageView menuIcon; 

     public ViewHolder(View convertView) { 
      super(convertView); // Must call super() first 

      menuName = (TextView) convertView.findViewById(R.id.menuName); 
      colorBlock = (View) convertView.findViewById(R.id.colorBlock); 
      menuIcon = (ImageView) convertView.findViewById(R.id.menuItem); 
     } 
    } 

    public interface OnItemClickListener { 

     void onItemClick(View view, ViewModel viewModel); 

    } 
} 

activity_main.xml

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity"> 

    <include 
     android:id="@+id/toolbar" 
     layout="@layout/toolbar" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent"/> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recyclerView" 
     android:layout_below="@id/toolbar" 
     android:layout_width= "match_parent" 
     android:layout_height = "match_parent" /> 

</RelativeLayout> 

main_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:clickable="true" 
    android:focusable="true" 
    android:foreground="?android:attr/selectableItemBackground"> 

    <View 
     android:id="@+id/colorBlock" 
     android:layout_width="match_parent" 
     android:layout_height="170dp" /> 

    <ImageView 
     android:id="@+id/menuItem" 
     android:layout_width="120dp" 
     android:layout_height="120dp" 
     android:layout_centerHorizontal="true" 
     android:padding="16dp" 
     /> 

    <TextView 
     android:id="@+id/menuName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="16dp" 
     android:layout_centerHorizontal="true" 
     android:layout_alignParentBottom="true" 
     android:textColor="@android:color/white" 
     android:textSize="16sp"/> 
</RelativeLayout> 
+0

लहर स्वचालित नहीं होती है, यह सिर्फ एक ड्रायबल है, आपको जाना है और इसे अपने आप में कुछ दृश्य में रखना है। https://developer.android.com/reference/android/graphics/drawable/RippleDrawable.html – Budius

उत्तर

13

लहर प्रभाव प्रभावी होगा (@ बुडियस उसकी टिप्पणी में गलत है) यदि आप main_item.xml की पृष्ठभूमि को ?android:selectableItemBackground या ?selectableItemBackground पर सेट करते हैं। मुझे दूसरे के संदर्भ मिले। हालांकि, एंड्रॉइडस्टूडियो ने चेतावनी दी है कि यह com.android.support:design में एक निजी था। उस निजी संस्करण का उपयोग करने का प्रयास करते समय मेरा ऐप दुर्घटनाग्रस्त हो गया। मैंने "एंड्रॉइड:" उपसर्ग, और वॉयला के साथ पहले व्यक्ति पर अनुमान लगाया, यह काम करता है।

शायद आप android:foreground दुर्घटना से सेट कर रहे हैं? मैंने कोशिश की लेकिन मेरे RecyclerView आइटम के साथ कुछ भी नहीं हुआ।

आपका अपडेट किया गया RelativeLayout होगा:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:clickable="true" 
android:focusable="true" 
android:foreground="?android:attr/selectableItemBackground" 
android:background="?android:selectableItemBackground"> 

मैं भी android:background="?android:attr/selectableItemBackground" काम देखा है लहर आरंभ करने के लिए। मैं यह भी उल्लेख करूंगा कि selectableItemBackground को main_item.xml के मूल तत्व पर होने की आवश्यकता नहीं है। मैं रूट आइटम में पृष्ठभूमि रंग का उपयोग कर रहा हूं, फिर नेस्टेड व्यू ग्रुप पर selectableItemBackground सेट कर रहा हूं।

मेरा उत्तर सामग्री डिजाइन एपकोपेट का उपयोग न करने के संदर्भ में एक फ्रेम से आता है। मुझे संदेह है कि यदि आप भौतिक डिज़ाइन एपकंपैट समर्थन लाइब्रेरी का उपयोग कर रहे हैं तो कोई अंतर है।

संबंधित मुद्दे