大马资讯论坛 - 马来西亚中文资讯平台

 找回密码
 注册
搜索

[教学] Android GridView backpressed maintain scroll position, not back on top

[复制链接]
发表于 2015-1-4 09:51:59 | 显示全部楼层 |阅读模式
Maintain/Save/Restore scroll position when returning to a GridView Android
Maintain Scroll Position of Android  GridView through Screen Rotation
Scroll position jumps on Android GridView
记录和恢复滚动位置-Android   GridView

  1. static int index;
  2. private GridView gridView;

  3. @Override
  4. public void onResume(){

  5.     gridView.setSelection(index);
  6.     super.onResume();
  7. }

  8. @Override
  9. public void onPause(){  
  10.     index = gridView.getFirstVisiblePosition();
  11.     super.onPause();
  12. }

  13. @Override
  14. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  15.                 Bundle savedInstanceState) {
  16.     View view = inflater.inflate(R.layout.fragment_home, container, false);
  17.                
  18.    
  19.     gridView= (GridView) view.findViewById(R.id.gridview);

  20.        
  21.     gridView.setOnItemClickListener(new OnItemClickListener() {
  22.                 @Override
  23.                 public void onItemClick(AdapterView<?> parent, View view,
  24.                                 int position, long id) {
  25.                         index = gridView.getFirstVisiblePosition();
  26.                         Intent myIntent = new Intent(getActivity(), NewsList_Activity.class);
  27.                         myIntent.putExtra("oStateItem", oStateItems.get(position));
  28.                         startActivity(myIntent);
  29.                 }
  30.     })

  31.     return view;
  32. }

复制代码

手机版|大马资讯论坛  

GMT+8, 2022-1-17 06:05 , Processed in 0.028155 second(s), 13 queries , File On.

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表