Created
December 21, 2017 11:53
-
-
Save SalaSuresh/5173023eb1c18688652a4768ad047850 to your computer and use it in GitHub Desktop.
Android RecyclerView with enable and disable button.
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.intent; | |
public class Contact { | |
public String name; | |
public String number; | |
public boolean blocked; | |
public String getName() { | |
return name; | |
} | |
public String getNumber() { | |
return number; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public void setNumber(String number) { | |
this.number = number; | |
} | |
public boolean isBlocked() { | |
return blocked; | |
} | |
public void setBlocked(boolean blocked) { | |
this.blocked = blocked; | |
} | |
} |
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.intent; | |
import android.support.v7.widget.RecyclerView; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.Button; | |
import android.widget.TextView; | |
import com.games4playstore.aroundyou.R; | |
import java.util.ArrayList; | |
public class ContactsAdapter extends RecyclerView.Adapter<ContactsAdapter.ContactsViewHolder> { | |
ArrayList<Contact> contacts = new ArrayList<>(); | |
public ContactsAdapter(ArrayList<Contact> contacts) { | |
this.contacts = contacts; | |
} | |
@Override | |
public ContactsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_contact, parent, false); | |
return new ContactsViewHolder(view); | |
} | |
@Override | |
public void onBindViewHolder(final ContactsViewHolder holder, final int position) { | |
holder.textName.setText(contacts.get(position).name); | |
holder.textNumber.setText(contacts.get(position).number); | |
if (contacts.get(position).isBlocked()) { | |
holder.buttonBlock.setText("Enable"); | |
} else { | |
holder.buttonBlock.setText("Block"); | |
} | |
holder.buttonBlock.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Log.e("test", "" + position + " : " + contacts.get(position).isBlocked()); | |
if (contacts.get(position).isBlocked()) { | |
contacts.get(position).setBlocked(false); | |
holder.buttonBlock.setText("block"); | |
} else { | |
contacts.get(position).setBlocked(true); | |
holder.buttonBlock.setText("enable"); | |
} | |
} | |
}); | |
} | |
@Override | |
public int getItemCount() { | |
return contacts.size(); | |
} | |
class ContactsViewHolder extends RecyclerView.ViewHolder { | |
TextView textName, textNumber; | |
Button buttonBlock; | |
public ContactsViewHolder(View itemView) { | |
super(itemView); | |
textName = itemView.findViewById(R.id.text_name); | |
textNumber = itemView.findViewById(R.id.text_number); | |
buttonBlock = itemView.findViewById(R.id.button_block); | |
} | |
} | |
public ArrayList<Contact> getContactList() { | |
return contacts; | |
} | |
} |
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" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="vertical" | |
android:padding="16dp"> | |
<LinearLayout | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:orientation="vertical"> | |
<TextView | |
android:id="@+id/text_name" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Contact Name" | |
android:textStyle="bold" /> | |
<TextView | |
android:id="@+id/text_number" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="+91 9972464598" /> | |
</LinearLayout> | |
<Button | |
android:id="@+id/button_block" | |
android:layout_width="65dp" | |
android:layout_height="35dp" | |
android:layout_alignParentRight="true" | |
android:text="enable" | |
android:textSize="10sp" /> | |
</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
<?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:orientation="vertical"> | |
<android.support.v7.widget.RecyclerView | |
android:id="@+id/recycler_contacts" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_above="@+id/button_get" | |
android:layout_alignParentTop="true" /> | |
<Button | |
android:id="@+id/button_get" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_alignParentBottom="true" | |
android:text="get" /> | |
</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 com.suresh.intent; | |
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.view.View; | |
import android.widget.Button; | |
import android.widget.Toast; | |
import com.games4playstore.aroundyou.R; | |
import java.util.ArrayList; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.layout_recycler); | |
ArrayList<Contact> contacts = new ArrayList<>(); | |
for (int i = 1; i < 20; i++) { | |
Contact contact = new Contact(); | |
contact.setName("User Name: " + i); | |
contact.setNumber("Number: " + i); | |
if (i == 5) | |
contact.setBlocked(true); | |
contacts.add(contact); | |
} | |
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_contacts); | |
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); | |
recyclerView.setLayoutManager(linearLayoutManager); | |
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL)); | |
final ContactsAdapter contactsAdapter = new ContactsAdapter(contacts); | |
recyclerView.setAdapter(contactsAdapter); | |
Button button = (Button) findViewById(R.id.button_get); | |
button.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
String message = ""; | |
ArrayList<Contact> contacts = contactsAdapter.getContactList(); | |
for (Contact contact : contacts) { | |
if (contact.blocked == true) { | |
message += "\n" + contact.name; | |
} | |
} | |
Toast.makeText(MainActivity.this, "" + message + "\n are blocked.", Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment