我正在使用以下代码在其中显示带有ViewPager的对话框
new Handler().postDelayed(new Runnable()
{
@Override
public void run()
{
TutorialAlerts alert = new TutorialAlerts(MainActivity.this, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen, TutorialAlerts.TUTORIAL_ALERT_INITIAL);
alert.show();
}
}, 3000);
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;
import com.iyedah.weping.R;
import com.iyedah.weping.fragments.TutorialFragment;
import com.viewpagerindicator.CirclePageIndicator;
import java.util.ArrayList;
public class TutorialAlerts extends Dialog
{
public ArrayList<TutorialFragment> fragments = new ArrayList<TutorialFragment>();
private int type = TUTORIAL_ALERT_CHAT;
public static final int TUTORIAL_ALERT_CHAT = 0;
public static final int TUTORIAL_ALERT_MAP = 1;
public static final int TUTORIAL_ALERT_INITIAL = 2;
public static final int TUTORIAL_ALERT_MESSAGE = 3;
private CirclePageIndicator cirleIndicator;
private ViewPager pager;
private FragmentActivity mContext;
private TextView nextBtn;
private TextView title;
private PagerAdapter adapter;
public TutorialAlerts(FragmentActivity context, int theme, int type)
{
super(context, theme);
this.mContext = context;
this.type = type;
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_tutorial);
nextBtn = (TextView) this.findViewById(R.id.nextBtn);
title = (TextView) findViewById(R.id.title);
pager = (ViewPager) findViewById(R.id.pager);
pager.setOnPageChangeListener(mPageChangeListener);
cirleIndicator = (CirclePageIndicator) findViewById(R.id.cirleIndicator);
if (type == TUTORIAL_ALERT_INITIAL)
{
title.setText("Getting started");
TutorialFragment page1 = new TutorialFragment();
Bundle bundle = new Bundle();
bundle.putInt("pageNumber", 1);
page1.setArguments(bundle);
fragments.add(page1);
TutorialFragment page2 = new TutorialFragment();
bundle = new Bundle();
bundle.putInt("pageNumber", 2);
page2.setArguments(bundle);
fragments.add(page2);
TutorialFragment page3 = new TutorialFragment();
bundle = new Bundle();
bundle.putInt("pageNumber", 3);
page3.setArguments(bundle);
fragments.add(page3);
}
adapter = new MyPagerAdapter(mContext.getSupportFragmentManager());
}
@Override
public void show()
{
super.show();
pager.setAdapter(adapter);
cirleIndicator.setViewPager(pager);
pager.setCurrentItem(0, true);
}
/************************************************************************
*
*
**********************************************************************/
public class MyPagerAdapter extends FragmentPagerAdapter
{
public MyPagerAdapter(FragmentManager fm)
{
super(fm);
}
@Override
public CharSequence getPageTitle(int position)
{
return "";
}
@Override
public int getCount()
{
return fragments.size();
}
@Override
public Fragment getItem(int position)
{
return fragments.get(position);
}
}
private ViewPager.OnPageChangeListener mPageChangeListener = new ViewPager.OnPageChangeListener()
{
@Override
public void onPageSelected(final int position)
{
fragments.get(position).setupInitialTutorial(position + 1);
if(position == fragments.size())
{
nextBtn.setText("Done");
nextBtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
dismiss();
}
});
}
else
{
nextBtn.setText("Next");
nextBtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
pager.setCurrentItem(position + 1, true);
}
});
}
}
@Override
public void onPageScrolled(int position, float arg1, int arg2)
{
}
@Override
public void onPageScrollStateChanged(int arg0)
{
}
};
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#C8000000"
>
<TextView
android:layout_marginTop="20dp"
android:gravity="center"
android:textStyle="bold"
android:textSize="23sp"
android:textColor="@color/green"
android:text="@string/use_app"
android:layout_width="match_parent"
android:layout_height="58dp"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:id="@+id/title"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_above="@+id/nextBtn"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<com.viewpagerindicator.CirclePageIndicator
android:layout_centerHorizontal="true"
android:id="@+id/cirleIndicator"
android:layout_marginBottom="10dp"
android:layout_above="@+id/nextBtn"
android:layout_height="40dp"
android:layout_width="60dp" />
<TextView
android:gravity="center"
android:textStyle="bold"
android:textSize="20sp"
android:layout_alignParentBottom="true"
android:background="@color/mild_black"
android:id="@+id/nextBtn"
android:textColor="@color/green"
android:text="@string/next"
android:layout_width="match_parent"
android:layout_height="58dp"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
/>
</RelativeLayout>
对话框显示确定,但viewPager似乎是隐形的,没有显示任何内容。
答案 0 :(得分:1)
我扩展了dialogfragment类并创建了新的customdialog
public class CustomDailog extends DialogFragment {
private View view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.addon_dialog, container);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
getDialog().setCancelable(true);
getDialog().getWindow().setGravity(Gravity.TOP);
return view;
}
@Override
public void onActivityCreated(Bundle arg0) {
super.onActivityCreated(arg0);
DisplayMetrics metrics = getResources().getDisplayMetrics();
int screenWidth = (int) (metrics.widthPixels * 0.95);
int screenHeight = (int) (metrics.heightPixels * 0.95);
getDialog().getWindow().setLayout(screenWidth, screenHeight);
android.support.v4.view.ViewPager addonsviewpager = (android.support.v4.view.ViewPager) view.findViewById(R.id.addonsviewpager);
}
}
addon_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/addonsviewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">
</android.support.v4.view.ViewPager>
</RelativeLayout>
并在代码中使用此对话框作为...
CustomDailog customDailog=new CustomDailog ();
customDailog.show(getFragmentManager(),"addons_fragment");