Example usage for android.widget ListView setSelectionFromTop

List of usage examples for android.widget ListView setSelectionFromTop

Introduction

In this page you can find the example usage for android.widget ListView setSelectionFromTop.

Prototype

public void setSelectionFromTop(int position, int y) 

Source Link

Document

Sets the selected item and positions the selection y pixels from the top edge of the ListView.

Usage

From source file:nl.spellenclubeindhoven.dominionshuffle.SelectActivity.java

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
    if (DIALOG_MINIMUM <= id && id <= DIALOG_MINIMUM + dialogMimimumCount) {
        AlertDialog alertDialog = (AlertDialog) dialog;
        ListView list = alertDialog.getListView();

        int pos;//from ww  w  .j a va 2  s.co m
        if (cardSelector.getCondition(selectedGroup) == null) {
            pos = cardSelector.getLimitMinimum(selectedGroup);
            if (pos >= 1)
                pos++;
        } else {
            pos = 1;
        }

        list.clearChoices();
        list.setItemChecked(pos, true);
        list.setSelectionFromTop(pos, list.getHeight() / 2);
    } else if (DIALOG_MAXIMUM <= id && id <= DIALOG_MAXIMUM + dialogMaximumCount) {
        AlertDialog alertDialog = (AlertDialog) dialog;
        ListView list = alertDialog.getListView();
        int pos = cardSelector.getLimitMaximum(selectedGroup);
        list.clearChoices();
        list.setItemChecked(pos, true);
        list.setSelectionFromTop(pos, list.getHeight() / 2);
    }

    switch (id) {
    case DIALOG_SOLVE_ERROR: {
        AlertDialog alertDialog = (AlertDialog) dialog;
        alertDialog.setMessage(dialogMessage);
        break;
    }
    default:
        break;
    }

    super.onPrepareDialog(id, dialog);
}

From source file:info.staticfree.android.units.Units.java

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
    switch (id) {
    case DIALOG_UNIT_CATEGORY: {
        dialog.setTitle(mDialogUnitCategoryUnit);

        final String[] projection = { UsageEntry._ID, UsageEntry._UNIT, UsageEntry._FACTOR_FPRINT };
        final Cursor c = managedQuery(
                UsageEntry.getEntriesMatchingFprint(UnitUsageDBHelper.getFingerprint(mDialogUnitCategoryUnit)),
                projection, null, null, UnitUsageDBHelper.USAGE_SORT);
        dialogUnitCategoryList.changeCursor(c);
        final ListView lv = ((AlertDialog) dialog).getListView();
        lv.setSelectionFromTop(0, 0);

    }//www  .jav a2  s .co  m
        break;
    default:
        super.onPrepareDialog(id, dialog);
    }

}

From source file:universum.studios.android.setting.SettingsBaseFragment.java

/**
 * This implementation restores the first visible item's position and its offset.
 *//*from  w  ww  .j  a v a  2 s  . co  m*/
@Override
public void onViewCreated(@NonNull final View view, @Nullable final Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    final ListView listView = (ListView) view.findViewById(android.R.id.list);
    if (listView != null) {
        final int itemPosition;
        final int itemTop;
        if (savedInstanceState != null) {
            itemPosition = savedInstanceState.getInt(SAVED_STATE_FIRST_VISIBLE_ITEM_POSITION, 0);
            itemTop = savedInstanceState.getInt(SAVED_STATE_FIRST_VISIBLE_ITEM_TOP, 0);
        } else if (mSavedFirstVisibleItemPosition != NO_POSITION) {
            itemPosition = mSavedFirstVisibleItemPosition;
            itemTop = mSavedFirstVisibleItemTop;
            this.mSavedFirstVisibleItemPosition = NO_POSITION;
            this.mSavedFirstVisibleItemTop = 0;
        } else {
            itemPosition = itemTop = 0;
        }
        view.post(new Runnable() {

            /**
             */
            @Override
            public void run() {
                listView.setSelectionFromTop(itemPosition, itemTop);
            }
        });
    }
}

From source file:com.github.wakhub.monodict.activity.FlashcardActivity.java

