using this code in
compile 'com.android.support:cardview-v7:25.0.0'
compile 'com.android.support:recyclerview-v7:25.0.0'
dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.3.1'testCompile 'junit:junit:4.12'compile 'com.android.support:cardview-v7:25.0.0'compile 'com.android.support:recyclerview-v7:25.0.0'}
MainActivity.javapublic class MainActivity extends AppCompatActivity { private ArrayList<String> names; private RecyclerView recyclerView; private RecyclerView.Adapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { recyclerView = (RecyclerView) findViewById(R.id.recyid); recyclerView.setHasFixedSize(true); final RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext()); recyclerView.setLayoutManager(layoutManager); names = new ArrayList<>(); for (int i = 1; i <= 50; i++) { names.add(i + " name"); } adapter = new MyAdapter(names, this); recyclerView.setAdapter(adapter); recyclerView.setItemAnimator(new DefaultItemAnimator()); recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() { GestureDetector gestureDetector = new GestureDetector(getApplicationContext(), new GestureDetector.SimpleOnGestureListener() { @Override public boolean onSingleTapUp(MotionEvent e) { return true; } }); @Override public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { View child = rv.findChildViewUnder(e.getX(), e.getY()); if(child != null && gestureDetector.onTouchEvent(e)) { int position = rv.getChildAdapterPosition(child); Toast.makeText(getApplicationContext(), names.get(position), Toast.LENGTH_SHORT).show(); } return false; } @Override public void onTouchEvent(RecyclerView rv, MotionEvent e) { } @Override public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { } }); } }activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.edupointbd.amirul.recyclercardviewdemo.MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/recyid"android:layout_width="wrap_content"android:layout_height="wrap_content"> </android.support.v7.widget.RecyclerView> </LinearLayout>
cardviewrow.xml
--------------
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <android.support.v7.widget.CardView
android:id="@+id/cardid"
android:layout_width="match_parent"
android:layout_height="wrap_content"> <TextView
android:id="@+id/txname"
android:textSize="30sp"
android:text="hello"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> </android.support.v7.widget.CardView> </LinearLayout>
MyAdapter.java
------------------
/** * Created by Amirul on 07-Oct-17. */ public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { private ArrayList<String> names; private Context ct; MyAdapter(ArrayList<String> nana, Context context){ names = nana; ct =context; } @Override public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardlayout,parent,false); return new ViewHolder(view); } @Override public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) { holder.textView.setText(names.get(position)); } @Override public int getItemCount() { return names.size(); } public class ViewHolder extends RecyclerView.ViewHolder { TextView textView; public ViewHolder(View itemView) { super(itemView); textView = (TextView)itemView.findViewById(R.id.txname); } } }