从FragmentStatePagerAdapter调用时,ListFragment不会重新显示数据

时间:2014-11-19 18:29:09

标签: android android-fragments android-viewpager android-listfragment fragmentstatepageradapter

这是我的UI层次结构:

主要片段 - >使用ActionBar.TabListener的片段 - >片段与FragmentStatePagerAdapter - > ListFragment

问题是ListFragment会在我第一次进入ListFragment的选项卡时显示,但它不会第二次显示(因此转到选项卡2显示它,然后转到选项卡1然后转到选项卡2它没有显示自己.ListFragment位于标签2)。

如果我删除FragmentStatePagerAdapter,这个布局工作正常,但现在我的项目需要页面,所以我需要添加FragmentStatePagerAdapter来使分页工作。

然而,在这一点上,我真的不知道问题是什么。我不知道问题是在FragmentStatePagerAdapter,ListFragment中还是由于某种原因手机丢失了ListFragment的数据。 我知道切换标签时不会再次调用FragmentStatePagerAdapter中的getItem。

这是我的TrendsPagerHolder和FragmentStatePagerAdapter(作为内部类)的代码:

import java.util.ArrayList;
import java.util.Collections;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Fragment;
import android.os.Bundle;
import android.app.FragmentManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class TrendsPagerHolder extends Fragment
{
private ArrayList<String>testArray = new ArrayList<String>();
private Context context;
private SharedPreferences preferences;
private Bundle applicationStatus;

// The number of pages to display
private static final int NUM_PAGES = 3;

private ViewPager pager;

//The pager adapter, which provides the pages to the view pager widget
private PagerAdapter pagerAdapter;

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

    context = getActivity().getApplicationContext();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 
{       
    context = inflater.getContext();
    applicationStatus = savedInstanceState;
    View rootView = (View) inflater.inflate(R.layout.trends_screen_slider, container, false);

    System.out.println("in TrendsPagerHolder onCreateView");

    pager = (ViewPager) rootView.findViewById(R.id.trends_pager);   
    pagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());   
    pager.setAdapter(pagerAdapter);
    pager.setCurrentItem(0);

    return rootView;
}

public class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter
{       
    TrendsLV[] arrayOfTrendsFragments;

    public ScreenSlidePagerAdapter(FragmentManager fragmentManager)
    {

        super(fragmentManager);

        testArray.add("One");
        testArray.add("Two");
        testArray.add("Three");

        arrayOfTrendsFragments = new TrendsLV[NUM_PAGES];
        arrayOfTrendsFragments[0] = new TrendsLV(testArray, 0);
        arrayOfTrendsFragments[1] = new TrendsLV(testArray, 1);
        arrayOfTrendsFragments[2] = new TrendsLV(testArray, 2);
    }

    @Override
    public Fragment getItem(int position) 
    {                       
        System.out.println("in getItem. Position = " + position);
        return arrayOfTrendsFragments[position];
    }

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

这是我的ListFragment代码:

import java.util.ArrayList;
import java.util.Collections;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListFragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class TrendsLV extends ListFragment
{
private Context context;
private ArrayList<String>testArray;
private Bundle applicationStatus;
private int pageIndex;

public TrendsLV(ArrayList<String> test, int index)
{
    testArray = test;
    pageIndex = index;
}

public TrendsLV()
{
    // required
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{       
    context = inflater.getContext();
    applicationStatus = savedInstanceState;

    System.out.println("in TrendsLV onCreateView");

    if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
    {                               
            if (testArray != null)
            {
                TrendsListAdapter adapter = new TrendsListAdapter(context, testArray, pageIndex);
                setListAdapter(adapter);
            }

    }
    return super.onCreateView(inflater, container, savedInstanceState);
}
}//end class TrendsLV

这里是从ListFragment调用的适配器代码:

    import java.util.ArrayList;
    import android.content.Context;
    import android.graphics.Color;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;

    public class TrendsListAdapter extends ArrayAdapter<String>
    {
    private Context context;
    private ArrayList<String>testArray;
    private int pageIndex;

    public TrendsListAdapter (Context c, ArrayList<String> test, int index)
    {
        super(c, R.layout.trends_row, test);
        context = c;
        testArray = test;

        pageIndex = index;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.trends_row, parent, false);
        TextView label_totalArea = (TextView) rowView.findViewById(R.id.trends_labelTotalArea);
        label_totalArea.setText(context.getResources().getString(R.string.stringTotalArea));

        return rowView;
    }
}

如果您需要其他信息来解决此问题,请与我们联系。 我正在使用Android 4.4.4在设备上测试它。

谢谢。

2 个答案:

答案 0 :(得分:0)

如果您在Fragment内充气Fragment,则应使用getChildFragmentManager()代替getFragmentManager()

以下是文档:http://developer.android.com/reference/android/app/Fragment.html#getChildFragmentManager()