@UiThread
void onLoadContents(Cursor cursor) {
    Log.d(TAG, "onLoadContents: " + cursor.getCount());

    // how to maintain scroll position of listview when it updates
    // http://stackoverflow.com/questions/10196079
    ListView listView = getListView();
    int lastPosition = listView.getFirstVisiblePosition();
    int lastTopOffset = 0;
    if (listView.getCount() > 0) {
        View view = listView.getChildAt(0);
        if (view != null) {
            lastTopOffset = view.getTop();
        }//  w  w  w . j  a  v a2  s.co m
    }

    listAdapter = new ListAdapter(this, cursor);
    setListAdapter(listAdapter);

    if (lastPosition > listView.getCount()) {
        listView.setSelection(listView.getCount() - 1);
    } else {
        listView.setSelectionFromTop(lastPosition, lastTopOffset);
    }

    reloadTabs();
    activityHelper.hideProgressDialog();
}

From source file:uk.org.rivernile.edinburghbustracker.android.fragments.general.NearestStopsFragment.java

/**
 * {@inheritDoc}// ww w  . j a v  a2 s.c o m
 */
@Override
public void onLoadFinished(final Loader<ArrayList<SearchResult>> loader,
        final ArrayList<SearchResult> results) {
    if (isAdded()) {
        final ListView lv = getListView();
        // Get the first visible position so the scroll position is restored
        // later.
        int currentIndex = lv.getFirstVisiblePosition();
        final View v = lv.getChildAt(0);

        // When loading has finished, clear the ArrayAdapter and add in all
        // the new results.
        ad.clear();
        ad.addAll(results);

        final int lastIndex = results.size() - 1;

        if (lastIndex < currentIndex) {
            // If the final index is less than the index before, then scroll
            // to the new final index.
            lv.setSelectionFromTop(lastIndex, 0);
        } else {
            // Otherwise, go to the previous exact scroll position.
            lv.setSelectionFromTop(currentIndex, (v == null) ? 0 : v.getTop());
        }
    }
}

