对话框显示绝对没有内容?

时间:2016-12-26 05:42:32

标签: java android xml user-interface view

基本上我只想在对话框中有一个视图寻呼机。就是这样,但出于某种原因,DIALOG是空的吗?

这是我的对话框代码:

final Dialog dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.voicedialog);
    dialog.setCanceledOnTouchOutside(false);

    MyPageAdapter adapter = new MyPageAdapter();
    ViewPager pager = (ViewPager) dialog.findViewById(R.id.viewpager);
    pager.setOffscreenPageLimit(3);
    pager.setAdapter(adapter);

    dialog.show();

这是对话框布局voicedialog,只是一个空视图寻呼机:

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    />

这是适配器:

public class MyPageAdapter extends PagerAdapter {
    public Object instantiateItem(ViewGroup collection, int position) {

        int resId = 0;
        switch (position) {
            case 0:
                resId = R.id.voice1;
                break;
            case 1:
                resId = R.id.voice2;
                break;
            case 2:
                resId = R.id.voice3;
                break;
        }
        return collection.findViewById(resId);
    }

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

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View)object);
    }
}

SO SIMPLE,RIGHT?但是,当我运行此代码时,我会收到一个EMPTY对话框:

相反,它应该显示我可以轻扫的三种布局(voice1,voice2,voice3)!这些布局只有一个textview,但它没有显示。

请告诉我如何才能做到这一点。令人沮丧的是 - 我花了近6个小时试图解决这个问题:)

2 个答案:

答案 0 :(得分:0)

首先在你的PagerAapter中通过这样的构造函数获取上下文,列表和LayoutInflator

Context mContext;
LayoutInflater mLayoutInflater;
List<String> mList;

public MyPageAdapter(Context context, List<String> list) {
    mContext = context;
    mList = list;
    mLayoutInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

然后在getItemCount方法中返回列表大小

@Override
public int getCount() {
    return mList.Size;
}

然后

public Object instantiateItem(ViewGroup collection, int position) {
     View view = inflater.inflate(R.layout.your_layout, null);
     TextView textView = (TextView) layout.findViewById(R.id.text_view);
     textView.setText(mList.get(postiion);
     collection.addView(view);
     return view;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
     container.removeView((View)object);
  }
}

答案 1 :(得分:0)

按照以下步骤在对话框中打开viewPager

1 - 单击按钮,打开扩展DialogFragment的片段,在该DialogFragment中声明并初始化viewpager

2 - 接下来创建一个扩展来自android.support.v13.app.FragmentPagerAdapter的FragmentPagerAdapter的适配器,对于这个适配器,你必须使用这个gradle: - compile&#39; com.android.support:support-v13 :+&#39;

点击按钮

打开DialogFragment
private void ShowDialog() {
    DialogViewPager dialogViewPager = new DialogViewPager();
    dialogViewPager.show(getFragmentManager(),"DialogViewPager");
}

<强> DialogFragment

public class DialogViewPager extends DialogFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.dialog_layout, container);
    getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    ViewPager viewPager = (ViewPager) getView().findViewById(R.id.view_pager);
    viewPager.setAdapter(new MyViewPagerAdapter(getChildFragmentManager()));
}

class MyViewPagerAdapter extends FragmentPagerAdapter {

    public MyViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return FirstFragment.newInstance();
            case 1:
                return SecondFragment.newInstance();
            default:
                return null;
        }
    }

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

First Fragment

Second Fragment