android.widget.ListView.setSelection()方法的使用及代码示例

x33g5p2x  于2022-01-23 转载在 其他  
字(8.4k)|赞(0)|评价(0)|浏览(229)

本文整理了Java中android.widget.ListView.setSelection()方法的一些代码示例,展示了ListView.setSelection()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ListView.setSelection()方法的具体详情如下:
包路径:android.widget.ListView
类名称:ListView
方法名:setSelection

ListView.setSelection介绍

暂无

代码示例

代码示例来源:origin: jeasonlzy/ImagePicker

public void setSelection(int selection) {
  listView.setSelection(selection);
}

代码示例来源:origin: alexvasilkov/GestureViews

@Override
void scrollToPosition(ListView list, int pos) {
  list.setSelection(pos);
}

代码示例来源:origin: LitePalFramework/LitePal

private void refreshListView(long id, String name, int age, int isMale) {
  List<String> stringList = new ArrayList<String>();
  stringList.add(String.valueOf(id));
  stringList.add(name);
  stringList.add(String.valueOf(age));
  stringList.add(String.valueOf(isMale));
  mList.add(stringList);
  mAdapter.notifyDataSetChanged();
  mDataListView.setSelection(mList.size());
}

代码示例来源:origin: stackoverflow.com

ChatAdapter adapter = new ChatAdapter(this);

ListView lv = (ListView) findViewById(R.id.chatList);
lv.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
lv.setAdapter(adapter);

adapter.registerDataSetObserver(new DataSetObserver() {
  @Override
  public void onChanged() {
    super.onChanged();
    lv.setSelection(adapter.getCount() - 1);    
  }
});

代码示例来源:origin: android-hacker/VirtualXposed

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  if (mAlwaysUseOption) {
    final int checkedPos = mListView.getCheckedItemPosition();
    final boolean enabled = checkedPos != ListView.INVALID_POSITION;
    mLastSelected = checkedPos;
    mAlwaysButton.setEnabled(enabled);
    mOnceButton.setEnabled(enabled);
    if (enabled) {
      mListView.setSelection(checkedPos);
    }
  }
}

代码示例来源:origin: k9mail/k-9

@Override
public void handleMessage(android.os.Message msg) {
  switch (msg.what) {
    case MSG_PROGRESS: {
      setProgressBarIndeterminateVisibility(msg.arg1 != 0);
      break;
    }
    case MSG_SET_SELECTED_FOLDER: {
      getListView().setSelection(msg.arg1);
      break;
    }
  }
}

代码示例来源:origin: robolectric/robolectric

@Test
public void testSetSelection_WhenNoItemSelectedListenerIsSet_ShouldDoNothing() throws Exception {
 listView.setSelection(0);
}

代码示例来源:origin: guolindev/booksource

/**
 * 查询全国所有的省,优先从数据库查询,如果没有查询到再去服务器上查询。
 */
private void queryProvinces() {
  titleText.setText("中国");
  backButton.setVisibility(View.GONE);
  provinceList = DataSupport.findAll(Province.class);
  if (provinceList.size() > 0) {
    dataList.clear();
    for (Province province : provinceList) {
      dataList.add(province.getProvinceName());
    }
    adapter.notifyDataSetChanged();
    listView.setSelection(0);
    currentLevel = LEVEL_PROVINCE;
  } else {
    String address = "http://guolin.tech/api/china";
    queryFromServer(address, "province");
  }
}

代码示例来源:origin: k9mail/k-9

public void onMoveUp() {
  int currentPosition = listView.getSelectedItemPosition();
  if (currentPosition == AdapterView.INVALID_POSITION || listView.isInTouchMode()) {
    currentPosition = listView.getFirstVisiblePosition();
  }
  if (currentPosition > 0) {
    listView.setSelection(currentPosition - 1);
  }
}

代码示例来源:origin: stackoverflow.com

final ListView listView = ...;
View listItemView = ...;
listView.smoothScrollBy(listItemView.getHeight() * NUMBER_OF_VIEWS, 
  DURATION * 2);
listView.postDelayed(new Runnable() {
  public void run() {
    listView.smoothScrollBy(0, 0); // Stops the listview from overshooting.
    listView.setSelection(0);
  }
}, DURATION);

代码示例来源:origin: seven332/EhViewer

private void updateSuggestions(boolean scrollToTop) {
  mSuggestionList.clear();
  String text = mEditText.getText().toString();
  if (mSuggestionProvider != null) {
    List<Suggestion> suggestions = mSuggestionProvider.providerSuggestions(text);
    if (suggestions != null && !suggestions.isEmpty()) {
      mSuggestionList.addAll(suggestions);
    }
  }
  String[] keywords = mSearchDatabase.getSuggestions(text, 128);
  for (String keyword : keywords) {
    mSuggestionList.add(new KeywordSuggestion(keyword));
  }
  if (mSuggestionList.size() == 0) {
    removeListHeader();
  } else {
    addListHeader();
  }
  mSuggestionAdapter.notifyDataSetChanged();
  if (scrollToTop) {
    mListView.setSelection(0);
  }
}

代码示例来源:origin: k9mail/k-9

public void onMoveDown() {
  int currentPosition = listView.getSelectedItemPosition();
  if (currentPosition == AdapterView.INVALID_POSITION || listView.isInTouchMode()) {
    currentPosition = listView.getFirstVisiblePosition();
  }
  if (currentPosition < listView.getCount()) {
    listView.setSelection(currentPosition + 1);
  }
}