From source file:com.simplealertdialog.SimpleAlertDialog.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(android.view.Window.FEATURE_NO_TITLE);
    setContentView(R.layout.sad__dialog_simple);
    getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

    // Background
    setBackground(R.id.header, mBackgroundTop);
    setBackground(R.id.bar_wrapper, mBackgroundMiddle);
    setBackground(R.id.body, mBackgroundBottom);

    // Title/*from   www  .ja  va 2 s  . c  om*/
    if (TextUtils.isEmpty(mTitle)) {
        findViewById(R.id.header).setVisibility(View.GONE);
        findViewById(R.id.bar_wrapper).setVisibility(View.GONE);
        findViewById(R.id.title).setVisibility(View.GONE);
        findViewById(R.id.icon).setVisibility(View.GONE);
        setBackground(R.id.body, mBackgroundFull);
    } else {
        ((TextView) findViewById(R.id.title)).setText(mTitle);
        if (mTitleTextStyle != 0) {
            ((TextView) findViewById(R.id.title)).setTextAppearance(getContext(), mTitleTextStyle);
        }
        if (mIcon > 0) {
            ((ImageView) findViewById(R.id.icon)).setImageResource(mIcon);
            findViewById(R.id.title).setPadding(findViewById(R.id.title).getPaddingLeft() / 2,
                    findViewById(R.id.title).getPaddingTop(), findViewById(R.id.title).getPaddingRight(),
                    findViewById(R.id.title).getPaddingBottom());
        } else {
            findViewById(R.id.icon).setVisibility(View.GONE);
        }
        setBackground(R.id.bar, mTitleSeparatorBackground);
        if (mTitleSeparatorHeight == 0) {
            mTitleSeparatorHeight = getContext().getResources()
                    .getDimensionPixelSize(R.dimen.sad__dialog_title_separator_height);
        }
        FrameLayout.LayoutParams lpBar = new FrameLayout.LayoutParams(getMatchParent(), mTitleSeparatorHeight);
        findViewById(R.id.bar).setLayoutParams(lpBar);
        findViewById(R.id.bar).requestLayout();
        LinearLayout.LayoutParams lpBarWrapper = new LinearLayout.LayoutParams(getMatchParent(),
                mTitleSeparatorHeight);
        findViewById(R.id.bar_wrapper).setLayoutParams(lpBarWrapper);
        findViewById(R.id.bar_wrapper).requestLayout();
    }

    // Message
    if (TextUtils.isEmpty(mMessage)) {
        findViewById(R.id.message).setVisibility(View.GONE);
    } else {
        ((TextView) findViewById(R.id.message)).setText(mMessage);
        if (mMessageTextStyle != 0) {
            ((TextView) findViewById(R.id.message)).setTextAppearance(getContext(), mMessageTextStyle);
        }
    }

    // Custom View
    if (mView != null) {
        LinearLayout group = (LinearLayout) findViewById(R.id.view);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(getMatchParent(),
                LinearLayout.LayoutParams.WRAP_CONTENT);
        group.addView(mView, lp);
    } else {
        findViewById(R.id.view).setVisibility(View.GONE);
    }

    // Custom Adapter
    if (mAdapter != null) {
        ListView list = (ListView) findViewById(R.id.list);
        list.setAdapter(mAdapter);
        if (mSingleChoice) {
            list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        }
        if (mSingleChoice && 0 <= mCheckedItem && mCheckedItem < mAdapter.getCount()) {
            list.setItemChecked(mCheckedItem, true);
            list.setSelectionFromTop(mCheckedItem, 0);
        }
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mListItemListener != null) {
                    mListItemListener.onItemClick(parent, view, position, id);
                }
                dismiss();
            }
        });
    } else {
        findViewById(R.id.list).setVisibility(View.GONE);
    }

    // Positive Button
    boolean hasPositiveButton = false;
    if (mPositiveButtonText != null) {
        hasPositiveButton = true;
        ((TextView) findViewById(R.id.button_positive_label)).setText(mPositiveButtonText);
        if (mButtonTextStyle != 0) {
            ((TextView) findViewById(R.id.button_positive_label)).setTextAppearance(getContext(),
                    mButtonTextStyle);
        }
    }
    if (mPositiveButtonListener != null) {
        hasPositiveButton = true;
        findViewById(R.id.button_positive).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                if (mPositiveButtonListener != null) {
                    mPositiveButtonListener.onClick(SimpleAlertDialog.this, 0);
                }
                dismiss();
            }
        });
    }
    if (!hasPositiveButton) {
        findViewById(R.id.button_positive).setVisibility(View.GONE);
    }

    // Neutral Button
    boolean hasNeutralButton = false;
    if (mNeutralButtonText != null) {
        hasNeutralButton = true;
        ((TextView) findViewById(R.id.button_neutral_label)).setText(mNeutralButtonText);
        if (mButtonTextStyle != 0) {
            ((TextView) findViewById(R.id.button_neutral_label)).setTextAppearance(getContext(),
                    mButtonTextStyle);
        }
    }
    if (mNeutralButtonListener != null) {
        hasNeutralButton = true;
        findViewById(R.id.button_neutral).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                if (mNeutralButtonListener != null) {
                    mNeutralButtonListener.onClick(SimpleAlertDialog.this, 0);
                }
                dismiss();
            }
        });
    }
    if (!hasNeutralButton) {
        findViewById(R.id.button_neutral).setVisibility(View.GONE);
    }

    // Negative Button
    boolean hasNegativeButton = false;
    if (mNegativeButtonText != null) {
        hasNegativeButton = true;
        ((TextView) findViewById(R.id.button_negative_label)).setText(mNegativeButtonText);
        if (mButtonTextStyle != 0) {
            ((TextView) findViewById(R.id.button_negative_label)).setTextAppearance(getContext(),
                    mButtonTextStyle);
        }
    }
    if (mNegativeButtonListener != null) {
        hasNegativeButton = true;
        findViewById(R.id.button_negative).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                if (mNegativeButtonListener != null) {
                    mNegativeButtonListener.onClick(SimpleAlertDialog.this, 1);
                }
                dismiss();
            }
        });
    }
    if (!hasNegativeButton) {
        findViewById(R.id.button_negative).setVisibility(View.GONE);
    }

    if (!hasPositiveButton && !hasNegativeButton) {
        findViewById(R.id.button_divider_top).setVisibility(View.GONE);
        findViewById(R.id.button_divider).setVisibility(View.GONE);
    } else if (!hasPositiveButton || !hasNegativeButton) {
        findViewById(R.id.button_divider).setVisibility(View.GONE);
        setBackground(R.id.button_divider_top, mButtonTopDividerBackground);
    } else {
        setBackground(R.id.button_divider_top, mButtonTopDividerBackground);
        setBackground(R.id.button_divider, mButtonVerticalDividerBackground);
    }
    if (hasNeutralButton) {
        setBackground(R.id.button_divider_neutral, mButtonVerticalDividerBackground);
    } else {
        findViewById(R.id.button_divider_neutral).setVisibility(View.GONE);
    }
}

From source file:com.todotxt.todotxttouch.TodoTxtTouch.java

