You need to enable JavaScript to run this app. 如何在返回ListView时保留上次选中的浏览位置?
优惠活动
大模型
产品
解决方案
定价
更多

如何在返回ListView时保留上次选中的浏览位置?

ListView跳转返回后丢失滚动位置解决方案

问题原因

位置丢失核心原因是:从下一级页面返回当前Activity时,触发了列表数据重新加载逻辑(调用import_Sup_listIndex/import_from_favorite等方法),刷新Adapter后没有恢复之前的滚动位置,因此列表默认回到顶部。

解决步骤

  • 第一步:在当前Activity中声明两个成员变量,用于存储滚动位置信息
// 存储列表第一个可见条目的位置
private int mLastVisiblePosition = 0;
// 存储第一个可见条目距离列表顶部的偏移量
private int mLastScrollOffset = 0;

注意将后续代码中出现的listView替换为你自己定义的ListView实例变量名。

  • 第二步:在跳转下一级页面前记录当前滚动位置
    在所有调用startActivity跳转到Show_Text_Activity的代码之前,添加如下位置记录逻辑:
// 记录当前滚动位置
mLastVisiblePosition = listView.getFirstVisiblePosition();
View firstItemView = listView.getChildAt(0);
mLastScrollOffset = firstItemView != null ? firstItemView.getTop() - listView.getPaddingTop() : 0;
  • 第三步:页面返回时恢复滚动位置
    在onResume方法中,完成列表数据加载、调用adapter.notifyDataSetChanged()之后,添加如下恢复逻辑:
// 等待ListView绘制完成后恢复位置
listView.post(() -> {
    listView.setSelectionFromTop(mLastVisiblePosition, mLastScrollOffset);
});

如果你的业务存在在同一页面切换listType(比如从index列表切换到Sup_index列表)的场景,可以通过HashMap为不同类型的列表分别存储对应的滚动位置,切换时读取对应值恢复即可。

额外优化建议

你当前的getView实现没有复用convertView,每次都重新inflate布局,会导致列表滑动卡顿,建议修改为ViewHolder复用模式:

@SuppressWarnings("deprecation")
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        // 首次加载时inflate布局,初始化控件
        convertView = View.inflate(getApplicationContext(), R.layout.row_itme, null);
        holder = new ViewHolder();
        holder.title = convertView.findViewById(R.id.textView_row_imte);
        holder.imgFav = convertView.findViewById(R.id.img_fav);
        // 字体只需要创建一次即可,不用每次滑动都重复加载
        Typeface typeface = Typeface.createFromAsset(getApplicationContext().getAssets(), "font.otf");
        holder.title.setTypeface(typeface);
        convertView.setTag(holder);
    } else {
        // 复用已有的布局和控件实例
        holder = (ViewHolder) convertView.getTag();
    }
    // 给控件赋值
    if (globalVar.getListType().equals("favorite") || globalVar.getListType().equals("search")) {
        holder.title.setText(Html.fromHtml("<font color=\"#ec4f00\"><b>"+mlistItmes.get(position).getMain_Title() +"</b></font>"+ " | " + mlistItmes.get(position).getSub_Title()));
    } else if (globalVar.getListType().equals("index")) {
        holder.title.setText(mlistItmes.get(position).getMain_Title());
    } else if (globalVar.getListType().equals("Sup_index")) {
        holder.title.setText(mlistItmes.get(position).getSub_Title());
    }
    final String page_id = mlistItmes.get(position).getPage_id();
    // 点击事件逻辑
    holder.title.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (globalVar.getListType().equals("index")) {
                globalVar.setListType("Sup_index");
                textView_Title.setText(mlistItmes.get(position).getMain_Title());
                globalVar.setMain_Title(mlistItmes.get(position).getMain_Title());
                import_Sup_listIndex(mlistItmes.get(position).getMain_Title());
            } else if (globalVar.getListType().equals("Sup_index") 
                    || globalVar.getListType().equals("favorite") 
                    || globalVar.getListType().equals("search")) {
                globalVar.setSub_Title(mlistItmes.get(position).getSub_Title());
                if (globalVar.getListType().equals("search")) {
                    globalVar.setMain_Title(mlistItmes.get(position).getMain_Title());
                }
                // 跳转前记录位置
                mLastVisiblePosition = listView.getFirstVisiblePosition();
                View firstItemView = listView.getChildAt(0);
                mLastScrollOffset = firstItemView != null ? firstItemView.getTop() - listView.getPaddingTop() : 0;
                
                Intent intpage = new Intent(MainActivity.this, com.rifai.asmaa7sna.Show_Text_Activity.class);
                intpage.putExtra("page_id", page_id);
                startActivity(intpage);
            }
        }
    });
    // 长按事件逻辑
    holder.title.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            if (globalVar.getListType().equals("favorite")) {
                AlertDialog.Builder build = new AlertDialog.Builder(MainActivity.this);
                build.setMessage(R.string.Delete_MessagText)
                        .setIcon(R.drawable.icon)
                        .setTitle(R.string.Delete_dilogTitle)
                        .setPositiveButton(R.string.dilog_yes, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                db_fav.Delete(mlistItmes.get(position).getId(), "favorite");
                                import_from_favorite();
                            }
                        })
                        .setNegativeButton(R.string.dilog_no, null)
                        .show();
            }
            return false;
        }
    });
    return convertView;
}
// ViewHolder内部类
static class ViewHolder {
    TextView title;
    ImageView imgFav;
}

内容的提问来源于stack exchange,提问作者ghassan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.11 21:06:03