从Activity获取片段视图的id

时间:2017-05-26 08:36:02

标签: android android-fragments onbackpressed

我有片段视频观看。当我按下按钮时,它在横向模式下显示为全视图,但是当我按下它时应该进入正常视图。为此,我在Activity中处理onBackPressed()但我无法在Activity中获取该视频观看的ID。那么如何在Activity onBackPressed() FragmentFragment中获取视频观看ID。

这是fullScreen = (ImageView) controller.findViewById(R.id.fullscreen); fullScreen.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 8.0f); videoView.setLayoutParams(param); getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } }); 中以完整视图显示视频的代码:

Activity

这是我需要在@Override public void onBackPressed() { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 5.0f); videoView.setLayoutParams(param); if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { super.onBackPressed(); } }

中处理的内容
videoView

但我需要Activity个ID。如何在<select id='selectdesign'> <option value="0"></option> <option value="top">top</option> <option value="jeans">jeans</option> </select> <select id='selectmenu'> <option value="0"></option> <option value="1">top-blue</option> <option value="2">top-pink</option> <option value="3">bottoms-red</option> <option value="4">bottoms-violet</option> </select> function selectOption(element) { if (element.value == 'top') { console.log('if'); option[2].setAttribute('selected', true); } else if (element.value == 'jeans') { console.log('else if'); option[1].setAttribute('selected', true); } } 中获取它?

2 个答案:

答案 0 :(得分:1)

强力方式是从你的片段中获取该视图:


@Override
public void onBackPressed() {

    VideoView videoView = yourFragment.getView().findViewById(R.id.your_video_view);
    ...
}


答案 1 :(得分:0)

您可以执行以下操作之一:

  1. 调用将Fragment连接到Activity的接口方法。
  2. 从您的活动中调用Fragment的公共方法。