在viewpager更换片段 [英] Replacing fragments within viewpager
问题描述
我目前有一个问题与另一个替换内ViewPager一定的片段。片段ID喜欢取而代之的是我的部门,它具有的ImageButton ID喜欢使用开始更换。我试图从其他类似的问题(其中大部分是老人和之前新的API版本,它允许对嵌套的片段),并没有成功申请了一些建议。请问使用嵌套的片段更容易?我是新的Android应用程序开发,因此任何帮助将是巨大的。先谢谢了。
这是我的FragmentAcitivty
公共类ViewPagerStyle扩展FragmentActivity {
私人ViewPager mViewPager;
私人ViewPagerAdapter适配器;
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
setupView函数();
setTab();
}
私人无效setupView函数(){
mViewPager =(ViewPager)findViewById(R.id.viewPager);
适配器=新ViewPagerAdapter(getApplicationContext(),getSupportFragmentManager());
mViewPager.setAdapter(适配器);
mViewPager.setCurrentItem(0);
}
私人无效setTab(){
mViewPager.setOnPageChangeListener(新OnPageChangeListener(){
@覆盖
公共无效onPageScrollStateChanged(INT位置){}
@覆盖
公共无效onPageScrolled(INT为arg0,浮动ARG1,INT ARG2){}
@覆盖
公共无效onPageSelected(INT位置){
// TODO自动生成方法存根
开关(位置){
情况下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);
打破;
情况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);
打破;
案例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);
打破;
}
}
});
}
}
FragmentPagerAdapter
公共类ViewPagerAdapter扩展FragmentPagerAdapter {
私人语境_context;
公共ViewPagerAdapter(上下文的背景下,FragmentManager FM){
超(FM);
_context =背景;
}
@覆盖
公共片段的getItem(INT位置){
片f =新的片段();
开关(位置){
情况下0:
F = PubView.newInstance(_context);
打破;
情况1:
F = MyView.newInstance(_context);
打破;
案例2:
F = Departments.newInstance(_context);
打破;
}
返回F;
}
@覆盖
公众诠释getCount将(){
返回3;
}
}
部门片段,按钮
公共类部门扩展片段{
公共静态片段的newInstance(上下文的背景下){
部门F =新的部门();
返回F;
}
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
查看根=(查看)inflater.inflate(R.layout.activity_departments,NULL);
的ImageButton engineeringButton =(的ImageButton)root.findViewById(R.id.engineeringButton);
engineeringButton.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
片段newFragment = Engineering.newInstance(空);
FragmentTransaction交易= getChildFragmentManager()的BeginTransaction()。
transaction.addToBackStack(空);
transaction.replace(R.id.viewPager,newFragment).commit();
}
});
返回根;
}
}
此外,这里是一个承载viewpager我的main.xml文件
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:背景=@色/紫色
机器人:方向=垂直>
< TableLayout
风格=@风格/ layout_f_w
机器人:stretchColumns =*>
<的TableRow
机器人:ID =@ + ID / tableRow1
风格=@风格/ layout_wrap
机器人:背景=@色/白>
<! - 第一个标签 - >
<的LinearLayout
机器人:ID =@ + ID / first_text
风格=@风格/ layout_f_w
机器人:方向=垂直>
<的TextView
机器人:ID =@ + ID / textView1
风格=@风格/ text_title
机器人:文本=pubView
机器人:文字颜色=@色/紫色/>
< / LinearLayout中>
<! - 第二个选项卡 - >
<的LinearLayout
机器人:ID =@ + ID / second_text
风格=@风格/ layout_f_w
机器人:方向=垂直>
<的TextView
机器人:ID =@ + ID / textView1
风格=@风格/ text_title
机器人:重力=中心
机器人:文本=MyView的
机器人:文字颜色=@色/紫色/>
< / LinearLayout中>
<! - 第三个选项卡 - >
<的LinearLayout
机器人:ID =@ + ID / third_text
风格=@风格/ layout_f_w
机器人:方向=垂直>
<的TextView
机器人:ID =@ + ID / textView1
风格=@风格/ text_title
机器人:文本=部。
机器人:文字颜色=@色/紫色/>
< / LinearLayout中>
< /的TableRow>
< / TableLayout>
<! - 包含选项卡指示器 - >
<包括
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
布局=@布局/指示器/>
< android.support.v4.view.ViewPager
机器人:ID =@ + ID / viewPager
机器人:layout_width =FILL_PARENT
机器人:layout_height =450dp/>
<的ImageButton
机器人:ID =@ + ID / settingsButton
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =30dp
机器人:layout_marginLeft =50dp
机器人:背景=@可绘制/ settings_button
机器人:SRC =@可绘制/ settings_button/>
< / LinearLayout中>
有一件事情我感到困惑的是,我不知道投入transaction.replace的(r.id.viewPager,newfragment)的第一个参数,它的ID ...香港专业教育学院读它需要的容器的id但是当我用这个,我从logcat中收到运行时错误:
04-13 21:41:48.680:E / AndroidRuntime(960):java.lang.IllegalArgumentException:如果没有查看发现ID 0x7f0a0029(com.pvcalendar:ID / viewPager)为片段工程{4089a730#0 ID = 0x7f0a0029}
我假设你想要的工程
片段是一个完全新的一页,因为你AREN 'T使用它在你的 ViewPagerAdapter
。如果是这样的话,创建一个新的活动
,与你的工程
片段在布局,并启动活动
从 engineeringButton
点击。
现在的问题是,你正在试图推你的工程片段进入查看 R.layout.activity_departments
的层次结构,并且有(希望)没有 ViewPager
在那里,因此错误。
I am currently having an issue with replacing a certain fragment within ViewPager with another. The fragment id like to replace is my "Departments" which has an Imagebutton id like to use to begin the replacement. I've tried to apply some suggestions from other similar questions (most of which were old and prior to the new api release which allows for nested fragments) and have had no success. Would using nested fragments be easier? I am new to android app development so any help would be great. Thanks in advance.
here is my 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;
}
}
Departments Fragment with button
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;
}
}
Also, here is my main.xml file which hosts the 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>
One thing i am confused on is that I don't know which id to put into the first argument of transaction.replace(r.id.viewPager, newfragment)...Ive read that it needs the id of the container but when I use this I receive the runtime error from 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}
I'm assuming you want the Engineering
fragment to be on a completely new page, because you aren't using it in your ViewPagerAdapter
. If that's the case, create a new Activity
, with your Engineering
fragment in the layout, and launch the Activity
from the engineeringButton
click.
The problem is you are trying to shove your Engineering fragment into the View hierarchy of R.layout.activity_departments
, and there is (hopefully) no ViewPager
in there, hence the error.
这篇关于在viewpager更换片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!