使用FragmentPagerAdapter的ViewPager中的片段为空白,第一次加载活动时

问题描述 投票:0回答:1

我正在使用FragmentStatePagerAdapter使用垂直页面适配器创建应用。我面临的最大问题是,首次启动应用程序时,数据不会显示在textview上,而是在滚动页面时显示。我相信片段视图会延迟创建textview,因为在Fragmentone.class内的LoadAlbumDataCompleted()函数上,我能够打印返回的数据或通过烤面包输出的数据,但不会填充到textview中。

请帮助。

MainActivity.class

public class MainActivity extends FragmentActivity implements LoadAalbumsTotalListener {

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

                mAdapter = new MainActivityVSlideAdapter(this, getSupportFragmentManager(), NUMBER_OF_PAGES);
                mPager.setAdapter(mAdapter);

             LoadTotalAlbumsNum.BindAlbumsTotalListener(this);

        }

@Override
    public void OnLoadAlbumsCompleted(String total) {

        if(total.trim().equalsIgnoreCase("")){
            NUMBER_OF_PAGES=0;
        }else{
            NUMBER_OF_PAGES=Integer.parseInt(total.trim());
        }

        mAdapter = new MainActivityVSlideAdapter(this, getSupportFragmentManager(), NUMBER_OF_PAGES);
        mPager.setAdapter(mAdapter);

    }
}

MainActivityVSlideAdapter.class适配器

public class MainActivityVSlideAdapter extends FragmentStatePagerAdapter {

    static int NUMBER_OF_PAGES;
    private Context con;

    public MainActivityVSlideAdapter(Context con, FragmentManager fm, int NUMBER_OF_PAGES) {
        super(fm);
        this.con=con;
        this.NUMBER_OF_PAGES=NUMBER_OF_PAGES;


    }

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

    @Override
    public Fragment getItem(int position) {

        return FragmentOne.newInstance(position);
    }
}

Fragmentone.class

public class FragmentOne extends Fragment implements LoadAlbumDataListener {

    private static final String MY_NUM_KEY = "num";

    private int mNum;
    private TextView SaloonName;
    private TextView location;


    // You can modify the parameters to pass in whatever you want
    public static FragmentOne newInstance(int num) {
        FragmentOne f = new FragmentOne();
        Bundle args = new Bundle();
        args.putInt(MY_NUM_KEY, num);
        f.setArguments(args);
        return f;
    }

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

        //get argument from
        mNum = getArguments() != null ? getArguments().getInt(MY_NUM_KEY) : 0;
        session=new Session(getActivity());

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_one, container, false);

        Methods methods=new Methods(getActivity());

       // v.setBackgroundColor(mColor);

         SaloonName = v.findViewById(R.id.SaloonName);
        location=v.findViewById(R.id.location);


        new LoadAlbumData(getActivity()).execute(getString(R.string.urlAddress)+"load-album-data.php", String.valueOf(mNum));

        LoadAlbumData.BindLoadAlbumDataListener(this);

        return v;
    }


    @Override
    public void LoadAlbumDataCompleted(String s) {


        JSONArray jsonPicsArray = null;
        JSONObject jsonObj;
        String  BusinessLocation=null;

        try {
            jsonPicsArray = new JSONArray(s);

                businessName = jsonObj.getString("businessName");
                BusinessLocation = jsonObj.getString("location");;
            }

          Toast.makeText(getActivity(), businessName, Toast.LENGTH_LONG).show();

           SaloonName.setText(businessName);
            location.setText(BusinessLocation);



        } catch (JSONException e) {
            e.printStackTrace();
        }

    }    
}
android textview fragment fragmentpageradapter fragmentstatepageradapter
1个回答
0
投票

从您的代码看来,您没有正确设置LoadAlbumDataListener。而不是静态设置侦听器。在您的LoadAlbumData Asynctask的构造函数中传递引用]

© www.soinside.com 2019 - 2024. All rights reserved.