Example usage for android.widget ListView setSelection

List of usage examples for android.widget ListView setSelection

Introduction

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

Prototype

@Override
public void setSelection(int position) 

Source Link

Document

Sets the currently selected item.

Usage

From source file:com.kaku.weac.fragment.RecorderFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    LogUtil.i(LOG_TAG, "onCreateView()");
    View view = inflater.inflate(R.layout.fm_ring_recorder, container, false);

    // ListView//from  ww w .j  a  v a 2  s  .c o  m
    ListView recordListView = (ListView) view.findViewById(R.id.ring_record_lv);
    // ListTextView
    TextView emptyView = (TextView) view.findViewById(R.id.ring_record_empty);
    // List
    recordListView.setEmptyView(emptyView);
    recordListView.setAdapter(mRecorderAdapter);
    // ??
    recordListView.setSelection(mPosition);
    recordListView.setOnItemClickListener(new OnItemClickListenerImpl());
    recordListView.setOnItemLongClickListener(new onItemLongClickListenerImpl());
    // 
    mRecordBtn = (ImageButton) view.findViewById(R.id.ring_record_record);
    mStopBtn = (ImageButton) view.findViewById(R.id.ring_record_stop);
    mRecordButtonInfo = (TextView) view.findViewById(R.id.ring_record_button_info);
    mRecordBtn.setOnClickListener(this);
    mStopBtn.setOnClickListener(this);

    // ?
    mRecordMic = (ViewGroup) view.findViewById(R.id.ring_record_mic_llyt);
    mRecordMicStatusImage = (ImageView) view.findViewById(R.id.ring_record_mic_status);
    mRecordTime = (TextView) view.findViewById(R.id.ring_record_time);
    return view;
}

From source file:com.android.talkback.controller.CursorControllerAppTest.java

@MediumTest
public void testUp_doWrap_doScroll_doUseInputFocus() {
    if (!checkApiLevelSupportsDirectional()) {
        return;/*from  www  .j a  va 2s  .co m*/
    }

    setContentView(R.layout.cursor_test);

    // Scroll teams list to the last item.
    getInstrumentation().runOnMainSync(new Runnable() {
        @Override
        public void run() {
            ListView teamsListView = (ListView) getViewForId(R.id.teams_list);
            teamsListView.setSelection(teamsListView.getCount() - 1);
        }
    });
    getInstrumentation().waitForIdleSync();

    // Put input focus on button AFTER the teams list is done updating.
    getInstrumentation().runOnMainSync(new Runnable() {
        @Override
        public void run() {
            View squareButton = getViewForId(R.id.button_square);
            squareButton.requestFocus();
        }
    });
    getInstrumentation().waitForIdleSync();
    waitForAccessibilityIdleSync();

    AccessibilityNodeInfoCompat teamsList = getNodeForId(R.id.teams_list);
    AccessibilityNodeInfoCompat checkBox = getNodeForId(R.id.check_me);
    AccessibilityNodeInfoCompat firstLabel = getNodeForId(R.id.text_first_item);
    AccessibilityNodeInfoCompat lastLabel = getNodeForId(R.id.text_last_item);

    // This is the expected traversal:
    // (Initial focus) button_square - has input focus
    // 1. text_first_item
    // 2. text_first_item - first previous() pauses on node
    // 3. text_last_item - second previous() wraps around
    // 4-12: teams_list items
    // 13. check_me
    final int traversals = 13;
    List<AccessibilityNodeInfoCompat> nodes = navigate(TraversalStrategy.SEARCH_FOCUS_UP, traversals, true,
            true, true);

    assertEquals(firstLabel, nodes.get(0));
    assertEquals(firstLabel, nodes.get(1));
    assertEquals(lastLabel, nodes.get(2));
    assertHasParent(teamsList, nodes.subList(3, traversals - 1));
    assertEquals(checkBox, nodes.get(traversals - 1));
}

From source file:org.catrobat.catroid.ui.fragment.SoundFragment.java

public void updateSoundAdapter(SoundInfo newSoundInfo) {

    if (soundInfoListChangedAfterNewListener != null) {
        soundInfoListChangedAfterNewListener.onSoundInfoListChangedAfterNew(newSoundInfo);
    }/*  w w w  .j a v  a 2s. c o m*/

    //scroll down the list to the new item:
    final ListView listView = getListView();
    listView.post(new Runnable() {
        @Override
        public void run() {
            listView.setSelection(listView.getCount() - 1);
        }
    });

    if (isResultHandled) {
        isResultHandled = false;

        ScriptActivity scriptActivity = (ScriptActivity) getActivity();
        if (scriptActivity.getIsSoundFragmentFromPlaySoundBrickNew()
                && scriptActivity.getIsSoundFragmentHandleAddButtonHandled()) {
            SoundController.getInstance().switchToScriptFragment(this);
        }
    }
}

