Last active
August 13, 2017 07:15
-
-
Save m7mdra/a964d1220b3666d3300949cfeccda7d4 to your computer and use it in GitHub Desktop.
recyclerview with clicklistener
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"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context="test.com.bttest.MainActivity"> | |
<android.support.v7.widget.RecyclerView | |
android:id="@+id/recyclerView" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_centerHorizontal="true" | |
android:layout_centerVertical="true" /> | |
</RelativeLayout> |
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 test.com.bttest; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import java.util.ArrayList; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
/* | |
* our data | |
*/ | |
final ArrayList<Person> data = new ArrayList<>(); | |
data.add(new Person("1", "Mohammad")); | |
data.add(new Person("2", "Ahmed")); | |
data.add(new Person("3", "Khalid")); | |
data.add(new Person("4", "Mustafa")); | |
data.add(new Person("5", "Alla")); | |
data.add(new Person("6", "Abdo")); | |
data.add(new Person("7", "Mega")); | |
data.add(new Person("8", "Mike")); | |
data.add(new Person("9", "Hii")); | |
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView); | |
recyclerView.setLayoutManager(new LinearLayoutManager(this)); | |
Adapter adapter = new Adapter(data); | |
recyclerView.setAdapter(adapter); | |
adapter.notifyDataSetChanged(); | |
adapter.setClickListener(new ClickListener() { | |
@Override | |
public void onClick(int position, View view) { | |
Person person = data.get(position); | |
Toast.makeText(MainActivity.this, "Id: " + person.getId() + " Name: " + person.getName(), Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
} | |
class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> { | |
private ArrayList<Person> list; | |
private ClickListener mClickListener; | |
public Adapter(ArrayList<Person> people) { | |
list = people; | |
} | |
/** | |
* set click listener . | |
* | |
* @param clickListener | |
*/ | |
public void setClickListener(ClickListener clickListener) { | |
mClickListener = clickListener; | |
} | |
/** | |
* inflate the layout | |
* | |
* @param parent | |
* @param viewType | |
* @return ViewHolder | |
*/ | |
@Override | |
public Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout, parent, false)); | |
} | |
@Override | |
public void onBindViewHolder(final Adapter.ViewHolder holder, int position) { | |
Person person = list.get(position); | |
holder.container.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
if (mClickListener != null) | |
mClickListener.onClick(holder.getAdapterPosition(), v); //call click listener method | |
} | |
}); | |
holder.id.setText(person.getId()); | |
holder.name.setText(person.getName()); | |
} | |
/** | |
* @return 0 if list==null eles return list.size() | |
*/ | |
@Override | |
public int getItemCount() { | |
return list == null ? 0 : list.size(); | |
} | |
/** | |
* row class | |
*/ | |
public class ViewHolder extends RecyclerView.ViewHolder { | |
private TextView id; | |
private TextView name; | |
private View container; // container == row | |
public ViewHolder(View v) { | |
super(v); | |
container = v; | |
id = (TextView) v.findViewById(R.id.id); | |
name = (TextView) v.findViewById(R.id.name); | |
} | |
} | |
} | |
public interface ClickListener { | |
void onClick(int position, View view); | |
} | |
} |
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 test.com.bttest; | |
/** | |
* Created by m7mdra on 13/08/17. | |
*/ | |
public class Person { | |
private String id, name; | |
public Person(String id, String name) { | |
this.id = id; | |
this.name = name; | |
} | |
public String getId() { | |
return id; | |
} | |
public String getName() { | |
return name; | |
} | |
} |
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:foreground="?selectableItemBackground" | |
android:orientation="horizontal"> | |
<TextView | |
android:id="@+id/id" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="1" | |
android:layout_margin="10dp" | |
android:textColor="@android:color/black" | |
android:textSize="18sp" /> | |
<TextView | |
android:layout_margin="10dp" | |
android:layout_weight="1" | |
android:id="@+id/name" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Mohammed" | |
android:textColor="@android:color/black" | |
android:textSize="20sp" /> | |
</LinearLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment