【问题标题】:Listview is automatically scrolling to the topListview 自动滚动到顶部
【发布时间】:2015-02-11 10:30:47
【问题描述】:

我的项目中有一个列表视图,它包含图像和信息。
所以假设它包含 50 个人信息。 现在,如果我想删除最后两项,在删除最后一项后,listlview 会自动聚焦到列表顶部。
所以现在用户已经向下滚动所有项目以到达第 49 个项目并将其删除。
那么有什么方法可以在我删除项目的那个位置修复列表视图位置。

//删除信息的代码

public void deleteFeed(int rowID) {
        // fetch the image File name
        Cursor cursor = dbAdapter.getTimelineFeedByID(mSysPrefs.getBabyID(), rowID);
        if(cursor != null && cursor.moveToFirst()) {
            // fetch from DB Cursor
            String imageFileName = cursor.getString(cursor.getColumnIndex(DatabaseAppHelper.KEY_IMAGENAME));
            File file = new File(GlobalConstants.FILE_PATH, imageFileName + GlobalConstants.IMAGE_EXTENSION);
            if(file.exists()) {
                file.delete();
            }
            //listview.smoothScrollToPosition(0, listview.getHeight());
            listview.smoothScrollToPosition(0, listview.getBottom());
        }
        // now delete the record from the DB    
        if(dbAdapter.deleteTimelineFeed(rowID) != -1) {
            onResume();
        } else {
            Toast.makeText(this, "Please try again..", Toast.LENGTH_LONG).show();
        }
    }

//创建列表视图的代码

@Override
protected void onResume() {
    super.onResume();
     turnGPSOn(); // method to turn on the GPS if its in off state.
     getMyCurrentLocation();  
    dbAdapter.openDBConnection();
    new Handler().post(new Runnable() {
        @Override
        public void run() {
            timelineCursorAdapter = new TimelineCursorAdapter(TimelineViewActivity.this,
                    dbAdapter.getAllTimelineFeedsForBaby(mSysPrefs.getBabyID()), 0);
            listview.setAdapter(timelineCursorAdapter);
            //timelineCursorAdapter.notifyDataSetChanged();
            listview.invalidateViews();

        }
    });
}

【问题讨论】:

  • 试试listView.setSelectionAfterHeaderView();list.smoothScrollToPosition(0);
  • 您可以发布您用来执行此操作的代码吗?这样帮助你会更容易

标签: android listview


【解决方案1】:

从您删除的项目中获取位置,然后将位置传递给 setselection 方法,如下所示:listview.setSelection(pos);

【讨论】:

  • 部分解决了这个问题。我还有一个问题。无论如何非常感谢
【解决方案2】:

试试下面的代码

在删除列表项之前使用以下代码获取可见位置

       mIndex = listView.getFirstVisiblePosition();
        View v = listView.getChildAt(0);
        mTop = (v == null) ? 0 : v.getTop();

删除项目后将该位置设置为列表视图

    listView.setSelectionFromTop(mIndex, mTop);

【讨论】:

    【解决方案3】:

    getCount() 返回适配器持有的当前项目位置。以下方法可以通过在 onResume() 中调用来提供帮助

    mListView.smoothScrollToPosition(mAdapter.getCount());

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-24
      • 1970-01-01
      相关资源
      最近更新 更多