动态修改viewpager里面的fragment(添加和删除)

来源:互联网 发布:pdf软件官方下载 9.0 编辑:程序博客网 时间:2024/06/07 08:37

在使用的时候,viewpager的适配器刚开始是继承FragmentPagerAdapter,getView的写法:

@Overridepublic Fragment getItem(int arg0) {// Fragment ft = null;//// ft = new DateShow(mContext);// for (int i = 0; i < lists.get(arg0).openplayitem.size(); i++) {// Log.i(TAG,// "arg0=" + arg0 + "name="// + lists.get(arg0).openplayitem.get(i).moviename);// }//// Bundle args = new Bundle();// args.putSerializable("date_title", lists.get(arg0).openplayitem);// ft.setArguments(args);// return ft;return mList.get(arg0);}

被注释掉的那写,然后在viewpager中显示出来后不能改变,即使更新适配器的数据也是无济于事。

最后看到这篇帖子,问题是和我一样的:http://www.desteps.com/mobile/android/0101093_2.html

总结起来就是:

注:引用stackoverflow牛人的解决说明:

The ViewPager doesn't remove your fragments with the code above because it loads several views (or fragments in your case) into memory. In addition to the visible view, it also loads the view to either side of the visible one. This provides the smooth scrolling from view to view that makes the ViewPager so cool.

To achieve the effect you want, you need to do a couple of things.

Change the FragmentPagerAdapter to a FragmentStatePagerAdapter. The reason for this is that the FragmentPagerAdapter will keep all the views that it loads into memory forever. Where the FragmentStatePagerAdapter disposes of views that fall outside the current and traversable views.

Override the adapter method getItemPosition (shown below). When we call mAdapter.notifyDataSetChanged(); the ViewPager interrogates the adapter to determine what has changed in terms of positioning. We use this method to say that everything has changed so reprocess all your view positioning。

大致意思是说:viewpager在加载当前页的时候已经将pager页左右页的内容加载进内存里了,这样才保证了viewpager左右滑动的时候的流畅性;

为了解决彻底删除fragment,我们要做的是:

1.将FragmentPagerAdapter 替换成FragmentStatePagerAdapter,因为前者只要加载过,fragment中的视图就一直在内存中,在这个过程中无论你怎么刷新,清除都是无用的,直至程序退出; 后者 可以满足我们的需求。

2.我们可以重写Adapter的方法--getItemPosition(),让其返回PagerAdapter.POSITION_NONE即可。

以下为引用内容:@Overridepublic int getItemPosition(Object object) {// TODO Auto-generated method stubreturn PagerAdapter.POSITION_NONE;}
我最后的代码:

package com.yang.Adapter;import java.util.ArrayList;import android.content.Context;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentStatePagerAdapter;import android.support.v4.view.PagerAdapter;public class MyFragmentPagerAdapter extends FragmentStatePagerAdapter {private static final String TAG = "MyFragmentPagerAdapter";// private ArrayList<OpilistItem> lists;private ArrayList<Fragment> mList;private Context mContext;public MyFragmentPagerAdapter(FragmentManager fm, Context context) {super(fm);this.mContext = context;mList = new ArrayList<Fragment>();}public void setLists(ArrayList<Fragment> lists) {this.mList = lists;}public void UpdateList(ArrayList<Fragment> arrayList) {this.mList.clear();this.mList.addAll(arrayList);notifyDataSetChanged();}@Overridepublic Fragment getItem(int arg0) {// Fragment ft = null;//// ft = new DateShow(mContext);// for (int i = 0; i < lists.get(arg0).openplayitem.size(); i++) {// Log.i(TAG,// "arg0=" + arg0 + "name="// + lists.get(arg0).openplayitem.get(i).moviename);// }//// Bundle args = new Bundle();// args.putSerializable("date_title", lists.get(arg0).openplayitem);// ft.setArguments(args);// return ft;return mList.get(arg0);}@Overridepublic int getCount() {return mList.size();}@Overridepublic int getItemPosition(Object object) {// TODO Auto-generated method stubreturn PagerAdapter.POSITION_NONE;}}

之前还有一个问题,就是在fragment中报空指针异常,是因为我要加载的动态加载的fragment只有一个,但是viewpager默认会去加载下一个,使得浏览流畅,从我注释的代码中可以看到,数据的传递是通过bundle,造成预加载的时候数据成空,造成空指针异常,解决的办法是在fragment的onCreateView方法中判断数据是否为空,然后再判断是不是生成并返回view。

我的适配器,不一定对大家有用,但至少有参考。

package com.yang.fragment;import java.util.ArrayList;import android.content.Context;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.view.ViewGroup.MarginLayoutParams;import android.widget.ListAdapter;import android.widget.ListView;import com.yang.Adapter.ViewPagerItemAdapter;import com.yang.gallery.R;import com.yang.model.OpenPlay;import com.yang.model.OpilistItem;public class DateShow extends Fragment {private static final String TAG = "DateShow";private Context mContext;private ListView mListView;private OpilistItem item;private ViewPagerItemAdapter mAdapter;private ArrayList<OpenPlay> mListItems;public DateShow(Context context,ArrayList<OpenPlay> list) {super();this.mContext = context;mListItems=list;}@SuppressWarnings("unchecked")@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {if (container == null) {return null;} else {//Bundle bundle = getArguments();//mListItems = (ArrayList<OpenPlay>) bundle.getSerializable("date_title");View rootView = inflater.inflate(R.layout.fragment_date_item,container, false);mListView = (ListView) rootView.findViewById(R.id.date_list);/** * 防止预加载下一页出现空指针异常 */if (mListItems != null) {Log.i(TAG, mListItems.size()+"");for (int i = 0; i < mListItems.size(); i++) {Log.i(TAG, mListItems.get(i).moviename);}mAdapter = new ViewPagerItemAdapter(mContext,mListItems);mListView.setAdapter(mAdapter);mAdapter.UpdateOpenPlays(mListItems);setListViewHeightBasedOnChildren(mListView);}return rootView;}}public void setListViewHeightBasedOnChildren(ListView listView) {ListAdapter listAdapter = listView.getAdapter();if (listAdapter == null) {return;}int totalHeight = 0;for (int i = 0; i < listAdapter.getCount(); i++) {View listItem = listAdapter.getView(i, null, listView);listItem.measure(0, 0);totalHeight += listItem.getMeasuredHeight();}ViewGroup.LayoutParams params = listView.getLayoutParams();params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));((MarginLayoutParams) params).setMargins(10, 10, 10, 10); // 可删除listView.setLayoutParams(params);}@Overridepublic void onActivityCreated(@Nullable Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);}}



1 1
原创粉丝点击