我使用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?
有什么建议吗?提前致谢! 低于所有代码
MainActivity
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);
}
}
StaticPagerAdapter
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;
}
}
DummyFragment1
public class DummyFragment1 extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_dummy, container, false);
//return super.onCreateView(inflater, container, savedInstanceState);
((TextView)view.findViewById(R.id.text)).setText("Fragment number 1");
return view;
}
}
LayoutFragment1
<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>
DummyFragment3
public class DummyFragment3 extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_dummy, container, false);
//return super.onCreateView(inflater, container, savedInstanceState);
((TextView)view.findViewById(R.id.text)).setText("Fragment number 3");
return view;
}
LayoutFragment3
<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>
我还添加了一些Logs.d消息来检查订单
在活动中
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new StaticPagerAdapter(getFragmentManager(),2);
Log.d("msg","1");
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();
Log.d("msg", "2");
mAdapter.startUpdate(mPager);
mAdapter.setFragment(0, new DummyFragment3());
mAdapter.notifyDataSetChanged();
mAdapter.finishUpdate(mPager);
Log.d("msg", "4");
//TextView tv = (TextView) this.findViewById(R.id.placeholder);
//tv.setText("PLACEHOLDER");
}
Fragment3
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中更改Fragment时,会调用该Fragment的onCreateView,但这实际上会在以后发生。 感谢所有输入
答案 0 :(得分:0)
您可以通过在日志
中放置打印指令来验证进程的执行顺序实施例
Log.d(&#34; msg&#34;,&#34; Throwable&#34;);
输入不同的MSG并检查订单。
答案 1 :(得分:0)
我刚刚找到了一个解决方案,我会在这里发布,以防有人遇到同样的问题。诀窍是在使用此序列更改Fragment时使用instantiateItem。
startUpdate将在FragmentManager中启动一个事务
instantiateItem将在FragmentManager的事务中附加或添加Fragment
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中的页面,这很好。
希望有所帮助