void setFilteredTasks(boolean reload) {
    if (reload) {
        try {//from w ww  . ja  v a2  s . c  om
            taskBag.reload();
        } catch (Exception e) {
            Log.e(TAG, e.getMessage(), e);
        }
    }

    if (mScrollPosition < 0) {
        calculateScrollPosition();
    }

    m_adapter.clear();
    for (Task task : taskBag.getTasks(FilterFactory.generateAndFilter(m_app.m_prios, m_app.m_contexts,
            m_app.m_projects, m_app.m_search, false), m_app.sort.getComparator())) {
        m_adapter.add(task);
    }

    ListView lv = getListView();
    lv.setSelectionFromTop(mScrollPosition, mScrollTop);

    final TextView filterText = (TextView) findViewById(R.id.filter_text);
    final LinearLayout actionbar = (LinearLayout) findViewById(R.id.actionbar);
    final ImageView actionbar_icon = (ImageView) findViewById(R.id.actionbar_icon);

    if (filterText != null) {
        if (m_app.m_filters.size() > 0) {
            String filterTitle = getString(R.string.title_filter_applied) + " ";
            int count = m_app.m_filters.size();

            for (int i = 0; i < count; i++) {
                filterTitle += m_app.m_filters.get(i) + " ";
            }

            if (!Strings.isEmptyOrNull(m_app.m_search)) {
                filterTitle += getString(R.string.filter_tab_search);
            }

            actionbar_icon.setImageResource(R.drawable.ic_actionbar_filter);

            actionbar.setVisibility(View.VISIBLE);
            filterText.setText(filterTitle);
        } else if (!Strings.isEmptyOrNull(m_app.m_search)) {
            if (filterText != null) {
                actionbar_icon.setImageResource(R.drawable.ic_actionbar_search);
                filterText.setText(getString(R.string.title_search_results) + " " + m_app.m_search);

                actionbar.setVisibility(View.VISIBLE);
            }
        } else {
            filterText.setText("");
            actionbar.setVisibility(View.GONE);
        }
    }

    m_swipeList.setEnabled(!inActionMode());
}

From source file:nl.mpcjanssen.simpletask.Simpletask.java

private void updateFilterBar() {
    ListView lv = getListView();
    int index = lv.getFirstVisiblePosition();
    View v = lv.getChildAt(0);//from   w ww . j a  v a 2  s .  co  m
    int top = (v == null) ? 0 : v.getTop();
    lv.setSelectionFromTop(index, top);

    final LinearLayout actionbar = (LinearLayout) findViewById(R.id.actionbar);
    final TextView filterText = (TextView) findViewById(R.id.filter_text);
    if (mFilter.hasFilter()) {
        actionbar.setVisibility(View.VISIBLE);
    } else {
        actionbar.setVisibility(View.GONE);
    }
    int count = m_adapter != null ? m_adapter.getCountVisbleTasks() : 0;
    int total = getTaskBag() != null ? getTaskBag().size() : 0;

    filterText.setText(mFilter.getTitle(count, total, getText(R.string.priority_prompt),
            getText(R.string.project_prompt), getText(R.string.context_prompt), getText(R.string.search),
            getText(R.string.script), getText(R.string.title_filter_applied), getText(R.string.no_filter)));
}

From source file:org.onebusaway.android.ui.TripDetailsListFragment.java

private void setTripDetails(ObaTripDetailsResponse data) {
    mTripInfo = data;/*from w  ww  .j  a v a 2  s . c o m*/

    final int code = mTripInfo.getCode();
    if (code == ObaApi.OBA_OK) {
        setEmptyText("");
    } else {
        setEmptyText(UIUtils.getRouteErrorString(getActivity(), code));
        return;
    }

    setUpHeader();
    final ListView listView = getListView();

    if (mAdapter == null) { // first time displaying list
        mAdapter = new TripDetailsAdapter();
        getListView().setDivider(null);
        setListAdapter(mAdapter);

        // Scroll to stop if we have the stopId available
        if (mStopId != null) {
            mStopIndex = findIndexForStop(mTripInfo.getSchedule().getStopTimes(), mStopId);
            if (mStopIndex != null) {
                listView.post(new Runnable() {
                    @Override
                    public void run() {
                        listView.setSelection(mStopIndex);
                    }
                });
            }
        } else {
            // If we don't have a stop, then scroll to the current position of the bus
            final Integer nextStop = mAdapter.getNextStopIndex();
            if (nextStop != null) {
                listView.post(new Runnable() {
                    @Override
                    public void run() {
                        listView.setSelection(nextStop - 1);
                    }
                });
            }
        }
        mAdapter.notifyDataSetChanged();
    } else { // refresh, keep scroll position
        int index = listView.getFirstVisiblePosition();
        View v = listView.getChildAt(0);
        int top = (v == null) ? 0 : v.getTop();

        mAdapter.notifyDataSetChanged();
        listView.setSelectionFromTop(index, top);
    }
}

