gpt4 book ai didi

java - 销毁 ViewPager

转载 作者:太空宇宙 更新时间:2023-11-04 09:07:13 33 4
gpt4 key购买 nike

我正在尝试克隆 TikTok 应用。对于主屏幕,我制作了一个 VerticalViewPager(自定义 View 分页器),其中包含“点赞”按钮、标题和评论。我正在从 Firebase 检索视频。 TikTok 模型有视频 URL、标题名称和点赞数。问题是当我尝试转到下一个视频时,上一个视频仍在播放背景。我试图销毁该 fragment ,但似乎不起作用。

这是我的HomeActivity.java

public class HomeActivity extends AppCompatActivity {

private VerticalPageViewer viewPager;
private ViewPageAdapter viewPageAdapter;
private List<TikTokModel> tikTokModels;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);

viewPager = findViewById(R.id.viewPager);
tikTokModels = new ArrayList<>();
FirebaseFirestore.getInstance().collection("TikTok")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
tikTokModels.clear();
if(task.isSuccessful()){
for(QueryDocumentSnapshot queryDocumentSnapshot : task.getResult()){
tikTokModels.add(queryDocumentSnapshot.toObject(TikTokModel.class));
}
Log.d("TEST", "onComplete: "+tikTokModels.get(0).getTitle());

viewPageAdapter = new ViewPageAdapter(getSupportFragmentManager(),tikTokModels);
viewPager.setAdapter(viewPageAdapter);
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {

}
});
}
}

还有ViewPageAdapter.java

class ViewPageAdapter extends PagerAdapter {

private List<TikTokModel> tikTokModels;
private Context context;
private ImageButton playButton;
private VideoView videoView;

ViewPageAdapter(List<TikTokModel> tikTokModels, Context context) {
this.tikTokModels= tikTokModels;
this.context = context;
}

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

@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view.equals(object);
}

@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
View v = LayoutInflater.from(context).inflate(R.layout.tiktok_frame, container, false);

TextView title = v.findViewById(R.id.title);
videoView = v.findViewById(R.id.videoView);
LikeButton likeButton = v.findViewById(R.id.like);
playButton = v.findViewById(R.id.playButton);

playButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (videoView.isPlaying()) {
videoView.pause();
playButton.setVisibility(View.VISIBLE);
} else {
videoView.resume();
playButton.setVisibility(View.INVISIBLE);
}
}
});

Uri uri = Uri.parse(tikTokModels.get(position).getUrl());
title.setText(tikTokModels.get(position).getTitle());
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();
playButton.setVisibility(View.INVISIBLE);

container.addView(v,0);
return v;
}

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

最佳答案

在您的 TikTokFragment 中,您可以考虑重写以下生命周期方法来暂停/恢复您的视频。

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
if (videoView != null) videoView.pause();
} else {
if (videoView != null) videoView.resume();
}
}

关于java - 销毁 ViewPager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60079888/

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