我目前在将 ViewPager 中的某个片段替换为另一个片段时遇到问题。喜欢替换的片段 id 是我的“部门”,它有一个 Imagebutton id,喜欢用来开始替换。我尝试应用其他类似问题的一些建议(其中大部分是旧的并且在允许嵌套片段的新 api 版本之前)并且没有成功。使用嵌套片段会更容易吗?我是 android 应用程序开发的新手,所以任何帮助都会很棒。提前致谢。
这是我的 FragmentAcitivty
public class ViewPagerStyle extends FragmentActivity {
private ViewPager mViewPager;
private ViewPagerAdapter adapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setUpView();
setTab();
}
private void setUpView(){
mViewPager = (ViewPager) findViewById(R.id.viewPager);
adapter = new ViewPagerAdapter(getApplicationContext(),getSupportFragmentManager());
mViewPager.setAdapter(adapter);
mViewPager.setCurrentItem(0);
}
private void setTab(){
mViewPager.setOnPageChangeListener(new OnPageChangeListener(){
@Override
public void onPageScrollStateChanged(int position) {}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {}
@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
switch(position){
case 0:
findViewById(R.id.first_tab).setVisibility(View.VISIBLE);
findViewById(R.id.second_tab).setVisibility(View.INVISIBLE);
findViewById(R.id.third_tab).setVisibility(View.INVISIBLE);
break;
case 1:
findViewById(R.id.first_tab).setVisibility(View.INVISIBLE);
findViewById(R.id.second_tab).setVisibility(View.VISIBLE);
findViewById(R.id.third_tab).setVisibility(View.INVISIBLE);
break;
case 2:
findViewById(R.id.first_tab).setVisibility(View.INVISIBLE);
findViewById(R.id.second_tab).setVisibility(View.INVISIBLE);
findViewById(R.id.third_tab).setVisibility(View.VISIBLE);
break;
}
}
});
}
}
FragmentPagerAdapter
public class ViewPagerAdapter extends FragmentPagerAdapter {
private Context _context;
public ViewPagerAdapter(Context context, FragmentManager fm) {
super(fm);
_context = context;
}
@Override
public Fragment getItem(int position) {
Fragment f = new Fragment();
switch(position){
case 0:
f=PubView.newInstance(_context);
break;
case 1:
f=MyView.newInstance(_context);
break;
case 2:
f=Departments.newInstance(_context);
break;
}
return f;
}
@Override
public int getCount() {
return 3;
}
}
带按钮的部门片段
public class Departments extends Fragment {
public static Fragment newInstance(Context context) {
Departments f = new Departments();
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View root = (View) inflater.inflate(R.layout.activity_departments, null);
ImageButton engineeringButton = (ImageButton)root.findViewById(R.id.engineeringButton);
engineeringButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Fragment newFragment = Engineering.newInstance(null);
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.addToBackStack(null);
transaction.replace(R.id.viewPager, newFragment).commit();
}
});
return root;
}
}
另外,这是我的 main.xml 文件,它托管了 viewpager
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/purple"
android:orientation="vertical" >
<TableLayout
style="@style/layout_f_w"
android:stretchColumns="*" >
<TableRow
android:id="@+id/tableRow1"
style="@style/layout_wrap"
android:background="@color/white" >
<!-- First Tab -->
<LinearLayout
android:id="@+id/first_text"
style="@style/layout_f_w"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
style="@style/text_title"
android:text="pubView"
android:textColor="@color/purple" />
</LinearLayout>
<!-- Second Tab -->
<LinearLayout
android:id="@+id/second_text"
style="@style/layout_f_w"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
style="@style/text_title"
android:gravity="center"
android:text="myView"
android:textColor="@color/purple" />
</LinearLayout>
<!-- Third Tab -->
<LinearLayout
android:id="@+id/third_text"
style="@style/layout_f_w"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
style="@style/text_title"
android:text="Dept."
android:textColor="@color/purple" />
</LinearLayout>
</TableRow>
</TableLayout>
<!-- Include Tab Indicator -->
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="@layout/indicator" />
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="fill_parent"
android:layout_height="450dp" />
<ImageButton
android:id="@+id/settingsButton"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginLeft="50dp"
android:background="@drawable/settings_button"
android:src="@drawable/settings_button" />
</LinearLayout>
我感到困惑的一件事是我不知道将哪个 id 放入 transaction.replace(r.id.viewPager, newfragment) 的第一个参数中...我读过它需要容器的 id 但是当我使用这个我从 logcat 收到运行时错误:
04-13 21:41:48.680: E/AndroidRuntime(960): java.lang.IllegalArgumentException: No view found for id 0x7f0a0029 (com.pvcalendar:id/viewPager) for fragment Engineering{4089a730 #0 id=0x7f0a0029}