From source file:com.sentaroh.android.TaskAutomation.Config.ProfileMaintenanceTaskProfile.java

private void restoreViewContents(final SavedViewContents sv) {
    final EditText dlg_prof_name_et = (EditText) mDialog.findViewById(R.id.edit_profile_task_profile_et_name);
    final CheckBox cb_active = (CheckBox) mDialog.findViewById(R.id.edit_profile_task_enabled);
    final CheckBox cb_notification = (CheckBox) mDialog.findViewById(R.id.edit_profile_task_error_notification);
    final Spinner spinnerTriggerCat = (Spinner) mDialog
            .findViewById(R.id.edit_profile_task_exec_trigger_category);
    final Spinner spinnerEvent = (Spinner) mDialog.findViewById(R.id.edit_profile_task_exec_trigger_event);
    final CheckBox cb_enable_env_parms = (CheckBox) mDialog
            .findViewById(R.id.edit_profile_task_enable_env_parms);
    final Spinner spinnerActionProfile = (Spinner) mDialog.findViewById(R.id.edit_profile_task_user_actionlist);
    final Spinner spinnerBuiltinPrimitiveAction = (Spinner) mDialog
            .findViewById(R.id.edit_profile_task_builtin_primitive_actionlist);
    final Spinner spinnerBuiltinConditionalAction = (Spinner) mDialog
            .findViewById(R.id.edit_profile_task_builtin_conditional_actionlist);
    final Spinner spinnerBuiltinCancelAction = (Spinner) mDialog
            .findViewById(R.id.edit_profile_task_builtin_cancel_actionlist);
    final Spinner spinnerBuiltinBlockAction = (Spinner) mDialog
            .findViewById(R.id.edit_profile_task_builtin_block_actionlist);
    final ListView lv_act_list = (ListView) mDialog.findViewById(android.R.id.list);
    final Spinner spinnerSelectAction = (Spinner) mDialog.findViewById(R.id.edit_profile_task_select_action);

    Handler hndl1 = new Handler();
    hndl1.postDelayed(new Runnable() {
        @Override/*from  w  w w.jav  a 2 s  . co  m*/
        public void run() {
            dlg_prof_name_et.setText(sv.dlg_prof_name_et);
            dlg_prof_name_et.setSelection(sv.dlg_prof_name_et_spos, sv.dlg_prof_name_et_epos);
            cb_active.setChecked(sv.cb_active);
            cb_notification.setChecked(sv.cb_notification);
            if (spinnerTriggerCat.getSelectedItemPosition() != sv.spinnerTriggerCat) {
                spinnerTriggerCat.setSelection(sv.spinnerTriggerCat);
            }
            Handler hndl2 = new Handler();
            hndl2.postDelayed(new Runnable() {
                @Override
                public void run() {
                    spinnerEvent.setSelection(sv.spinnerEvent);

                    lv_act_list.setSelectionFromTop(sv.lv_act_list[0], sv.lv_act_list[1]);
                    for (int i = 0; i < mGlblParms.taskActionListAdapter.getCount(); i++)
                        mGlblParms.taskActionListAdapter.remove(0);
                    for (int i = 0; i < sv.action_adapter_list.size(); i++)
                        mGlblParms.taskActionListAdapter.add(sv.action_adapter_list.get(i));
                    mGlblParms.taskActionListAdapter.notifyDataSetChanged();
                }
            }, 50);
            cb_enable_env_parms.setChecked(sv.cb_enable_env_parms);

            spinnerActionProfile.setSelection(sv.spinnerActionProfile);
            spinnerBuiltinPrimitiveAction.setSelection(sv.spinnerBuiltinPrimitiveAction);
            spinnerBuiltinConditionalAction.setSelection(sv.spinnerBuiltinConditionalAction);
            spinnerBuiltinCancelAction.setSelection(sv.spinnerBuiltinCancelAction);
            spinnerBuiltinBlockAction.setSelection(sv.spinnerBuiltinBlockAction);
            spinnerSelectAction.setSelection(sv.spinnerSelectAction);
        }
    }, 50);
}