add dialog for selecting notebook

This commit is contained in:
houxg
2016-12-21 16:05:33 +08:00
parent d4aba9db95
commit 8541c4c5c6
4 changed files with 83 additions and 30 deletions

View File

@@ -30,9 +30,23 @@ public class NotebookAdapter extends RecyclerView.Adapter<NotebookAdapter.Notebo
private Stack<String> mStack = new Stack<>();
private List<Notebook> mData;
private NotebookAdapterListener mListener;
private boolean mHasAddButton = true;
private boolean mCanOpenEmpty = true;
private int currentSelection = -1;
public void setListener(NotebookAdapterListener listener) {
public NotebookAdapter setListener(NotebookAdapterListener listener) {
mListener = listener;
return this;
}
public NotebookAdapter setHasAddButton(boolean hasAddButton) {
mHasAddButton = hasAddButton;
return this;
}
public NotebookAdapter setCanOpenEmpty(boolean canOpenEmpty) {
mCanOpenEmpty = canOpenEmpty;
return this;
}
public void refresh() {
@@ -52,7 +66,7 @@ public class NotebookAdapter extends RecyclerView.Adapter<NotebookAdapter.Notebo
@Override
public int getItemViewType(int position) {
if (position == getItemCount() - 1) {
if (mHasAddButton && position == getItemCount() - 1) {
return TYPE_ADD;
} else {
return TYPE_NOTEBOOK;
@@ -90,9 +104,9 @@ public class NotebookAdapter extends RecyclerView.Adapter<NotebookAdapter.Notebo
String notebookId = notebook.getNotebookId();
boolean isSuper = isSuper(notebookId);
boolean isSuperOrRoot = isSuper | mStack.isEmpty();
// boolean hasChild = hasChild(notebookId);
boolean hasChild = hasChild(notebookId);
holder.placeholder.setVisibility(isSuperOrRoot ? View.GONE : View.VISIBLE);
// holder.navigator.setVisibility(hasChild ? View.VISIBLE : View.INVISIBLE);
holder.navigator.setVisibility(mCanOpenEmpty | hasChild ? View.VISIBLE : View.INVISIBLE);
holder.navigator.setImageResource(isSuper ? R.drawable.ic_expanding : R.drawable.ic_expandable);
holder.navigator.setOnClickListener(new View.OnClickListener() {
@Override
@@ -165,7 +179,8 @@ public class NotebookAdapter extends RecyclerView.Adapter<NotebookAdapter.Notebo
@Override
public int getItemCount() {
return mData == null ? 1 : mData.size() + 1;
int fixed = mHasAddButton ? 1 : 0;
return mData == null ? fixed : mData.size() + fixed;
}
public interface NotebookAdapterListener {

View File

@@ -1,9 +1,7 @@
package org.houxg.leamonax.ui.edit;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
@@ -11,7 +9,6 @@ import android.text.Editable;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@@ -26,6 +23,7 @@ import org.houxg.leamonax.model.Notebook;
import org.houxg.leamonax.model.Tag;
import org.houxg.leamonax.service.AccountService;
import org.houxg.leamonax.utils.CollectionUtils;
import org.houxg.leamonax.utils.DialogUtils;
import org.houxg.leamonax.utils.DisplayUtils;
import org.houxg.leamonax.widget.RoundedRectBackgroundSpan;
@@ -172,29 +170,13 @@ public class SettingFragment extends Fragment {
@OnClick(R.id.ll_notebook)
void selectNotebook() {
final List<Notebook> notebooks = AppDataBase.getAllNotebook(AccountService.getCurrent().getUserId());
int currentSelection = -1;
String[] titles = new String[notebooks.size()];
for (int i = 0; i < titles.length; i++) {
titles[i] = notebooks.get(i).getTitle();
if (notebooks.get(i).getNotebookId().equals(mNoteBookId)) {
currentSelection = i;
DialogUtils.selectNotebook(getActivity(), getString(R.string.select_notebook), new DialogUtils.SelectNotebookListener() {
@Override
public void onNotebookSelected(Notebook notebook) {
mNoteBookId = notebook.getNotebookId();
mNotebookTv.setText(notebook.getTitle());
}
}
new AlertDialog.Builder(getActivity())
.setTitle(R.string.choose_notebook)
.setSingleChoiceItems(titles, currentSelection, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Notebook selected = notebooks.get(which);
mNoteBookId = selected.getNotebookId();
Log.i(TAG, "select=" + mNoteBookId);
mNotebookTv.setText(selected.getTitle());
dialog.dismiss();
}
})
.setCancelable(true)
.show();
});
}
public interface SettingFragmentListener {

View File

@@ -5,11 +5,15 @@ import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import org.houxg.leamonax.R;
import org.houxg.leamonax.adapter.NotebookAdapter;
import org.houxg.leamonax.model.Notebook;
public class DialogUtils {
@@ -39,8 +43,48 @@ public class DialogUtils {
.show();
}
public static void selectNotebook(Context context, String title, final SelectNotebookListener listener) {
View view = LayoutInflater.from(context).inflate(R.layout.dialog_notebook, null);
final RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.rv_notebook);
final NotebookAdapter adapter =
new NotebookAdapter()
.setCanOpenEmpty(false)
.setHasAddButton(false);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.setAdapter(adapter);
adapter.refresh();
final AlertDialog dialog = new AlertDialog.Builder(context)
.setTitle(title)
.setView(view)
.setCancelable(true)
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create();
adapter.setListener(new NotebookAdapter.NotebookAdapterListener() {
@Override
public void onClickedNotebook(Notebook notebook) {
listener.onNotebookSelected(notebook);
dialog.dismiss();
}
@Override
public void onClickedAddNotebook(String parentNotebookId) {
}
});
dialog.show();
}
public interface ChangedListener {
void onChanged(String title, String link);
}
public interface SelectNotebookListener {
void onNotebookSelected(Notebook notebook);
}
}

View File

@@ -0,0 +1,12 @@
<?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:padding="16dp">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_notebook"
android:layout_width="match_parent"
android:layout_height="320dp" />
</LinearLayout>