Created
July 31, 2016 23:00
-
-
Save ccentauri/1812b03795ab7495f020cec4074d1262 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
public class ActionDialog extends BottomSheetDialogFragment { | |
private final String TAG = getClass().getSimpleName(); | |
@BindView(R.id.recycler_view) | |
public RecyclerView recyclerView; | |
private Builder.ActionAdapter adapter; | |
private FragmentManager fragmentManager; | |
private LinearLayoutManager layoutManager; | |
@Override | |
public void setupDialog(Dialog dialog, int style) { | |
View rootView = getActivity().getLayoutInflater().inflate(R.layout.bottom_sheet_dialog_action, null); | |
ButterKnife.bind(this, rootView); | |
layoutManager = new LinearLayoutManager(getActivity()); | |
recyclerView.setLayoutManager(layoutManager); | |
recyclerView.setAdapter(adapter); | |
recyclerView.setHasFixedSize(true); | |
recyclerView.setItemAnimator(new DefaultItemAnimator()); | |
dialog.setContentView(rootView); | |
super.setupDialog(dialog, style); | |
} | |
public ActionDialog setAdapter(Builder.ActionAdapter adapter) { | |
this.adapter = adapter; | |
return this; | |
} | |
public ActionDialog setFragmentManager(FragmentManager fragmentManager) { | |
this.fragmentManager = fragmentManager; | |
return this; | |
} | |
public void show() { | |
show(fragmentManager, TAG); | |
} | |
public static class Builder { | |
private final String TAG = ActionDialog.class.getSimpleName() + "." + this.getClass().getSimpleName(); | |
private Context context; | |
private ArrayList<SimpleActionItem> dataSet; | |
private ActionAdapter adapter; | |
private ActionDialog dialog; | |
public Builder(FragmentManager fragmentManager, Context context) { | |
this.context = context; | |
dataSet = new ArrayList<>(); | |
adapter = new ActionAdapter(dataSet); | |
dialog = new ActionDialog(); | |
dialog.setAdapter(adapter); | |
dialog.setFragmentManager(fragmentManager); | |
} | |
public Builder addItem(SimpleActionItem item) { | |
if (item != null) { | |
dataSet.add(item); | |
} | |
adapter.notifyItemInserted(dataSet.indexOf(item)); | |
return this; | |
} | |
public ActionDialog build() { | |
return dialog; | |
} | |
private class ActionAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { | |
private ArrayList<SimpleActionItem> dataSet; | |
public ActionAdapter(ArrayList<SimpleActionItem> dataSet) { | |
this.dataSet = dataSet; | |
} | |
public class ActionViewHolder extends RecyclerView.ViewHolder { | |
private TextView TVTitle; | |
public ActionViewHolder(View itemView) { | |
super(itemView); | |
TVTitle = (TextView) itemView.findViewById(R.id.text_view_title); | |
} | |
} | |
@Override | |
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
return new ActionViewHolder( | |
LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_dialog_action, parent, false)); | |
} | |
@Override | |
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { | |
if (!(holder instanceof ActionViewHolder)) { | |
Log.e(TAG, "onBindViewHolder()-> holder is not instance of ActionViewHolder"); | |
return; | |
} | |
if (dataSet == null) { | |
Log.e(TAG, "onBindViewHolder()-> dataSet is null"); | |
return; | |
} | |
ActionViewHolder viewHolder = (ActionViewHolder) holder; | |
final SimpleActionItem simpleActionItem = dataSet.get(position); | |
Log.d(TAG, "onBindViewHolder()-> Title: " + simpleActionItem.getTitle()); | |
viewHolder.TVTitle.setText(dataSet.get(position).getTitle()); | |
viewHolder.TVTitle.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
if (simpleActionItem.getOnItemClickListener() != null) { | |
simpleActionItem.getOnItemClickListener().onClick(simpleActionItem); | |
} | |
} | |
}); | |
viewHolder.TVTitle.setOnLongClickListener(new View.OnLongClickListener() { | |
@Override | |
public boolean onLongClick(View v) { | |
if (simpleActionItem.getOnItemClickListener() != null) { | |
simpleActionItem.getOnItemClickListener().onLongClick(simpleActionItem); | |
return true; | |
} | |
return false; | |
} | |
}); | |
} | |
@Override | |
public int getItemCount() { | |
return dataSet == null ? 0 : dataSet.size(); | |
} | |
public ArrayList<SimpleActionItem> getDataSet() { | |
return dataSet; | |
} | |
} | |
} | |
public static class SimpleActionItem { | |
private String title; | |
private OnItemClickListener onItemClickListener; | |
public SimpleActionItem(String title, OnItemClickListener onItemClickListener) { | |
this.title = title; | |
this.onItemClickListener = onItemClickListener; | |
} | |
public String getTitle() { | |
return title; | |
} | |
public OnItemClickListener getOnItemClickListener() { | |
return onItemClickListener; | |
} | |
} | |
public static interface OnItemClickListener { | |
void onClick(SimpleActionItem clickedItem); | |
void onLongClick(SimpleActionItem clickedItem); | |
} | |
} | |
<!-- recycler_dialog_action --> | |
<?xml version="1.0" encoding="utf-8"?> | |
<TextView xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/text_view_title" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:background="@drawable/action_dialog_item" | |
android:clickable="true" | |
android:paddingBottom="16dp" | |
android:paddingLeft="16dp" | |
android:paddingRight="16dp" | |
android:paddingTop="16dp" | |
android:text="Action 1" | |
android:textColor="@color/colorTextPrimary" | |
android:textSize="16sp" /> | |
<!-- bottom_sheet_dialog_action --> | |
<?xml version="1.0" encoding="utf-8"?> | |
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:fitsSystemWindows="true" | |
app:layout_behavior="@string/bottom_sheet_behavior"> | |
<android.support.v7.widget.RecyclerView | |
android:id="@+id/recycler_view" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:background="@color/white" /> | |
</FrameLayout> | |
<!-- usage --> | |
ActionDialog actionDialog = new ActionDialog.Builder(getSupportFragmentManager(), this) | |
.addItem(new ActionDialog.SimpleActionItem("Item 1", new ActionDialog.OnItemClickListener() { | |
@Override | |
public void onClick(ActionDialog.SimpleActionItem clickedItem) { | |
Toast.makeText(MyProfileActivity.this, clickedItem.getTitle(), Toast.LENGTH_SHORT).show(); | |
} | |
@Override | |
public void onLongClick(ActionDialog.SimpleActionItem clickedItem) { | |
Toast.makeText(MyProfileActivity.this, "Long click on " + clickedItem.getTitle(), Toast.LENGTH_SHORT).show(); | |
} | |
})) | |
.addItem(new ActionDialog.SimpleActionItem("Item 2", new ActionDialog.OnItemClickListener() { | |
@Override | |
public void onClick(ActionDialog.SimpleActionItem clickedItem) { | |
Toast.makeText(MyProfileActivity.this, clickedItem.getTitle(), Toast.LENGTH_SHORT).show(); | |
} | |
@Override | |
public void onLongClick(ActionDialog.SimpleActionItem clickedItem) { | |
Toast.makeText(MyProfileActivity.this, "Long click on " + clickedItem.getTitle(), Toast.LENGTH_SHORT).show(); | |
} | |
})) | |
.addItem(new ActionDialog.SimpleActionItem("Item 3", new ActionDialog.OnItemClickListener() { | |
@Override | |
public void onClick(ActionDialog.SimpleActionItem clickedItem) { | |
Toast.makeText(MyProfileActivity.this, clickedItem.getTitle(), Toast.LENGTH_SHORT).show(); | |
} | |
@Override | |
public void onLongClick(ActionDialog.SimpleActionItem clickedItem) { | |
Toast.makeText(MyProfileActivity.this, "Long click on " + clickedItem.getTitle(), Toast.LENGTH_SHORT).show(); | |
} | |
})) | |
.build(); | |
actionDialog.show(); |
Gaponec
commented
Aug 1, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment