ViewPager+Fragment+GridView方案notifyDataSetChanged()无法刷新数据问题

采用ViewPager+Fragment+GridView方案实现类似qq的多屏滑动的效果,但是在运行问题中发现了第三屏的GridView修改数据是无法刷新,采用notifyDataSetChanged()也无法刷新,在网上找了很多例子多是讲到在FragmentPagerAdapter中除清缓存,经过验证只做这步是不行的,无奈之下只能从Fragment生命周期入手。结合网上的例子给出了解决方案:

首先是MyFragmentPagerAdapter.java

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
/**
 * 
 * @author Tom.Cai
 *
 */
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
    private ArrayList<Fragment> fragments;
    private FragmentManager fm;
    
    public MyFragmentPagerAdapter(FragmentManager fm) {
    	super(fm);
    }

    public MyFragmentPagerAdapter(FragmentManager fm, ArrayList<Fragment> fragments) {
        super(fm);
        this.fm = fm;
        this.fragments = fragments;
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

    @Override
    public Fragment getItem(int arg0) {
        return fragments.get(arg0);
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }
    
    public void setFragmentsList(ArrayList<Fragment> fragmentsList) {
    	if(this.fragments != null){
    		FragmentTransaction ft	 = fm.beginTransaction();
    		for(Fragment f:fragmentsList){
    			ft.remove(f);
    		}
    		ft.commit();
    		ft= null;
    		this.fragments = fragmentsList;
    		notifyDataSetChanged();
    	}
	}

}

然后是the GridView inside of Fragment

/**
 * @Function: 第三个屏幕
 * @Date: 2013-12-11
 * @Time: 下午3:45:32
 * @author Tom.Cai
 */
public class ThreeFragment extends Fragment {

	private GridView gridview;
	
	private ArrayList<HashMap<String, Object>> listImageItem;

	private SimpleAdapter simpleAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		  View view = inflater.inflate(R.layout.threefragment, container, false);
		  InitView(view);
		  InitListener();
		  return view;
	}

	private void InitView(View view) {
		gridview = (GridView) view.findViewById(R.id.gridview);
	}

	private void InitListener() {
		gridview.setOnItemClickListener(new MyItemClickListener());
		gridview.setOnItemLongClickListener(new MyItemLongClickListener());
	}



	@Override
	public void onStart() {
		// 生成动态数组,并且转入数据 #在onStart()方法中加载数据,就在Fragment中可以动态刷新数据#
		listImageItem = new ArrayList<HashMap<String, Object>>();
			for (int i = 0; i < 9; i++) {
				HashMap<String, Object> map = new HashMap<String, Object>();
			  map.put("ItemImage", R.drawable.user);// 添加图像资源的ID
				map.put("ItemText", i+"");// 按序号做ItemText
				listImageItem.add(map);
			}
		}
		simpleAdapter = new SimpleAdapter(
				getActivity().getApplicationContext(), listImageItem,
				R.layout.contact_list_item_gridview, new String[] {
						"ItemImage", "ItemText" }, new int[] { R.id.displayImg,
						R.id.displayName });
		//添加图片绑定
		simpleAdapter.setViewBinder(new ViewBinder() {
			public boolean setViewValue(View view, Object data,
					String textRepresentation) {
				if (view instanceof ImageView && data instanceof Drawable) {
					ImageView iv = (ImageView) view;
					iv.setImageDrawable((Drawable) data);
					return true;
				} else
					return false;
			}
		});
		gridview.setAdapter(simpleAdapter);
		super.onStart();
	}
}
除了在MyFragentPagerAdapter中清除缓存外,另外重要的是 在onStart()方法中加载数据,就在Fragment中可以动态刷新数据,本例中就是生产动态数组和setAdapt();


更多文章请查看我的博客:http://blog.csdn.net/caicongyang




你可能感兴趣的:(ViewPager+Fragment+GridView方案notifyDataSetChanged()无法刷新数据问题)