枫之缘
2019-05-22 22:22
采纳率: 60%
浏览 1.1k

DialogFragment中使用viewpager+fragment时出现问题

fragment does not have a view

package com.yidi.livelibrary.widget.dialog;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
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.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.hn.library.utils.AppLogger;
import com.yidi.livelibrary.R;
import com.yidi.livelibrary.ui.fragment.HnContributionFragment;

import java.util.ArrayList;
import java.util.List;


/**
 * Menu Dialog
 */
public class HnContributionDialog extends DialogFragment implements View.OnClickListener {

    private FragmentActivity mActivity;
    private TextView tvLeft;
    private TextView tvMiddle;
    private TextView tvRight;
    private ViewPager viewPager;
    private String anchor_user_id;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    public static HnContributionDialog newInstance(String anchor_user_id) {
        Bundle bundle = new Bundle();
        bundle.putString("anchor_user_id",anchor_user_id);
        HnContributionDialog sDialog = new HnContributionDialog();
        sDialog.setArguments(bundle);
        return sDialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = getActivity();
        Bundle bundle = getArguments();
        if (bundle != null) {
            anchor_user_id = bundle.getString("anchor_user_id");
            AppLogger.e(anchor_user_id);
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        View view = View.inflate(mActivity, R.layout.live_left_dialog, null);
        Dialog dialog = null;
        dialog = new Dialog(mActivity, R.style.LeftDialog);
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(true);
        Window alertWindow = dialog.getWindow();
        WindowManager.LayoutParams params = alertWindow.getAttributes();
        alertWindow.setGravity(Gravity.LEFT);
        params.height = (mActivity.getWindowManager().getDefaultDisplay().getHeight());
        params.width = mActivity.getWindowManager().getDefaultDisplay().getWidth();
        alertWindow.setAttributes(params);
        dialog.getWindow().setDimAmount(0f);
        //初始化组件
        initView(view);
        return dialog;

    }
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }


    /**
     * 初始化视图
     *
     * @param view
     */
    private void initView(View view) {
        tvLeft = (TextView) view.findViewById(R.id.tvLeft);
        tvMiddle = (TextView) view.findViewById(R.id.tvMiddle);
        tvRight = (TextView) view.findViewById(R.id.tvRight);
        viewPager = (ViewPager) view.findViewById(R.id.vp);
        view.findViewById(R.id.vOut).setOnClickListener(this);
        tvLeft.setSelected(true);
        tvLeft.setOnClickListener(this);
        tvMiddle.setOnClickListener(this);
        tvRight.setOnClickListener(this);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        tvLeft.setSelected(true);
                        tvMiddle.setSelected(false);
                        tvRight.setSelected(false);
                        break;
                    case 1:
                        tvLeft.setSelected(false);
                        tvMiddle.setSelected(true);
                        tvRight.setSelected(false);
                        break;
                    case 2:
                        tvLeft.setSelected(false);
                        tvMiddle.setSelected(false);
                        tvRight.setSelected(true);
                        break;
                    default:
                        break;
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        mFragments.add(HnContributionFragment.newInstance("day",anchor_user_id));
        mFragments.add(HnContributionFragment.newInstance("week",anchor_user_id));
        mFragments.add(HnContributionFragment.newInstance("all",anchor_user_id));
        PagerAdapter adapter = new PagerAdapter(getFragmentManager(), mFragments);
        viewPager.setAdapter(adapter);
    }
    public  void setViewPager(){

    }
    class PagerAdapter extends FragmentPagerAdapter {

        private List<Fragment> mFragments;

        public PagerAdapter(FragmentManager fm, List<Fragment> mFragments) {
            super(fm);
            this.mFragments = mFragments;
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return null;
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = mFragments.get(position);
            return fragment;
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tvLeft) {
            tvLeft.setSelected(true);
            tvMiddle.setSelected(false);
            tvRight.setSelected(false);
        } else if (id == R.id.tvMiddle) {
            tvLeft.setSelected(false);
            tvMiddle.setSelected(true);
            tvRight.setSelected(false);
        } else if (id == R.id.tvRight) {
            tvLeft.setSelected(false);
            tvMiddle.setSelected(false);
            tvRight.setSelected(true);
        } else if (id == R.id.vOut) {
            dismiss();
        }

    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        mActivity = null;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }


}

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • 枫之缘 2019-06-15 10:07
    已采纳

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // TODO: inflate a fragment view
    View rootView =View.inflate(mActivity, R.layout.live_left_dialog, null);
    initView(rootView);
    return rootView;
    }

    已采纳该答案
    打赏 评论
  • 王能 2019-05-23 13:57

    嵌套的Fragment请使用getChildFragmentManager()
    在你new PagerAdapter那一行

    打赏 评论

相关推荐 更多相似问题