如何从自定义视图调用片段

时间:2016-03-23 12:31:57

标签: android android-fragments android-view

我尝试从自定义View类调用 Fragment 但是我的代码无效:

public class Footer extends RelativeLayout implements View.OnClickListener {
  private Context context = null;

  private ImageView submitButton = null;

  private ImageView searchButton = null;    

  private ImageView closeButton = null;

  public Footer(Context context, AttributeSet attrs) {
    super(context);
    initialize(context);
  }

  private void initialize(Context context) {
    this.context = context;
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.footer, this);
    ImageView imgFB = (ImageView) findViewById(R.id.camera);
    imgFB.setOnClickListener(this);
  } 

  @Override
  public void onClick(View v) {
    switch (v.getId()) {
    case R.id.camera:
      selectImage();
      break;
    case R.id.submit:
      Fragment fragment = new AddProductFragment();
      FragmentManager fragmentManager = fragment.getFragmentManager();
      fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).addToBackStack(null).commit();
      break;
    }
  }

  private void selectImage() {
    Dialog cameraDialog = new Dialog(context);
    cameraDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    cameraDialog.setContentView(R.layout.camera_popup);
    submitButton = (ImageView) cameraDialog.findViewById(R.id.submit);
    searchButton = (ImageView) cameraDialog.findViewById(R.id.seacrh);
    closeButton = (ImageView) cameraDialog.findViewById(R.id.close);

    submitButton.setOnClickListener(this);
    searchButton.setOnClickListener(this);
    closeButton.setOnClickListener(this);

    cameraDialog.show();
  }
}

为什么这段代码不起作用?可能是我在这里做错了。如何从自定义View调用片段?

我们可以通过 getActivity()方法轻松调用 Fragment 。但在本课程中,我没有 getActivity()方法,这就是为什么我无法使用 getActivity()

3 个答案:

答案 0 :(得分:0)

使用此函数调用任何片段

R.id.linearLayoutDataContent 是片段的竞争者

public void showFragmentInContener(Fragment fragment)
    {
        FragmentManager fragmentManager;
        FragmentTransaction fragmentTransaction;

        fragmentManager = getFragmentManager();
        fragmentTransaction =   fragmentManager.beginTransaction();

        fragmentTransaction.replace(R.id.linearLayoutDataContent, fragment);
        fragmentTransaction.commit();
    }

答案 1 :(得分:0)

RelativeLayout是一个观点。它必须存在于某些activityfragment中。处理fragment从一个应存在于片段内的视图内部进行交换没有建筑意义。

创建一个新的FragmentActivity(或只是Activity)并在xml文件中使用您的视图或以编程方式创建。将onlickListener等放在Activity中不在视图中。

答案 2 :(得分:0)

从视图中调用片段是不正确的,所以在这种情况下你可以创建一个回调(在我的情况下它是 OnBackListener )界面如下:

public class EmptyPage extends FrameLayout {

    private View mRootLayout;
    private TextView mTitle;
    private TextView mDescription;
    private TextView mIcon;

    private OnBackClickListener onBackClickListener;

    public interface OnBackClickListener {
        public void onBackClick();
    }


    public EmptyPage(Context context) {
        super(context);
        if (!isInEditMode())
            init(null);
    }

    public EmptyPage(Context context, AttributeSet attrs) {
        super(context, attrs);
        if (!isInEditMode())
            init(attrs);
    }

    public EmptyPage(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        if (!isInEditMode())
            init(attrs);
    }

    private void init(AttributeSet attrs) {

        mRootLayout = inflate(getContext(), R.layout.include_empty_page, null);
        mTitle = (TextView) mRootLayout.findViewById(R.id.text_warning);
        mIcon = (TextView) mRootLayout.findViewById(R.id.icon_warning);
        mDescription = (TextView) mRootLayout.findViewById(R.id.text_warning_hint);


        if (attrs != null) {
            TypedArray attributes = getContext().obtainStyledAttributes(attrs, R.styleable.EmptyPage);

            String text = attributes.getString(R.styleable.EmptyPage_android_text);
            if (text != null)
                mTitle.setText(text);

            attributes.recycle();
        }

        //put your clickable items here
        mRootLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onBackClickListener != null) {
                    onBackClickListener.onBackClick();
                }
            }
        });
        addView(mRootLayout);
    }
}

然后在您想要的项目上放置onClickListener。 (在我的情况下,我希望整个视图可以点击,所以我把它放在 mRootLayout

然后在你的Activity / Fragment中你应该做这样的事情:

mEmptyPage = (EmptyPage) rootView.findViewById(R.id.ep_empty_view);
        mEmptyPage.setOnBackClickListener(new EmptyPage.OnBackClickListener() {
            @Override
            public void onBackClick() {

                //Open new FRAGMENT HERE 

            }
        });

已经完成了。当您点击查看 OnBackClick 时,您可以毫无例外地打开您的片段。

P.S:如果您需要更多可点击的项目,您只需要在界面中添加另一种方法(例如添加 onBackClick2