use of com.ichi2.anki.widgets.DeckDropDownAdapter in project AnkiChinaAndroid by ankichinateam.
the class Statistics method loadData.
public void loadData(Collection col) {
Timber.d("onCollectionLoaded():%s", getAnkiActivity());
if (getAnkiActivity() == null || col == null) {
return;
}
mDropDownDecks = col.getDecks().allSorted();
mActionBarSpinner.setAdapter(new DeckDropDownAdapter(getContext(), mDropDownDecks, R.layout.dropdown_deck_selected_item_static, this));
// Setup Task Handler
mTaskHandler = new AnkiStatsTaskHandler(col);
// Dirty way to get text size from a TextView with current style, change if possible
float size = new TextView(getContext()).getTextSize();
mTaskHandler.setmStandardTextSize(size);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
// Set up the ViewPager with the sections adapter.
mViewPager.setAdapter(new StatsPagerAdapter(getChildFragmentManager()));
mSlidingTabLayout = mRoot.findViewById(R.id.sliding_tabs);
// Fixes #8984: scroll to position 0 in RTL layouts
ViewTreeObserver tabObserver = mSlidingTabLayout.getViewTreeObserver();
tabObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
// Note: we can't use a lambda as we use 'this' to refer to the class.
@Override
public void onGlobalLayout() {
// we need this here: If we select tab 0 before in an RTL context the layout has been drawn,
// then it doesn't perform a scroll animation and selects the wrong element
mSlidingTabLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
mSlidingTabLayout.selectTab(mSlidingTabLayout.getTabAt(0));
}
});
// Prepare options menu only after loading everything
getAnkiActivity().supportInvalidateOptionsMenu();
// initMenu(mToolbar.getMenu());
mViewPager.getAdapter().notifyDataSetChanged();
// Default to libanki's selected deck
selectDeckById(col.getDecks().selected());
}
use of com.ichi2.anki.widgets.DeckDropDownAdapter in project AnkiChinaAndroid by ankichinateam.
the class SelfStudyActivity method onCollectionLoaded.
// Finish initializing the activity after the collection has been correctly loaded
@Override
protected void onCollectionLoaded(Collection col) {
super.onCollectionLoaded(col);
Timber.d("onCollectionLoaded()");
registerExternalStorageListener();
final SharedPreferences preferences = AnkiDroidApp.getSharedPrefs(getBaseContext());
// Load reference to action bar title
mActionBarTitle = findViewById(R.id.toolbar_title);
mTabLayout = findViewById(R.id.tab_layout);
mTop = findViewById(R.id.invisible_top);
mTop.setVisibility(View.GONE);
mRestrictOnTab = "";
mTabType = getIntent().getIntExtra("type", 0);
mComplete = findViewById(R.id.tv_complete);
mComplete.setOnClickListener(v -> toggleMultiSelectMode(false));
mBack = findViewById(R.id.iv_back);
mStartStudyButton = findViewById(R.id.confirm);
mStartStudyButton.setOnClickListener(v -> {
boolean showDialog = false;
try {
if (!preferences.getString(Consts.KEY_SELF_STUDYING_LIST, "").isEmpty()) {
String[] savedCardListStr = preferences.getString(Consts.KEY_SELF_STUDYING_LIST, "").replace("[", "").replace("]", "").split(", ");
long[] savedCardList = new long[savedCardListStr.length];
for (int i = 0; i < savedCardListStr.length; i++) {
savedCardList[i] = Long.parseLong(savedCardListStr[i]);
}
List<Long> newCardList = getCol().filterToValidCards(savedCardList);
long[] filterToValidCards = new long[newCardList.size()];
for (int i = 0; i < newCardList.size(); i++) {
filterToValidCards[i] = newCardList.get(i);
}
if (preferences.getInt(Consts.KEY_SELF_STUDYING_LIST_INDEX, 0) + 1 < savedCardListStr.length) {
// 没跑完
Intent previewer = new Intent(SelfStudyActivity.this, Previewer2.class);
CustomStyleDialog studyDialog = new CustomStyleDialog.Builder(this).setCustomLayout(R.layout.dialog_common_custom_next).setTitle("是否继续上一次的主动练习?").centerTitle().setMessage("你上次的主动练习还有待学任务,请选择是继续上一次还是开始新的主动练习").setPositiveButton("开始新的", (dialog, which) -> {
dialog.dismiss();
long[] cardList;
if (inMultiSelectMode() && checkedCardCount() > 1) {
// Multiple cards have been explicitly selected, so preview only those cards
previewer.putExtra("index", 0);
cardList = getSelectedCardIds();
} else {
// Preview all cards, starting from the one that is currently selected
// int startIndex = mCheckedCards.isEmpty() ? 0 : mCheckedCards.iterator().next().getPosition();
previewer.putExtra("index", 0);
cardList = getAllCardIds();
}
previewer.putExtra("cardList", cardList);
preferences.edit().putString(Consts.KEY_SELF_STUDYING_LIST, Arrays.toString(cardList)).putInt(Consts.KEY_SELF_STUDYING_LIST_INDEX, 0).apply();
startActivityForResultWithoutAnimation(previewer, PREVIEW_CARDS);
}).setNegativeButton("继续上次", (dialog, which) -> {
dialog.dismiss();
previewer.putExtra("cardList", filterToValidCards);
previewer.putExtra("index", preferences.getInt(Consts.KEY_SELF_STUDYING_LIST_INDEX, 0));
startActivityForResultWithoutAnimation(previewer, PREVIEW_CARDS);
}).create();
showDialog = true;
studyDialog.show();
}
}
} catch (Exception ignored) {
}
if (!showDialog) {
Intent previewer = new Intent(SelfStudyActivity.this, Previewer2.class);
long[] cardList;
if (inMultiSelectMode() && checkedCardCount() > 1) {
// Multiple cards have been explicitly selected, so preview only those cards
previewer.putExtra("index", 0);
cardList = getSelectedCardIds();
} else {
// Preview all cards, starting from the one that is currently selected
// int startIndex = mCheckedCards.isEmpty() ? 0 : mCheckedCards.iterator().next().getPosition();
previewer.putExtra("index", 0);
cardList = getAllCardIds();
}
previewer.putExtra("cardList", cardList);
preferences.edit().putString(Consts.KEY_SELF_STUDYING_LIST, Arrays.toString(cardList)).putInt(Consts.KEY_SELF_STUDYING_LIST_INDEX, 0).apply();
startActivityForResultWithoutAnimation(previewer, PREVIEW_CARDS);
}
});
// Add drop-down menu to select deck to action bar.
mDropDownDecks = getCol().getDecks().allSorted();
mDropDownDeckAdapter = new DeckDropDownAdapter(this, mDropDownDecks, R.layout.dropdown_deck_selected_item_self, this);
mToolbar = findViewById(R.id.toolbar);
if (mToolbar != null) {
setSupportActionBar(mToolbar);
// enable ActionBar app icon to behave as action to toggle nav drawer
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// getSupportActionBar().setHomeButtonEnabled(true);
// Decide which action to take when the navigation button is tapped.
// mToolbar.setNavigationIcon(R.mipmap.nav_bar_back_normal);
// mToolbar.setNavigationOnClickListener(v -> onBackPressed());
}
ActionBar mActionBar = getSupportActionBar();
if (mActionBar != null) {
mActionBar.setDisplayShowTitleEnabled(false);
}
// mOrder = CARD_ORDER_CREATE_TIME;
String colOrder = getCol().getConf().getString("sortType");
for (int c = 0; c < fSortTypes.length; ++c) {
if (fSortTypes[c].equals(colOrder)) {
mOrder = c;
break;
}
}
if (mOrder == 1 && preferences.getBoolean("cardBrowserNoSorting", false)) {
mOrder = 0;
}
// This upgrade should already have been done during
// setConf. However older version of AnkiDroid didn't call
// upgradeJSONIfNecessary during setConf, which means the
// conf saved may still have this bug.
mOrderAsc = Upgrade.upgradeJSONIfNecessary(getCol(), getCol().getConf(), "sortBackwards", false);
// mOrderAsc = true;
getCol().getConf().put("sortType", fSortTypes[mOrder]);
getCol().getConf().put("sortBackwards", mOrderAsc);
mCards = new ArrayList<>();
mCardsListView = findViewById(R.id.card_browser_list);
mMultiModeBottomLayout = findViewById(R.id.rl_multi_mode);
findViewById(R.id.add_note_action).setOnClickListener(v -> {
Intent intent = new Intent(SelfStudyActivity.this, NoteEditor.class);
intent.putExtra(NoteEditor.EXTRA_CALLER, NoteEditor.CALLER_CARDBROWSER_ADD);
startActivityForResultWithAnimation(intent, ADD_NOTE, ActivityTransitionAnimation.LEFT);
});
TextView selectCount = findViewById(R.id.select_count);
TextView move = findViewById(R.id.move);
TextView delete = findViewById(R.id.delete);
TextView cancel = findViewById(R.id.cancel);
CheckBox stick = findViewById(R.id.stick);
move.setOnClickListener(v -> {
if (mCardsAdapter.getSelectedItemIds().isEmpty()) {
return;
}
AlertDialog.Builder builderSingle = new AlertDialog.Builder(SelfStudyActivity.this);
builderSingle.setTitle(getString(R.string.move_all_to_deck));
// WARNING: changeDeck depends on this index, so any changes should be reflected there.
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(SelfStudyActivity.this, R.layout.dropdown_deck_item);
for (Deck deck : getValidDecksForChangeDeck()) {
try {
arrayAdapter.add(deck.getString("name"));
} catch (JSONException e) {
e.printStackTrace();
}
}
builderSingle.setNegativeButton(getString(R.string.cancel), (dialog, which) -> dialog.dismiss());
builderSingle.setAdapter(arrayAdapter, (dialog, which) -> changeDeck(which));
builderSingle.show();
});
delete.setOnClickListener(v -> {
if (mCardsAdapter.getSelectedItemIds().isEmpty()) {
return;
}
CollectionTask.launchCollectionTask(DISMISS_MULTI, mDeleteNoteHandler, new TaskData(new Object[] { mCardsAdapter.getSelectedItemIdArray(), Collection.DismissType.DELETE_NOTE_MULTI }));
toggleMultiSelectMode(false);
mCardsAdapter.getSelectedItemIds().clear();
mCardsAdapter.notifyDataSetChanged();
});
int[] attrs = new int[] { R.attr.primary_text_third_color999999 };
TypedArray ta = obtainStyledAttributes(attrs);
int textGrayColor = ta.getColor(0, ContextCompat.getColor(this, R.color.new_primary_text_third_color));
ta.recycle();
stick.setOnCheckedChangeListener((buttonView, isChecked) -> {
mCardsAdapter.selectItem(isChecked);
selectCount.setText((isChecked ? "全选" : "已选") + mCardsAdapter.selectItemCount());
selectCount.setTextColor(isChecked ? ContextCompat.getColor(this, R.color.primary_color) : textGrayColor);
});
selectCount.setOnClickListener(v -> stick.performClick());
cancel.setOnClickListener(v -> {
toggleMultiSelectMode(false);
});
mCardsAdapter = new CardsListAdapter(getLayoutInflater(), this, new CardsListAdapter.CardListAdapterCallback() {
@Override
public List<CardCache> getCards() {
return SelfStudyActivity.this.getCards();
}
@Override
public void onChangeMultiMode(boolean isMultiMode) {
mMultiModeBottomLayout.setVisibility(isMultiMode ? View.VISIBLE : View.GONE);
mStartStudyButton.setVisibility(isMultiMode ? View.GONE : mCards.size() > 0 ? View.VISIBLE : View.GONE);
mSearchView.setVisibility(isMultiMode ? View.INVISIBLE : View.VISIBLE);
mBack.setVisibility(isMultiMode ? View.GONE : View.VISIBLE);
mComplete.setVisibility(isMultiMode ? View.VISIBLE : View.GONE);
selectCount.setText("已选0");
supportInvalidateOptionsMenu();
}
@Override
public void onItemSelect(int count) {
selectCount.setText("已选" + count);
updateMultiselectMenu();
}
});
// link the adapter to the main mCardsListView
mCardsListView.setAdapter(mCardsAdapter);
mCardsListView.setLayoutManager(new LinearLayoutManager(this));
mCardsAdapter.setTvOrderClickListener(v -> showOrderListDialog());
mCardsAdapter.setIvOrderClickListener(v -> {
// 修改升序/降序
mOrderAsc = !mOrderAsc;
getCol().getConf().put("sortBackwards", mOrderAsc);
Collections.reverse(mCards);
updateList();
mCardsAdapter.updateOrderState(mOrderNames[mOrder], mOrderAsc);
});
mCardsAdapter.setDeckClickListener(view -> {
if (mCardsAdapter.isMultiCheckableMode()) {
return;
}
Intent previewer = new Intent(SelfStudyActivity.this, Previewer.class);
long[] ids = inMultiSelectMode() && checkedCardCount() > 1 ? getSelectedCardIds() : getAllCardIds();
long targetId = (long) view.getTag();
for (int i = 0; i < ids.length; i++) {
if (ids[i] == targetId) {
previewer.putExtra("index", i);
break;
}
}
previewer.putExtra("cardList", ids);
startActivityForResultWithoutAnimation(previewer, PREVIEW_CARDS);
// openNoteEditorForCard((long) view.getTag());
});
mCardsAdapter.setDeckLongClickListener(view -> {
if (mCardsAdapter.isMultiCheckableMode()) {
return false;
}
mCardsAdapter.setMultiCheckable(true);
return true;
});
mCardsAdapter.setMarkClickListener(v -> {
CollectionTask.launchCollectionTask(DISMISS_MULTI, markCardHandler(), new TaskData(new Object[] { new long[] { (long) v.getTag() }, Collection.DismissType.MARK_NOTE_MULTI }));
mCardsAdapter.notifyDataSetChanged();
});
mOrderNames = getResources().getStringArray(R.array.card_browser_order_labels);
mCardsAdapter.updateOrderState(mOrderNames[mOrder], mOrderAsc);
mCardsAdapter.setFlagClickListener(v -> {
if (mListPop == null) {
mListPop = new ListPopupWindow(this);
for (int i = 0; i < mFlagRes.length; i++) {
Map<String, Object> map = new HashMap<>();
map.put("img", mFlagRes[i]);
map.put("content", mFlagContent[i]);
mFlagList.add(map);
}
mListPop.setAdapter(new SimpleAdapter(SelfStudyActivity.this, mFlagList, R.layout.item_flags_list, new String[] { "img", "content" }, new int[] { R.id.flag_icon, R.id.flag_text }));
mListPop.setWidth(v.getRootView().getWidth() / 2);
mListPop.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
// 设置是否是模式
mListPop.setModal(true);
}
mListPop.setOnItemClickListener((parent, view, position, id) -> {
CollectionTask.launchCollectionTask(DISMISS_MULTI, flagCardHandler(), new TaskData(new Object[] { new long[] { (long) v.getTag() }, Collection.DismissType.FLAG, position }));
mCardsAdapter.notifyDataSetChanged();
mListPop.dismiss();
});
mListPop.setAnchorView(v);
mListPop.show();
});
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
// If a valid value for last deck exists then use it, otherwise use libanki selected deck
if ((getLastDeckId() != null && getLastDeckId() == ALL_DECKS_ID)) {
selectAllDecks();
} else if (getLastDeckId() != null && getCol().getDecks().get(getLastDeckId(), false) != null) {
selectDeckById(getLastDeckId());
} else {
selectDeckById(getCol().getDecks().selected());
}
initSearchView();
initTabLayout();
findViewById(R.id.shadeView).setOnClickListener(v -> {
if (mPopupWindow != null && mPopupWindow.isShowing()) {
mPopupWindow.dismiss();
}
if (mOrderListWindow != null && mOrderListWindow.isShowing()) {
mOrderListWindow.dismiss();
}
});
}
use of com.ichi2.anki.widgets.DeckDropDownAdapter in project AnkiChinaAndroid by ankichinateam.
the class CardBrowser method onCollectionLoaded.
// Finish initializing the activity after the collection has been correctly loaded
@Override
protected void onCollectionLoaded(Collection col) {
super.onCollectionLoaded(col);
Timber.d("onCollectionLoaded()");
registerExternalStorageListener();
SharedPreferences preferences = AnkiDroidApp.getSharedPrefs(getBaseContext());
// Load reference to action bar title
mActionBarTitle = (TextView) findViewById(R.id.toolbar_title);
// Add drop-down menu to select deck to action bar.
mDropDownDecks = getCol().getDecks().allSorted();
mDropDownAdapter = new DeckDropDownAdapter(this, mDropDownDecks, R.layout.dropdown_deck_selected_item, this);
Toolbar toolbar = findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
// enable ActionBar app icon to behave as action to toggle nav drawer
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
// Decide which action to take when the navigation button is tapped.
toolbar.setNavigationOnClickListener(v -> finishActivityWithFade(this, ActivityTransitionAnimation.RIGHT));
}
ActionBar mActionBar = getSupportActionBar();
if (mActionBar != null) {
mActionBar.setDisplayShowTitleEnabled(false);
}
mActionBarSpinner = (Spinner) findViewById(R.id.toolbar_spinner);
mActionBarSpinner.setAdapter(mDropDownAdapter);
mActionBarSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
deckDropDownItemChanged(position);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// do nothing
}
});
mActionBarSpinner.setVisibility(View.VISIBLE);
mOrder = CARD_ORDER_NONE;
String colOrder = getCol().getConf().getString("sortType");
for (int c = 0; c < fSortTypes.length; ++c) {
if (fSortTypes[c].equals(colOrder)) {
mOrder = c;
break;
}
}
if (mOrder == 1 && preferences.getBoolean("cardBrowserNoSorting", false)) {
mOrder = 0;
}
// This upgrade should already have been done during
// setConf. However older version of AnkiDroid didn't call
// upgradeJSONIfNecessary during setConf, which means the
// conf saved may still have this bug.
mOrderAsc = Upgrade.upgradeJSONIfNecessary(getCol(), getCol().getConf(), "sortBackwards", false);
mCards = new ArrayList<>();
mCardsListView = (ListView) findViewById(R.id.card_browser_list);
// Create a spinner for column1
Spinner cardsColumn1Spinner = (Spinner) findViewById(R.id.browser_column1_spinner);
ArrayAdapter<CharSequence> column1Adapter = ArrayAdapter.createFromResource(this, R.array.browser_column1_headings, android.R.layout.simple_spinner_item);
column1Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cardsColumn1Spinner.setAdapter(column1Adapter);
mColumn1Index = AnkiDroidApp.getSharedPrefs(getBaseContext()).getInt("cardBrowserColumn1", 0);
cardsColumn1Spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
// If a new column was selected then change the key used to map from mCards to the column TextView
if (pos != mColumn1Index) {
mColumn1Index = pos;
AnkiDroidApp.getSharedPrefs(AnkiDroidApp.getInstance().getBaseContext()).edit().putInt("cardBrowserColumn1", mColumn1Index).commit();
Column[] fromMap = mCardsAdapter.getFromMapping();
fromMap[0] = COLUMN1_KEYS[mColumn1Index];
mCardsAdapter.setFromMapping(fromMap);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// Do Nothing
}
});
// Load default value for column2 selection
mColumn2Index = AnkiDroidApp.getSharedPrefs(getBaseContext()).getInt("cardBrowserColumn2", 0);
// Setup the column 2 heading as a spinner so that users can easily change the column type
Spinner cardsColumn2Spinner = (Spinner) findViewById(R.id.browser_column2_spinner);
ArrayAdapter<CharSequence> column2Adapter = ArrayAdapter.createFromResource(this, R.array.browser_column2_headings, android.R.layout.simple_spinner_item);
column2Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cardsColumn2Spinner.setAdapter(column2Adapter);
// Create a new list adapter with updated column map any time the user changes the column
cardsColumn2Spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
// If a new column was selected then change the key used to map from mCards to the column TextView
if (pos != mColumn2Index) {
mColumn2Index = pos;
AnkiDroidApp.getSharedPrefs(AnkiDroidApp.getInstance().getBaseContext()).edit().putInt("cardBrowserColumn2", mColumn2Index).commit();
Column[] fromMap = mCardsAdapter.getFromMapping();
fromMap[1] = COLUMN2_KEYS[mColumn2Index];
mCardsAdapter.setFromMapping(fromMap);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// Do Nothing
}
});
// get the font and font size from the preferences
int sflRelativeFontSize = preferences.getInt("relativeCardBrowserFontSize", DEFAULT_FONT_SIZE_RATIO);
String sflCustomFont = preferences.getString("browserEditorFont", "");
Column[] columnsContent = { COLUMN1_KEYS[mColumn1Index], COLUMN2_KEYS[mColumn2Index] };
// make a new list adapter mapping the data in mCards to column1 and column2 of R.layout.card_item_browser
mCardsAdapter = new MultiColumnListAdapter(this, R.layout.card_item_browser, columnsContent, new int[] { R.id.card_sfld, R.id.card_column2 }, sflRelativeFontSize, sflCustomFont);
// link the adapter to the main mCardsListView
mCardsListView.setAdapter(mCardsAdapter);
// make the items (e.g. question & answer) render dynamically when scrolling
mCardsListView.setOnScrollListener(new RenderOnScroll());
// set the spinner index
cardsColumn1Spinner.setSelection(mColumn1Index);
cardsColumn2Spinner.setSelection(mColumn2Index);
mCardsListView.setOnItemClickListener(new ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (mInMultiSelectMode) {
// click on whole cell triggers select
CheckBox cb = (CheckBox) view.findViewById(R.id.card_checkbox);
cb.toggle();
onCheck(position, view);
} else {
// load up the card selected on the list
long clickedCardId = getCards().get(position).getId();
openNoteEditorForCard(clickedCardId);
}
}
});
mCardsListView.setOnItemLongClickListener(new ListView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int position, long id) {
mLastSelectedPosition = position;
loadMultiSelectMode();
// click on whole cell triggers select
CheckBox cb = (CheckBox) view.findViewById(R.id.card_checkbox);
cb.toggle();
onCheck(position, view);
recenterListView(view);
mCardsAdapter.notifyDataSetChanged();
return true;
}
});
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
// If a valid value for last deck exists then use it, otherwise use libanki selected deck
if (getLastDeckId() != null && getLastDeckId() == ALL_DECKS_ID) {
selectAllDecks();
} else if (getLastDeckId() != null && getCol().getDecks().get(getLastDeckId(), false) != null) {
selectDeckById(getLastDeckId());
} else {
selectDeckById(getCol().getDecks().selected());
}
}
Aggregations