使用意图从另一个片段调用一个片段
Posted
技术标签:
【中文标题】使用意图从另一个片段调用一个片段【英文标题】:Using intent to call a fragment from another fragment 【发布时间】:2014-11-14 17:43:54 【问题描述】:我正在使用 viewpager 控件来托管三个片段。我在每个片段中放置了一个按钮以转到下一个片段。所以我想知道如何使用意图从一个片段转到另一个片段?
主要目的是创建一个注册表单,它分为三个片段。代码如下:
主要活动:
package pl.looksok.viewpagerdemo;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class MainActivity extends FragmentActivity
ViewPager pager;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyPagerAdapter pageAdapter = new MyPagerAdapter(getSupportFragmentManager());
pager = (ViewPager)findViewById(R.id.myViewPager);
pager.setAdapter(pageAdapter);
第一个片段:
package pl.looksok.viewpagerdemo;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.support.v4.app.Fragment;
public class FragmentBlue extends Fragment
Button btnnext1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
View view = inflater.inflate(R.layout.fragment_blue, container, false);
btnnext1=(Button) view.findViewById(R.id.btnnext1);
btnnext1.setOnClickListener(new OnClickListener()
@Override
public void onClick(View view)
Intent intent = new Intent(view.getContext(), FragmentGreen.class);
view.getContext().startActivity(intent);
getActivity().finish();
);
return view;
第二个片段:
package pl.looksok.viewpagerdemo;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.support.v4.app.Fragment;
public class FragmentGreen extends Fragment
Button btnnext2;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
View view = inflater.inflate(R.layout.fragment_green, container, false);
btnnext2=(Button) view.findViewById(R.id.btnnext2);
btnnext2.setOnClickListener(new OnClickListener()
@Override
public void onClick(View view)
Intent intent = new Intent(view.getContext(), FragmentPink.class);
view.getContext().startActivity(intent);
getActivity().finish();
);
return view;
第三个片段:
package pl.looksok.viewpagerdemo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.support.v4.app.Fragment;
public class FragmentPink extends Fragment
Button btnnext3;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
View view = inflater.inflate(R.layout.fragment_pink, container, false);
btnnext3=(Button) view.findViewById(R.id.btnnext3);
btnnext3.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
);
return view;
viewpager 的适配器:
package pl.looksok.viewpagerdemo;
import java.util.ArrayList;
import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MyPagerAdapter extends FragmentPagerAdapter
private List<Fragment> fragments;
public MyPagerAdapter(FragmentManager fm)
super(fm);
this.fragments = new ArrayList<Fragment>();
fragments.add(new FragmentBlue());
fragments.add(new FragmentGreen());
fragments.add(new FragmentPink());
@Override
public Fragment getItem(int position)
return fragments.get(position);
@Override
public int getCount()
return fragments.size();
viewpager 的自定义类:
package pl.looksok.viewpagerdemo;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
public class CustomViewPager extends ViewPager
private boolean isPagingEnabled = false;
public CustomViewPager(Context context)
super(context);
public CustomViewPager(Context context, AttributeSet attrs)
super(context, attrs);
@Override
public boolean onTouchEvent(MotionEvent event)
return this.isPagingEnabled && super.onTouchEvent(event);
@Override
public boolean onInterceptTouchEvent(MotionEvent event)
return this.isPagingEnabled && super.onInterceptTouchEvent(event);
public void setPagingEnabled(boolean b)
this.isPagingEnabled = b;
我现在使用的代码在单击第一个按钮时显示以下错误:
09-20 10:54:18.632: E/AndroidRuntime(9401): FATAL EXCEPTION: main
09-20 10:54:18.632: E/AndroidRuntime(9401): android.content.ActivityNotFoundException: Unable to find explicit activity class pl.looksok.viewpagerdemo/pl.looksok.viewpagerdemo.FragmentGreen; have you declared this activity in your AndroidManifest.xml?
09-20 10:54:18.632: E/AndroidRuntime(9401): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
09-20 10:54:18.632: E/AndroidRuntime(9401): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
09-20 10:54:18.632: E/AndroidRuntime(9401): at android.app.Activity.startActivityForResult(Activity.java:2827)
09-20 10:54:18.632: E/AndroidRuntime(9401): at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:824)
09-20 10:54:18.632: E/AndroidRuntime(9401): at android.app.Activity.startActivity(Activity.java:2933)
09-20 10:54:18.632: E/AndroidRuntime(9401): at pl.looksok.viewpagerdemo.FragmentBlue$1.onClick(FragmentBlue.java:25)
09-20 10:54:18.632: E/AndroidRuntime(9401): at android.view.View.performClick(View.java:2538)
09-20 10:54:18.632: E/AndroidRuntime(9401): at android.view.View$PerformClick.run(View.java:9152)
09-20 10:54:18.632: E/AndroidRuntime(9401): at android.os.Handler.handleCallback(Handler.java:587)
09-20 10:54:18.632: E/AndroidRuntime(9401): at android.os.Handler.dispatchMessage(Handler.java:92)
09-20 10:54:18.632: E/AndroidRuntime(9401): at android.os.Looper.loop(Looper.java:130)
09-20 10:54:18.632: E/AndroidRuntime(9401): at android.app.ActivityThread.main(ActivityThread.java:3689)
09-20 10:54:18.632: E/AndroidRuntime(9401): at java.lang.reflect.Method.invokeNative(Native Method)
09-20 10:54:18.632: E/AndroidRuntime(9401): at java.lang.reflect.Method.invoke(Method.java:507)
09-20 10:54:18.632: E/AndroidRuntime(9401): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
09-20 10:54:18.632: E/AndroidRuntime(9401): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
09-20 10:54:18.632: E/AndroidRuntime(9401): at dalvik.system.NativeStart.main(Native Method)
所以问题是如何在片段之间使用intent。有没有更好的方法将注册表单分成三个部分而不使用viewpager?
【问题讨论】:
您是否在 AndroidManifest.xml 中声明了此活动? 【参考方案1】:你不能这样调用你的 Fragment,
Intent intent = new Intent(view.getContext(), FragmentGreen.class);
view.getContext().startActivity(intent);
getActivity().finish();
你需要这样调用你的片段
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
FragmentGreen llf = new FragmentGreen();
ft.replace(R.id.listFragment, llf);
ft.commit();
Intent
基本上用于从另一个活动调用一个活动。要添加新的Fragment
,您不能使用Intent
。为此,您必须使用FragmentManager
和开放片段FragmentTransaction
。
更多详情请转至Here
【讨论】:
我应该在 R.id.listFragment 使用什么? listFragment 是 Fragment 容器的 id。因此,您必须在一个 ID 为 listFragment 的 xml 文件中添加一个 FrameLayout,以便在该布局中添加片段。 先检查这个Intent intent = new Intent(view.getContext(), FragmentGreen.class); view.getContext().startActivity(intent); getActivity().finish();然后告诉我【参考方案2】:如果所有片段都在同一个 ViewPager 中,并且您只想导航到该片段,那么您不需要任何 Intents
。你只需要:
mViewPager.setCurrentItem(POSITION, true);
这里的 POSITION 是一个整数。
现在视图寻呼机在Activity
中,但Button
在Fragment
中,因此您需要在活动中创建此方法
public void selectFragment(int position)
mViewPager.setCurrentItem(position, true);
// true is to animate the transaction
并在您的OnClickListener
中这样调用:
((MainActivity)getActivity()).selectFragment(POSITION_YOU_WANNA_SELECT);
【讨论】:
我得到错误 void is invalid type for the method selectFragment【参考方案3】:public class Fragment3 extends Fragment
Button button;
@Nullable
@Override
public View onCreateView(@NonNull final LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState)
final View view;
Fragment fragment;
view = inflater.inflate(R.layout.page_4, container, false);
button=view.findViewById(R.id.exit);
button.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
FragmentManager fm = getFragmentManager();
assert fm != null;
FragmentTransaction ft = fm.beginTransaction();
FragmentLogin llf = new FragmentLogin();
ft.replace(R.id.FrameButton, llf);
ft.commit();
);
return view;
【讨论】:
【参考方案4】:FragmentManager fm=getFragmentManager();
fm.beginTransaction().replace(R.layout.fragment_blue, (Fragment)new FragmentGreen()).commit();
getActivity().getActionBar().setTitle("Home");
【讨论】:
以上是关于使用意图从另一个片段调用一个片段的主要内容,如果未能解决你的问题,请参考以下文章