1

我有一个使用 Viewpager 的 Activity。Viewpager 有一些片段,当然片段是由 FragmentPagerAdapter 准备的。让我举个例子:

这是我的活动:

public class SampleActivity extends FragmentActivity {

private ViewPager viewpager;
private SampleFragmentAdapter sample_fragment_adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.sample_layout);
    viewpager = (ViewPager)findViewById(R.id.sample_pager);
    sample_fragment_adapter = new SampleFragmentAdapter(getSupportFragmentManager(),this);
    viewpager.setAdapter(sample_fragment_adapter);
}

片段页面适配器:

public class SampleFragmentAdapter extends FragmentPagerAdapter{

 private Context context;
 private int fCount;
 private SampleFragment[] fragment_list;

    public SampleFragmentAdapter(FragmentManager fm,Context context) {
        super(fm);
        this.context=context;
        fragment_list=new SampleFragment[8];
        fCount=fragment_list.length;
        for(int i=0;i<8;i++){
            fragment_list[i]=new SampleFragment();
            fragment_list[i].setList(getList());
        }
    }
}

@Override
    public Fragment getItem(int position) {
        return fragment_list[position];
    }

    @Override
    public int getCount() {
        return fCount;
    }

public void getList(){

        //here , an arraylist which is not null is returned
        //this arraylist includes data using by listview in fragment
}

最后是我的 Fragment 类(Fragment 包括一个列表视图):

public class SampleFragment extends Fragment implements AdapterView.OnItemClickListener{

private View view;
private ListView sample_list_view;
private LessonListAdapter sample_list_adapter;
private ArrayList<Sample> sample_list;

public void setList(ArrayList<Sample> sample_list){
    this.sample_list=sample_list;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     view=inflater.inflate(R.layout.sample_list_layout, container, false);
     sample_list_view=(ListView)view.findViewById(R.id.sample_list_view);
     sample_list_adapter=new SampleListAdapter(getActivity(),sample_list);
     sample_list.setAdapter(sample_list_adapter);
     return view;
}

我的问题:当活动在任何方向上开始时,它都能正常工作。但是当我改变方向时,我猜活动再次开始,并且在 setList() 方法之前调用 SampleFragment 中的 onCreateView 方法。所以 null 数组列表转到 sample_list_adapter 但无法准备片段视图直到arraylist准备好。我想这是Fragment的生命周期问题。如何解决?

谢谢你的帮助

4

1 回答 1

1

最简单的解决方案是使用 final List

public class SampleFragment extends Fragment implements AdapterView.OnItemClickListener{

private View view;
private ListView sample_list_view;
private LessonListAdapter sample_list_adapter;
private final ArrayList<Sample> sample_list = new ArrayList<Sample>();

public void setList(ArrayList<Sample> sample_list){
    this.sample_list.clear();
    this.sample_list.addAll(sample_list);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     view=inflater.inflate(R.layout.sample_list_layout, container, false);
     sample_list_view=(ListView)view.findViewById(R.id.sample_list_view);
     sample_list_adapter=new LessonListAdapter(getActivity(),sample_list);
     sample_list.setAdapter(sample_list_adapter);
     return view;
}

这将更改适配器内部的列表,而不是在将空列表设置为适配器后替换列表。

确保跨实例保存 ArrayList:

public class SampleFragment extends Fragment implements AdapterView.OnItemClickListener{
    private final ArrayList<Sample> sample_list = new ArrayList<Sample>();

    public void onCreate(Bundle state) {
        super.onCreate(state);
        if (state != null) {
            setList((private final ArrayList<Sample> sample_list = new ArrayList<Sample>();)state.getSerializable("list");
        }
    }

    public void onSaveInstanceState(Bundle state) {
        super.onSaveInstanceState();
        state.putSerializable("list", sample_list);
    }
}
于 2013-09-07T09:11:22.490 回答