Android 如何确保在ViewPager中的片段更改时调用onCreateView?

Android 如何确保在ViewPager中的片段更改时调用onCreateView?,android,android-viewpager,Android,Android Viewpager,我将ViewPager与自定义PagerAdapter一起使用(StaticPagerAdapter扩展了FragmentStatePagerAdapter)。 问题是:当我用Fragment3更改Fragment1时,onCreateView不会被调用,findViewById(R.id.placeholder)返回null protected void onStart() { super.onStart(); mAdapter.startUpdate(mPager);

我将ViewPager与自定义PagerAdapter一起使用(StaticPagerAdapter扩展了FragmentStatePagerAdapter)。 问题是:当我用Fragment3更改Fragment1时,onCreateView不会被调用,findViewById(R.id.placeholder)返回null

protected void onStart() {
    super.onStart();

    mAdapter.startUpdate(mPager);
    mAdapter.setFragment(0, new DummyFragment3());
    mAdapter.notifyDataSetChanged();
    mAdapter.finishUpdate(mPager);

    TextView tv = (TextView) this.findViewById(R.id.placeholder);
    tv.setText("PLACEHOLDER");
}
一个解决办法是搬家

TextView tv = (TextView) this.findViewById(R.id.placeholder);
tv.setText("PLACEHOLDER");
在片段中的onCreateView上,但不幸的是我不能这样做

问题:如何确保在ViewPager中的片段更改后立即调用onCreateView

有什么建议吗?提前谢谢! 下面是所有代码

主要活动

public class MainActivity extends AppCompatActivity {

StaticPagerAdapter mAdapter;
 ViewPager mPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    mAdapter = new StaticPagerAdapter(getFragmentManager(),2);

    mAdapter.setTitle(0,"EXPENSES");
    mAdapter.setTitle(1,"CONTACTS");
    mAdapter.setFragment(0, new DummyFragment1());
    mAdapter.setFragment(1, new DummyFragment2());

    SlidingTabLayout mTabs = (SlidingTabLayout) findViewById(R.id.tabs);
    mTabs.setDistributeEvenly(true);

    mPager = (ViewPager) findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);
    mTabs.setViewPager(mPager);
}

@Override
protected void onStart() {
    super.onStart();

    mAdapter.startUpdate(mPager);
    mAdapter.setFragment(0, new DummyFragment3());
    mAdapter.notifyDataSetChanged();
    mAdapter.finishUpdate(mPager);

    TextView tv = (TextView) this.findViewById(R.id.placeholder);
    tv.setText("PLACEHOLDER");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}
静电雷达

import android.app.Fragment;
import android.app.FragmentManager;
import android.support.v13.app.FragmentStatePagerAdapter;


public class StaticPagerAdapter extends FragmentStatePagerAdapter {

    String[] titels;
    Fragment[] fragments;
    public StaticPagerAdapter(FragmentManager fm, int size) {
        super(fm);
        titels = new String[size];
        fragments = new Fragment[size];
    }

    @Override
    public Fragment getItem(int position) {
        if (fragments!=null & position< fragments.length)
            return fragments[position];
        return new Fragment();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        if (titels!=null && position<titels.length)
            return titels[position];
        return "";
    }

    public void setTitle(int position, String title) {
        if (titels!=null && position<titels.length)
            titels[position]=title;
    }

    public void setFragment(int position, Fragment fragment) {
        if (fragments!=null && position<fragments.length)
            fragments[position]=fragment;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return titels.length;
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }
}
布局碎片1

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"/>


</LinearLayout>
布局碎片3

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"/>

    <TextView
        android:id="@+id/placeholder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text"
        android:textSize="20dp"
        />


</LinearLayout>
碎片3

public class DummyFragment3 extends Fragment{

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    Log.d("msg", "3");
    View view = inflater.inflate(R.layout.fragment_dummy, container, false);
    ((TextView)view.findViewById(R.id.text)).setText("Fragment number 3");

    return view;


}
}
但命令是

D/msg: 1
D/msg: 2
D/msg: 4
D/msg: 3    
我希望当我在PagerAdapter中更改片段时,会调用该片段的onCreateView,但这实际上会在以后发生。
感谢所有输入

您可以通过在日志中放置打印指令来验证流程的执行顺序

范例

Log.d(“味精”、“可丢弃”)


输入不同的消息并检查订单。

我刚刚找到了一个解决方案,我将在这里发布,以防有人有同样的问题。诀窍是在使用此序列更改片段时使用InstanceItem

startUpdate将在FragmentManager中启动事务

InstanceItem将在FragmentManager的事务中附加或添加片段

finishUpdate将调用FragmentManager的executePendingTransactions,它最终将调用片段的OnCreateView

@Override
protected void onStart() {
    super.onStart();

    Log.d("msg", "2");

    Fragment tmp = new DummyFragment3();
    mAdapter.setFragment(0, tmp);
    mAdapter.startUpdate(mPager);
    mAdapter.instantiateItem(mPager, 0);
    mAdapter.finishUpdate(mPager);

    Log.d("msg", "4");
    TextView tv = (TextView) mPager.findViewById(R.id.placeholder);
    tv.setText("PLACEHOLDER");
}
最重要的是在FragmentPagerAdapter的实现中覆盖getItemId方法,为每个要显示的片段返回一个唯一的键

@Override
public long getItemId(int position) {
    return getItem(position).hashCode();
}
原始的getItemId只返回位置,如果不必更改ViewPager中的页面,这是很好的


希望能有所帮助

你好,Josedlujan,感谢您的快速回复。我在问题中添加了Log.d消息:我希望当我在PagerAdapter中更改片段时,会调用该片段的onCreateView,但这实际上是在这之后发生的。谢谢你的评论,这会有帮助的
D/msg: 1
D/msg: 2
D/msg: 4
D/msg: 3    
@Override
protected void onStart() {
    super.onStart();

    Log.d("msg", "2");

    Fragment tmp = new DummyFragment3();
    mAdapter.setFragment(0, tmp);
    mAdapter.startUpdate(mPager);
    mAdapter.instantiateItem(mPager, 0);
    mAdapter.finishUpdate(mPager);

    Log.d("msg", "4");
    TextView tv = (TextView) mPager.findViewById(R.id.placeholder);
    tv.setText("PLACEHOLDER");
}
@Override
public long getItemId(int position) {
    return getItem(position).hashCode();
}