gpt4 book ai didi

android - 简单对话框中的 ViewPager

转载 作者:搜寻专家 更新时间:2023-11-01 08:06:27 24 4
gpt4 key购买 nike

我想在对话框中使用 ViewPager,但失败了!这是所有代码。对话框中显示两个 fragment :

布局

ma​​in.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main" >

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dialog" />

</RelativeLayout>

details.xml( fragment 布局)

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

<TextView
android:id="@+id/detailsText"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginTop="20dip"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30dip" />

</LinearLayout>

image.xml( fragment 布局)

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

<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher" />

</LinearLayout>

dialog.xml

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

<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />

<com.viewpagerindicator.CirclePageIndicator
android:id="@+id/indicator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip" />

</LinearLayout>

主.java

public class Main extends FragmentActivity {

private Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

setupUIComponents();
setupListeners();
}

private void setupUIComponents() {
button = (Button) findViewById(R.id.button);
}

private void setupListeners() {
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MyDialog dialog = new MyDialog(Main.this);
dialog.startDialog();
}
});
}
}

MyDialog.java

public class MyDialog extends FragmentActivity {

private Dialog dialog;
private Context context;
private CirclePageIndicator mIndicator;
private Main mMain;
private ViewPager mPager;
private FragmentAdapter mAdapter;

public MyDialog(Context context) {
super();
this.context = context;
this.mMain = (Main) context;
}

public void startDialog() {
dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog);

mAdapter = new FragmentAdapter(mMain.getSupportFragmentManager());
mPager = (ViewPager) dialog.findViewById(R.id.pager);
mPager.setAdapter(mAdapter);

mIndicator = (CirclePageIndicator) dialog.findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
mIndicator.setCurrentItem(0);

dialog.show();
}

}

fragment 适配器.java

public class FragmentAdapter extends FragmentPagerAdapter {
public FragmentAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int which) {
switch (which) {
case 0:
ImageFragment imageFragment = new ImageFragment();
return imageFragment;
case 1:
DetailFragment detailsFragment = new DetailFragment();
return detailsFragment;
}
return null;
}

@Override
public int getCount() {
return 2;
}

}

DetailsFragment.java

public class DetailFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.details, container, false);
TextView textView = (TextView) view.findViewById(R.id.detailsText);
textView.setText("Testing");
return view;
}
}

ImageFragment.java

public class ImageFragment extends Fragment {

public ImageFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.image, container, false);
ImageView imageView = (ImageView) view.findViewById(R.id.imageView1);
imageView.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));
return view;
}
}

为了快速总结代码,Main.java 已启动,单击按钮将在 View 分页器中启动一个包含两个 fragment 的对话框。

有堆栈跟踪:

12-11 11:28:15.999: E/AndroidRuntime(7453): FATAL EXCEPTION: main
12-11 11:28:15.999: E/AndroidRuntime(7453): java.lang.IllegalArgumentException: No view found for id 0x7f040006 for fragment ImageFragment{410a2468 #0 id=0x7f040006 android:switcher:2130968582:0}
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:864)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:431)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:139)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.support.v4.view.ViewPager.populate(ViewPager.java:804)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1016)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.view.View.measure(View.java:15172)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.view.View.measure(View.java:15172)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.view.View.measure(View.java:15172)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.view.View.measure(View.java:15172)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.view.View.measure(View.java:15172)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-11 11:28:15.999: E/AndroidRuntime(7453): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.view.View.measure(View.java:15172)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1850)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1077)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1275)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.view.Choreographer.doFrame(Choreographer.java:525)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.os.Handler.handleCallback(Handler.java:615)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.os.Handler.dispatchMessage(Handler.java:92)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.os.Looper.loop(Looper.java:137)
12-11 11:28:15.999: E/AndroidRuntime(7453): at android.app.ActivityThread.main(ActivityThread.java:4966)
12-11 11:28:15.999: E/AndroidRuntime(7453): at java.lang.reflect.Method.invokeNative(Native Method)
12-11 11:28:15.999: E/AndroidRuntime(7453): at java.lang.reflect.Method.invoke(Method.java:511)
12-11 11:28:15.999: E/AndroidRuntime(7453): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
12-11 11:28:15.999: E/AndroidRuntime(7453): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
12-11 11:28:15.999: E/AndroidRuntime(7453): at dalvik.system.NativeStart.main(Native Method)

同样,是否可以在对话框的 viewpager 中显示 fragment ?如果是这样,我哪里出错了?

最佳答案

Again, is it possible to show fragments in a viewpager in a dialog?

是的,但不是您尝试的方式(如果我没有遗漏您的代码中的某些内容),ViewPager 将无法将这些子 Fragments 附加为它被放置在对话框中。 4.2 API 级别带来了 nested Fragments (但也可用于较低版本的兼容包的新版本),您应该使用它来显示您的“对话 fragment ”。

在您的 Main Activity 中,您将创建一个 DialogFragment 并在该 DialogFragment 中设置 ViewPager 和将 getChildFragmentManager() 传递给它的适配器。

此外,只需在 Main Activity 中使用移动 startDialog() 方法并且不要实例化一个新的 Activity 因为这不是 Android 的运行方式(在您的情况下它可以工作,因为您没有对它做任何事情)。

关于android - 简单对话框中的 ViewPager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13819516/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com