From source file:de.egore911.drilog.tasks.JsonLoadTask.java

@Override
protected void onPostExecute(String json) {
    ShowDataAdapter result;//  www  .j  a  v a2s  . c  om

    JSONObject o = null;
    try {
        if (json != null) {
            o = new JSONObject(json);
        }
    } catch (JSONException e) {
        Log.e(getClass().getSimpleName(), e.getMessage(), e);
        exceptionMessage = e.getClass().getSimpleName() + ": " + e.getLocalizedMessage();
    }

    result = (ShowDataAdapter) logviewFragment.getListAdapter();
    if (result == null) {
        result = new ShowDataAdapter(activity, o, watchList);
    } else {
        result.updateElements(o, watchList);
    }

    if (exceptionMessage != null) {
        Toast.makeText(activity, exceptionMessage, Toast.LENGTH_LONG).show();
    }
    activity.setTitle(result.channel);
    activity.updateDateText(DateUtil.getDateString(result.date == null ? new Date() : result.date));
    ListView listView = null;
    int oldposition;
    try {
        listView = logviewFragment.getListView();
        oldposition = listView.getFirstVisiblePosition();
    } catch (IllegalStateException e) {
        oldposition = -1;
    }
    logviewFragment.setListAdapter(result);

    // Now do some scrolling
    String anchor = activity.getAnchor();
    activity.setAnchor(null);
    if (anchor != null) {
        List<ListElement> listElements = result.getListElements();
        int position = 0;
        for (ListElement listElement : listElements) {
            if (listElement instanceof Comment) {
                Comment c = (Comment) listElement;
                if (c.anchor.compareTo(anchor) >= 0) {
                    result.anchor = c;
                    break;
                }
            }
            position++;
        }
        if (listView != null) {
            //listView.smoothScrollToPosition(position);
            listView.setSelection(position);
        }
    } else {
        if (oldposition >= 0 && listView != null) {
            //listView.smoothScrollToPosition(oldposition);
        }
    }

    fragmentManager.beginTransaction().replace(R.id.content_frame, logviewFragment).commit();

    // FIXME crashes on Android 21: activity.setSupportProgressBarIndeterminateVisibility(false);
}

From source file:com.tweetlanes.android.core.view.DirectMessageFeedFragment.java

@Override
public void onJumpToTop() {
    if (mConversationListView != null) {
        ListView listView = mConversationListView.getRefreshableView();
        if (listView != null && listView.getAdapter() != null && !listView.getAdapter().isEmpty()) {
            listView.setSelection(0);
        }// w w  w  .  ja  v  a 2s. co  m
    }
}

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 va 2s  .  c om*/
    }

    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:org.onebusaway.android.ui.TripDetailsListFragment.java