代码示例来源:origin: guolindev/booksource

/**
 * 查询选中省内所有的市,优先从数据库查询,如果没有查询到再去服务器上查询。
 */
private void queryCities() {
  titleText.setText(selectedProvince.getProvinceName());
  backButton.setVisibility(View.VISIBLE);
  cityList = DataSupport.where("provinceid = ?", String.valueOf(selectedProvince.getId())).find(City.class);
  if (cityList.size() > 0) {
    dataList.clear();
    for (City city : cityList) {
      dataList.add(city.getCityName());
    }
    adapter.notifyDataSetChanged();
    listView.setSelection(0);
    currentLevel = LEVEL_CITY;
  } else {
    int provinceCode = selectedProvince.getProvinceCode();
    String address = "http://guolin.tech/api/china/" + provinceCode;
    queryFromServer(address, "city");
  }
}

代码示例来源:origin: k9mail/k-9

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
  // Shortcuts that work no matter what is selected
  if (K9.useVolumeKeysForListNavigationEnabled() &&
      (keyCode == KeyEvent.KEYCODE_VOLUME_UP ||
          keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)) {
    final ListView listView = getListView();
    int currentPosition = listView.getSelectedItemPosition();
    if (currentPosition == AdapterView.INVALID_POSITION || listView.isInTouchMode()) {
      currentPosition = listView.getFirstVisiblePosition();
    }
    if (keyCode == KeyEvent.KEYCODE_VOLUME_UP && currentPosition > 0) {
      listView.setSelection(currentPosition - 1);
    } else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN &&
        currentPosition < listView.getCount()) {
      listView.setSelection(currentPosition + 1);
    }
    return true;
  }
  return super.onKeyDown(keyCode, event);
}

代码示例来源:origin: guolindev/booksource

/**
 * 查询选中市内所有的县,优先从数据库查询,如果没有查询到再去服务器上查询。
 */
private void queryCounties() {
  titleText.setText(selectedCity.getCityName());
  backButton.setVisibility(View.VISIBLE);
  countyList = DataSupport.where("cityid = ?", String.valueOf(selectedCity.getId())).find(County.class);
  if (countyList.size() > 0) {
    dataList.clear();
    for (County county : countyList) {
      dataList.add(county.getCountyName());
    }
    adapter.notifyDataSetChanged();
    listView.setSelection(0);
    currentLevel = LEVEL_COUNTY;
  } else {
    int provinceCode = selectedProvince.getProvinceCode();
    int cityCode = selectedCity.getCityCode();
    String address = "http://guolin.tech/api/china/" + provinceCode + "/" + cityCode;
    queryFromServer(address, "county");
  }
}

代码示例来源:origin: k9mail/k-9

private void openMessageAtPosition(int position) {
  // Scroll message into view if necessary
  int listViewPosition = adapterToListViewPosition(position);
  if (listViewPosition != AdapterView.INVALID_POSITION &&
      (listViewPosition < listView.getFirstVisiblePosition() ||
      listViewPosition > listView.getLastVisiblePosition())) {
    listView.setSelection(listViewPosition);
  }
  MessageReference ref = getReferenceForPosition(position);
  // For some reason the listView.setSelection() above won't do anything when we call
  // onOpenMessage() (and consequently adapter.notifyDataSetChanged()) right away. So we
  // defer the call using MessageListHandler.
  handler.openMessage(ref);
}

代码示例来源:origin: jaydenxiao2016/AndroidFire

@Override
public void onClick(View v) {
  if (v.getId() == btnAlbumSelected.getId()) {
    if (folderPopupWindow == null) {
      WindowManager wm = getActivity().getWindowManager();
      int width = wm.getDefaultDisplay().getWidth();
      createPopupFolderList(width / 3 * 2, width / 3 * 2);
    }
    if (folderPopupWindow.isShowing()) {
      folderPopupWindow.dismiss();
    } else {
      folderPopupWindow.show();
      int index = folderListAdapter.getSelectIndex();
      index = index == 0 ? index : index - 1;
      folderPopupWindow.getListView().setSelection(index);
    }
  }
}

代码示例来源:origin: lovetuzitong/MultiImageSelector

@Override
  public void onClick(View view) {
    if(mFolderPopupWindow == null){
      createPopupFolderList();
    }
    if (mFolderPopupWindow.isShowing()) {
      mFolderPopupWindow.dismiss();
    } else {
      mFolderPopupWindow.show();
      int index = mFolderAdapter.getSelectIndex();
      index = index == 0 ? index : index - 1;
      mFolderPopupWindow.getListView().setSelection(index);
    }
  }
});

代码示例来源:origin: stackoverflow.com

final ListView list = (ListView) findViewById(R.id.list);
list.post(new Runnable() {            
  @Override
  public void run() {
    list.setSelection(0);
  }
});

代码示例来源:origin: stackoverflow.com

public static void ensureVisible(ListView listView, int pos)
{
  if (listView == null)
  {
    return;
  }

  if(pos < 0 || pos >= listView.getCount())
  {
    return;
  }

  int first = listView.getFirstVisiblePosition();
  int last = listView.getLastVisiblePosition();

  if (pos < first)
  {
    listView.setSelection(pos);
    return;
  }

  if (pos >= last)
  {
    listView.setSelection(1 + pos - (last - first));
    return;
  }
}

相关文章

ListView类方法