 public class TrendsPagerHolder extends Fragment
{
private ArrayList<String>testArray = new ArrayList<String>();
private Context context;
private SharedPreferences preferences;
private Bundle applicationStatus;

// The number of pages to display
private static final int NUM_PAGES = 3;

private ViewPager pager;

//The pager adapter, which provides the pages to the view pager widget
private PagerAdapter pagerAdapter;

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

    context = getActivity().getApplicationContext();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 
{       
    context = inflater.getContext();
    applicationStatus = savedInstanceState;
    View rootView = (View) inflater.inflate(R.layout.trends_screen_slider, container, false);

    System.out.println("in TrendsPagerHolder onCreateView");

    pager = (ViewPager) rootView.findViewById(R.id.trends_pager);   
    pagerAdapter = new ScreenSlidePagerAdapter(getChildFragmentManager());   
    pager.setAdapter(pagerAdapter);
    pager.setCurrentItem(0);

    return rootView;
}

答案 1 :(得分:0)

我认为您应该创建ListFragment的实例并在FragmentStatePagerAdapter中使用该实例..这样的事情..

TrendsLV 类中,删除构造函数并添加以下方法以创建片段实例..

public static TrendsLV newInstance(ArrayList<String> test, int index) {
    TrendsLV fragment = new TrendsLV();
     testArray = test;
     pageIndex = index;
    return fragment;
}

然后更新您的ScreenSlidePagerAdapter,如下所示..

    public class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter
{       
    TrendsLV[] arrayOfTrendsFragments;

    public ScreenSlidePagerAdapter(FragmentManager fragmentManager)
    {

        super(fragmentManager);

        testArray.add("One");
        testArray.add("Two");
        testArray.add("Three");

        arrayOfTrendsFragments = new TrendsLV[NUM_PAGES];
        arrayOfTrendsFragments[0] = TrendsLV.newInstance(testArray, 0);
        arrayOfTrendsFragments[1] = TrendsLV.newInstance(testArray, 1)
        arrayOfTrendsFragments[2] = TrendsLV.newInstance(testArray, 2)
    }

    @Override
    public Fragment getItem(int position) 
    {                       
        System.out.println("in getItem. Position = " + position);
        return arrayOfTrendsFragments[position];
    }

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

或者不是创建片段数组..你可以像这样替换in getItem()方法..

   public class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter
{       


    public ScreenSlidePagerAdapter(FragmentManager fragmentManager)
    {

        super(fragmentManager);

        testArray.add("One");
        testArray.add("Two");
        testArray.add("Three");

    }

    @Override
    public Fragment getItem(int position) 
    {                       
        System.out.println("in getItem. Position = " + position);

        Fragment instanceFragment = null;

        switch (position) {
        case 0:
            instanceFragment =TrendsLV.newInstance(testArray, 0);;

            break;
        case 1:
            instanceFragment =TrendsLV.newInstance(testArray, 1);;

            break;
        case 2:
            instanceFragment =TrendsLV.newInstance(testArray, 2);;

            break;
        default:
            break;
        }
        return instanceFragment;
    }

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

我不知道testArray的用途是什么,所以我没有对它进行任何更改。

希望它有所帮助.. !! :)