private void setTripDetails(ObaTripDetailsResponse data) {
    mTripInfo = data;//from   www.  j  ava 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:android.support.v7.app.AlertController.java

private void setupView() {
    final View parentPanel = mWindow.findViewById(R.id.parentPanel);
    final View defaultTopPanel = parentPanel.findViewById(R.id.topPanel);
    final View defaultContentPanel = parentPanel.findViewById(R.id.contentPanel);
    final View defaultButtonPanel = parentPanel.findViewById(R.id.buttonPanel);

    // Install custom content before setting up the title or buttons so
    // that we can handle panel overrides.
    final ViewGroup customPanel = (ViewGroup) parentPanel.findViewById(R.id.customPanel);
    setupCustomContent(customPanel);/* w w w.j a  v a2 s .  c  om*/

    final View customTopPanel = customPanel.findViewById(R.id.topPanel);
    final View customContentPanel = customPanel.findViewById(R.id.contentPanel);
    final View customButtonPanel = customPanel.findViewById(R.id.buttonPanel);

    // Resolve the correct panels and remove the defaults, if needed.
    final ViewGroup topPanel = resolvePanel(customTopPanel, defaultTopPanel);
    final ViewGroup contentPanel = resolvePanel(customContentPanel, defaultContentPanel);
    final ViewGroup buttonPanel = resolvePanel(customButtonPanel, defaultButtonPanel);

    setupContent(contentPanel);
    setupButtons(buttonPanel);
    setupTitle(topPanel);

    final boolean hasCustomPanel = customPanel != null && customPanel.getVisibility() != View.GONE;
    final boolean hasTopPanel = topPanel != null && topPanel.getVisibility() != View.GONE;
    final boolean hasButtonPanel = buttonPanel != null && buttonPanel.getVisibility() != View.GONE;

    // Only display the text spacer if we don't have buttons.
    if (!hasButtonPanel) {
        if (contentPanel != null) {
            final View spacer = contentPanel.findViewById(R.id.textSpacerNoButtons);
            if (spacer != null) {
                spacer.setVisibility(View.VISIBLE);
            }
        }
    }

    if (hasTopPanel) {
        // Only clip scrolling content to padding if we have a title.
        if (mScrollView != null) {
            mScrollView.setClipToPadding(true);
        }
    }

    // Update scroll indicators as needed.
    if (!hasCustomPanel) {
        final View content = mListView != null ? mListView : mScrollView;
        if (content != null) {
            final int indicators = (hasTopPanel ? ViewCompat.SCROLL_INDICATOR_TOP : 0)
                    | (hasButtonPanel ? ViewCompat.SCROLL_INDICATOR_BOTTOM : 0);
            setScrollIndicators(contentPanel, content, indicators,
                    ViewCompat.SCROLL_INDICATOR_TOP | ViewCompat.SCROLL_INDICATOR_BOTTOM);
        }
    }

    final ListView listView = mListView;
    if (listView != null && mAdapter != null) {
        listView.setAdapter(mAdapter);
        final int checkedItem = mCheckedItem;
        if (checkedItem > -1) {
            listView.setItemChecked(checkedItem, true);
            listView.setSelection(checkedItem);
        }
    }
}

From source file:it.gulch.linuxday.android.activities.MainActivity.java

private void setupMainMenu() {
    mainMenu = findViewById(R.id.main_menu);
    ListView menuListView = (ListView) findViewById(R.id.main_menu_list);
    LayoutInflater inflater = LayoutInflater.from(this);
    View menuHeaderView = inflater.inflate(R.layout.header_main_menu, null);
    menuListView.addHeaderView(menuHeaderView, null, false);

    LocalBroadcastManager.getInstance(this).registerReceiver(scheduleRefreshedReceiver,
            new IntentFilter(ActionConstants.ACTION_SCHEDULE_REFRESHED));

    menuAdapter = new MainMenuAdapter(inflater);
    menuListView.setAdapter(menuAdapter);
    menuListView.setOnItemClickListener(this);

    // Last update date, below the menu
    lastUpdateTextView = (TextView) findViewById(R.id.last_update);
    updateLastUpdateTime();//from   www  . j  a  v  a2  s. com

    // Ensure the current section is visible in the menu
    menuListView.setSelection(currentSection.ordinal());
}

From source file:com.wenhui.syncedListView.demo.demo.SyncListViewContainerFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View root = inflater.inflate(R.layout.sync_list_fragment, null);
    mLayout = (SyncedListLayout) root;//from   w w  w  . j a  v  a 2s. c  om
    final ListView lvLeft = (ListView) root.findViewById(R.id.list_view_left);
    final ListView lvRight = (ListView) root.findViewById(R.id.list_view_right);
    int ivHeightLeft = getResources().getDimensionPixelSize(R.dimen.image_thumbnail_left_height);
    int ivHeightRight = getResources().getDimensionPixelSize(R.dimen.image_thumbnail_right_height);

    final ImageAdapter leftAdapter = new ImageAdapter(getActivity(), ivHeightLeft, Images.imageLeftThumbUrls);
    final ImageAdapter rightAdapter = new ImageAdapter(getActivity(), ivHeightRight,
            Images.imageRightThumbUrls);

    lvLeft.setAdapter(leftAdapter);
    lvRight.setAdapter(rightAdapter);

    lvLeft.setOnItemClickListener(mLeftListClickListener);
    lvRight.setOnItemClickListener(mRightListClickListener);

    lvLeft.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            removeGlobalLayoutListenerWrapper(lvLeft.getViewTreeObserver(), this);
            lvLeft.setSelection(200000);
        }
    });

    lvRight.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            removeGlobalLayoutListenerWrapper(lvRight.getViewTreeObserver(), this);
            int velocity = getResources().getDimensionPixelSize(R.dimen.animation_velocity);
            lvRight.setSelection(200000);
            mLayout.setAnimationVelocity(velocity);
            mLayout.startAnimation(100l);

        }
    });

    return root;
}