gpt4 book ai didi

android - 获取 fragment 宽度,viewtreeobserver 监听器不工作

转载 作者:行者123 更新时间:2023-11-29 19:38:22 24 4
gpt4 key购买 nike

我有一个 fragment 可以在细节 Activity 中(如果设备的视口(viewport)很小)或在主要 Activity 中(如果视口(viewport)很大,例如平板电脑)。

fragment 内部有一个 ImageView,我需要根据 fragment 的宽度调整其大小。我做了一些研究,发现实现此目的的“规范”方法是向 viewtreeobserver 添加一个监听器。但是,由于某种原因,它不起作用:

这是我的 fragment :

public class MediaDetailFragment extends Fragment implements DetailsMvpView {

private static final String RESOURCE = "resource";

@Inject DetailPresenter mPresenter;
@BindView(R.id.media_image_flipper)
ViewFlipper mImageFlipper;
@BindView(R.id.title_textview)
TextView mTitleTextView;
@BindView(R.id.button_share)
ImageButton mShare;
@BindView(R.id.button_trailers) ImageButton mTrailers;
@BindView(R.id.keyword_recyclerview)
RecyclerView mKeywordRecyclerView;
@BindView(R.id.overview_content_textview) TextView mOverviewTextView;
@BindView(R.id.main_image_holder) ViewGroup mImageHolder;
@BindView(R.id.parent_cardview)
CardView mParentCardView;

@BindString(R.string.youtube_base_url) String mYoutubeUrl;
@BindBool(R.bool.isTablet) boolean mIsTablet;

private Media mMedia;
private DisplayMetrics mDisplayMetrics = new DisplayMetrics();

public static MediaDetailFragment insertOnTarget(AppCompatActivity activity, int target, Media media) {
MediaDetailFragment fragment = MediaDetailFragment.instantiateWithArguments(media);
FragmentManager fm = activity.getSupportFragmentManager();
fm.beginTransaction()
.replace(target, fragment)
.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right)
.commit();
return fragment;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mMedia = getArguments().getParcelable(RESOURCE);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.media_details_fragment, container, false);
((BaseActivity)getActivity()).activityComponent().inject(this);
ButterKnife.bind(this, rootView);

getActivity().getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);



mTitleTextView.setText(mMedia.title());
mOverviewTextView.setText(mMedia.overview());

mKeywordRecyclerView.setAdapter(new KeywordAdapter(getContext()));

return rootView;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

final View view = getView();
if (view == null) return;
view.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
if (mIsTablet) {
mImageFlipper.getLayoutParams().height =
ViewUtil.setHeightForAspectRatio(view.getWidth(), ViewUtil.PORTRAIT);
} else {
mImageFlipper.getLayoutParams().height =
ViewUtil.setHeightForAspectRatio(mDisplayMetrics.widthPixels, ViewUtil.PORTRAIT);
}
});
}

@Override
public void onStart() {
super.onStart();
mShare.setOnClickListener(v -> {startActivity(new Intent(Intent.ACTION_SEND)
.putExtra(Intent.EXTRA_TEXT, String.format(mYoutubeUrl, mMedia.overview())));});
mTrailers.setOnClickListener(v -> mPresenter.loadMovies(mMedia.id()));
mPresenter.attachView(this);
mPresenter.loadImages(mMedia.id());
mPresenter.loadKeywords(mMedia.id());
}


@Override
public void onStop() {
mPresenter.detachView();
super.onStop();
}

@Override
public void showImages(String images) {
ImageView imageView = new ImageView(getContext());
ViewUtil.loadImage(images, imageView, getContext(), true, true);
mImageFlipper.addView(imageView, new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
}

@Override
public void showKeywords(List<String> keywords) {
((KeywordAdapter) mKeywordRecyclerView.getAdapter()).setKeywords(keywords);
}

@Override
public void showVideos(List<Video> videos) {
TrailerDialogFragment.newInstance(videos).show(getFragmentManager(), null);
}

@Override
public void showError() {

}

private static MediaDetailFragment instantiateWithArguments(Media media) {
Bundle bundle = new Bundle();
bundle.putParcelable(RESOURCE, media);
MediaDetailFragment fragment = new MediaDetailFragment();
fragment.setArguments(bundle);
return fragment;
}

}

我在 onactivitycreated 回调上调用附加监听器。

你能帮我找出丢失的部分吗?

谢谢。

最佳答案

出于某些奇怪的原因,当我将代码包装在可运行对象中时,代码可以正常工作。这是我的解决方案:

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

final View view = getView();
if (view == null) return;
view.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
view.post(() -> {
if (mIsTablet) {
mImageFlipper.getLayoutParams().height =
ViewUtil.setHeightForAspectRatio(view.getWidth(), ViewUtil.PORTRAIT);
} else {
mImageFlipper.getLayoutParams().height =
ViewUtil.setHeightForAspectRatio(mDisplayMetrics.widthPixels, ViewUtil.PORTRAIT);
}
});

});
}

我终于可以解决它,但我不明白为什么。如果你们中有人知道为什么会发生这种情况,请告诉我。

关于android - 获取 fragment 宽度,viewtreeobserver 监听器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39004023/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com