Created
December 28, 2017 05:20
-
-
Save SalaSuresh/245f3497fd8a2ba31b9ff5ac09a00c32 to your computer and use it in GitHub Desktop.
Android RecyclerView exmaple
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical"> | |
<android.support.v7.widget.RecyclerView | |
android:id="@+id/recyclerview_demo" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal"> | |
<ImageView | |
android:id="@+id/image_picture" | |
android:layout_width="50dp" | |
android:layout_height="50dp" | |
android:padding="5dp" | |
android:src="@mipmap/ic_launcher_round" /> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="vertical" | |
android:padding="10dp"> | |
<TextView | |
android:id="@+id/text_name" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="Name" /> | |
<TextView | |
android:id="@+id/text_message" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="Message" /> | |
</LinearLayout> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.suresh.recyclerviewdemo; | |
import android.os.Bundle; | |
import android.support.annotation.Nullable; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.DividerItemDecoration; | |
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
import android.widget.Toast; | |
import com.dumadugames.pocketbowling.R; | |
import java.util.ArrayList; | |
/** | |
* Created by Suresh on 17-Oct-17. | |
*/ | |
public class MainActivity extends AppCompatActivity implements MyRecyclerViewAdapter.itemClickListener { | |
ArrayList<String> names = new ArrayList<>(); | |
ArrayList<String> message = new ArrayList<>(); | |
ArrayList<Integer> picture = new ArrayList<>(); | |
@Override | |
protected void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main_list); | |
RecyclerView recyclerView = findViewById(R.id.recyclerview_demo); | |
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this); | |
recyclerView.setLayoutManager(layoutManager); | |
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL)); | |
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL)); | |
for (int i = 0; i < 20; i++) { | |
names.add("name " + i); | |
message.add("message " + i); | |
picture.add(R.mipmap.ic_launcher_round); | |
} | |
MyRecyclerViewAdapter myRecyclerViewAdapter = new MyRecyclerViewAdapter(this, names, message, picture, this); | |
recyclerView.setAdapter(myRecyclerViewAdapter); | |
} | |
@Override | |
public void onItemClick(int position) { | |
Toast.makeText(this, names.get(position), Toast.LENGTH_SHORT).show(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.suresh.recyclerviewdemo; | |
import android.content.Context; | |
import android.os.Build; | |
import android.support.annotation.RequiresApi; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
import com.dumadugames.pocketbowling.R; | |
import java.util.ArrayList; | |
/** | |
* Created by Dumadu on 17-Oct-17. | |
*/ | |
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.MyViewHolder> { | |
ArrayList<String> names = new ArrayList<>(); | |
ArrayList<String> message = new ArrayList<>(); | |
ArrayList<Integer> picture = new ArrayList<>(); | |
Context context; | |
itemClickListener itemClickListener; | |
interface itemClickListener { | |
void onItemClick(int position); | |
} | |
MyRecyclerViewAdapter(Context context, ArrayList<String> names, ArrayList<String> message, ArrayList<Integer> picture, itemClickListener itemClickListener) { | |
this.context = context; | |
this.names = names; | |
this.message = message; | |
this.picture = picture; | |
this.itemClickListener = itemClickListener; | |
} | |
@Override | |
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_demo, parent, false); | |
return new MyViewHolder(view, itemClickListener); | |
} | |
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) | |
@Override | |
public void onBindViewHolder(MyViewHolder holder, int position) { | |
holder.textViewName.setText(names.get(position)); | |
holder.textViewMaessge.setText(message.get(position)); | |
holder.imageViewPicture.setImageDrawable(context.getDrawable(picture.get(position))); | |
} | |
@Override | |
public int getItemCount() { | |
return names.size(); | |
} | |
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { | |
TextView textViewName, textViewMaessge; | |
ImageView imageViewPicture; | |
itemClickListener itemClickListener; | |
public MyViewHolder(View itemView, itemClickListener itemClickListener) { | |
super(itemView); | |
textViewName = itemView.findViewById(R.id.text_name); | |
textViewMaessge = itemView.findViewById(R.id.text_message); | |
imageViewPicture = itemView.findViewById(R.id.image_picture); | |
this.itemClickListener = itemClickListener; | |
itemView.setOnClickListener(this); | |
} | |
@Override | |
public void onClick(View v) { | |
itemClickListener.onItemClick(getAdapterPosition()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment