【问题标题】:How to save Scrolling state of a complex listview in a fragment如何在片段中保存复杂列表视图的滚动状态
【发布时间】:2015-08-27 18:53:22
【问题描述】:

我在片段中使用文件资源管理器列表视图。

但不知道如何保存其滚动状态。

我在过去 2 天搜索了 Stackoverflow quora 和 google,但没有得到任何结果。

我的项目支持 Api 14 +。

在我的片段资源管理器中,

我正在像这样制作 onCreateview 和 onclick

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.artist,container, false);

 //My coding stuff here

        lv=(ListView) root.findViewById(android.R.id.list);
        lv.setAdapter(adaptor);

        return root;
}

onclick如下:-

public void onActivityCreated(Bundle savedInstanceState) {
                super.onActivityCreated(savedInstanceState);
              lv=getListView();
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                            public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
             gtuMcaBean1= gtuMcaBean.get(position);
                                 lv.setAdapter(adaptor);
}

在我的 Fragmentactivity 类中,我调用 onBackpressed

    @Override
     public void onBackPressed() {
        // super.onBackPressed();
         if(pager.getCurrentItem()==0)//0 is item no for explorer view
         {
//My coding stuff here
        lv.setadaptor(adaptor);
         }

在我的 Mainactivity 中,我也有一个 onbackressed,当我从一个活动中查看片段视图时,它可以工作

@Override
   public void onBackPressed() {
        // super.onBackPressed();
         if(pager.getCurrentItem()==0)//0 is item no for explorer view
         {
//My coding stuff here
        lv.setadaptor(adaptor);
         }

现在我想保存 Listview 的滚动状态,所以如果用户按下返回按钮,它应该显示上一个滚动视图,而不是从 ListView 的开始。

谢谢提前:)

【问题讨论】:

    标签: java android listview android-fragments android-listview


    【解决方案1】:

    转自here

    伊恩回答

     // save index and top position
    int index = mList.getFirstVisiblePosition();
    View v = mList.getChildAt(0);
    int top = (v == null) ? 0 : (v.getTop() - mList.getPaddingTop());
    
    // ...
    
    // restore index and position
    mList.setSelectionFromTop(index, top);
    

    【讨论】:

      猜你喜欢
      • 2013-07-22
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多