简体   繁体   English

Android ListAdapter开始@顶部

[英]Android ListAdapter start @ top

Currently the list when populated is starting with the view @ the bottom of the list. 当前,列表在填充时是从列表底部的@视图开始的。 Is there a way using listAdapters to force it to the top of the list? 有没有一种方法可以使用listAdapters将其强制到列表顶部?

Currently the orientation scrolls to the bottom on create. 当前方向在创建时滚动到底部。 Is there a way to pin the screen to the top when it creates? 创建屏幕时,是否可以将屏幕固定在顶部? http://imgur.com/wGTEy in this example you see that entry 1 on create is shoved upwards to make room for six... Instead I want it to populate like this. 在此示例中,您会看到http://imgur.com/wGTEy向上推了create的条目1,以便为六个条目腾出空间。相反,我希望它像这样填充。 http://imgur.com/6Lg6e ... entry 1 is the top of the list and 6 is pushed off to the bottom for the scroll. http://imgur.com/6Lg6e ...条目1是列表的顶部,条目6被推至底部以进行滚动。

If you look at the picture above you will notice it starts at the bottom of the list instead of at the top. 如果您看上面的图片,您会发现它从列表的底部而不是顶部开始。 Any Ideas? 有任何想法吗?

mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings);
        setListAdapter(mAdapter);
        registerForContextMenu(getListView());


populateFields();

private void populateFields() {
       if (mRowId != null) {
           Cursor note = mDbHelper.fetchDaily(mRowId);
           startManagingCursor(note);
           String body = note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_DBODY));
           mAdapter.clear();
           if (!(body.trim().equals(""))){
               String bodysplit[] = body.split(",");
               for (int i = 0; i < bodysplit.length; i++) {
               mAdapter.add(bodysplit[i].trim());
               } 
           }
       }
   }

**edited to fix != string error. **编辑修复!=字符串错误。

You want the items later in the list to be at the top of the ListView ? 您希望列表后面的项目在ListView的顶部吗? If so, check out this questions: Is it possible to make a ListView populate from the bottom? 如果是这样,请检查以下问题: 是否可以从底部填充ListView?

You are completely changing the adapter, so the scroll position is lost in the process... You can use: 您将完全更换适配器,因此在此过程中滚动位置将丢失。您可以使用:

ListView listView = getListView();
int position = listView.getFirstVisiblePosition();
if (!(body.trim().equals(""))){
   String bodysplit[] = body.split(",");
   for (int i = 0; i < bodysplit.length; i++) {
      mAdapter.add(bodysplit[i].trim());
   } 
}

listView.setSelection(position);

But this is not perfect as it is, if a row is added before position the index will be off. 但这并不是完美的,如果在position之前添加一行,索引将关闭。 If your list contains unique values you can use ArrayAdapter#getPosition() , to find the new index. 如果列表包含唯一值,则可以使用ArrayAdapter#getPosition()来查找新索引。


While I still recommend using a CursorAdapter, because it handles large table data better, I want to address a point on efficiency with your ArrayAdapter code. 虽然我仍然建议使用CursorAdapter,因为它可以更好地处理大表数据,但我想用ArrayAdapter代码解决效率问题。

By using adapter.clear() and adapter.add() you are asking the ListView to redraw itself on every step... potentially dozens or hundreds of times. 通过使用adapter.clear()adapter.add()您要求ListView在每个步骤上重绘自身……可能数十次或数百次。 Instead you should work with the ArrayList directly and then ask the ListView to redraw once itself with ArrayAdapter#notifyDataSetChanged() after the loop completes. 相反,您应该直接使用ArrayList,然后在循环完成后要求ListView使用ArrayAdapter#notifyDataSetChanged()重绘一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM