Last active
October 4, 2017 02:56
-
-
Save andriangungon/166601043469123dfde76c8d791ae2bd to your computer and use it in GitHub Desktop.
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 net.simplifiedcoding.recyclerviewoptionsmenu; | |
import android.content.Context; | |
import android.content.DialogInterface; | |
import android.support.v7.app.AlertDialog; | |
import android.support.v7.widget.PopupMenu; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.LayoutInflater; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.TextView; | |
import java.util.List; | |
/** | |
* Created by Belal on 29/09/16. | |
*/ | |
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> { | |
private List<MyList> list; | |
private Context mCtx; | |
public CustomAdapter(List<MyList> list, Context mCtx) { | |
this.list = list; | |
this.mCtx = mCtx; | |
} | |
@Override | |
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
View v = LayoutInflater.from(parent.getContext()) | |
.inflate(R.layout.list_items, parent, false); | |
return new ViewHolder(v); | |
} | |
@Override | |
public void onBindViewHolder(final CustomAdapter.ViewHolder holder, int position) { | |
MyList myList = list.get(position); | |
holder.textViewHead.setText(myList.getHead()); | |
holder.textViewDesc.setText(myList.getDesc()); | |
holder.buttonViewOption.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
//creating a popup menu | |
PopupMenu popup = new PopupMenu(mCtx, holder.buttonViewOption); | |
//inflating menu from xml resource | |
popup.inflate(R.menu.options_menu); | |
//adding click listener | |
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { | |
@Override | |
public boolean onMenuItemClick(MenuItem item) { | |
switch (item.getItemId()) { | |
case R.id.menu1: | |
//handle menu1 click | |
break; | |
case R.id.menu2: | |
//handle menu2 click | |
break; | |
case R.id.menu3: | |
//handle menu3 click | |
break; | |
} | |
return false; | |
} | |
}); | |
//displaying the popup | |
popup.show(); | |
} | |
}); | |
} | |
@Override | |
public int getItemCount() { | |
return list.size(); | |
} | |
public class ViewHolder extends RecyclerView.ViewHolder { | |
public TextView textViewHead; | |
public TextView textViewDesc; | |
public TextView buttonViewOption; | |
public ViewHolder(View itemView) { | |
super(itemView); | |
textViewHead = (TextView) itemView.findViewById(R.id.textViewHead); | |
textViewDesc = (TextView) itemView.findViewById(R.id.textViewDesc); | |
buttonViewOption = (TextView) itemView.findViewById(R.id.textViewOptions); | |
} | |
} | |
} |
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="vertical"> | |
<android.support.v7.widget.CardView | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_margin="4dp" | |
android:elevation="4dp"> | |
<RelativeLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:padding="@dimen/activity_horizontal_margin"> | |
<TextView | |
android:id="@+id/textViewHead" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="Your Heading" | |
android:textAppearance="?android:textAppearanceLarge" /> | |
<TextView | |
android:id="@+id/textViewDesc" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_below="@id/textViewHead" | |
android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi molestie nisi dui. " | |
android:textAppearance="?android:textAppearanceMedium" /> | |
<TextView | |
android:id="@+id/textViewOptions" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignParentRight="true" | |
android:layout_alignParentTop="true" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:text="⋮" | |
android:textAppearance="?android:textAppearanceLarge" /> | |
</RelativeLayout> | |
</android.support.v7.widget.CardView> | |
</LinearLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment