我在数组中具有视频链接,并且我正在使用垂直页面查看器和Fragment实例在视频之间垂直滑动。第一个和最后一个片段都工作正常,但是当我滑动或单击视频数组索引为0的第一个视频时,索引1的视频正在播放和暂停,但索引0的操作未发生。帮助我提供解决方案,我已经尝试了2天了,我是android studio的新手。
package com.example.final_view_pager;
import android.annotation.TargetApi;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;
import java.util.ArrayList;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
public class MainActivity extends AppCompatActivity {
MyAdapter mAdapter;
VerticalViewPager mPager;
public static VideoView videoView;
private String videosArray[] = {
"android.resource://com.example.final_view_pager/" + R.raw.four,
"android.resource://com.example.final_view_pager/" + R.raw.six,
"android.resource://com.example.final_view_pager/" + R.raw.video,
"android.resource://com.example.final_view_pager/" + R.raw.video1,
"android.resource://com.example.final_view_pager/" + R.raw.video2,
"android.resource://com.example.final_view_pager/" + R.raw.video3,
"android.resource://com.example.final_view_pager/" + R.raw.video5,
"android.resource://com.example.final_view_pager/" + R.raw.video6
};
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new MyAdapter(getSupportFragmentManager(), videosArray);
mPager = findViewById(R.id.viewpager);
mPager.setAdapter(mAdapter);
mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Toast.makeText(getApplicationContext(), " current position is " + position, Toast.LENGTH_SHORT).show();
//Log.i("page", "on page scrolled");
}
@Override
public void onPageSelected(int position) {
Integer current_item = mPager.getCurrentItem();
Log.i("page", " cure pos " + current_item);
FragmentInstance.newInstance(position, videosArray[position]);
Log.i("page", "on page selected position is " + position);
}
@Override
public void onPageScrollStateChanged(int state) {
//Log.i("page", "on page scroll state changed");
}
});
}
public static class MyAdapter extends FragmentPagerAdapter {
String videosArray[];
//private long baseId = 0;
public MyAdapter(FragmentManager fm, String[] videosArr) {
super(fm);
videosArray = videosArr;
}
@Override
public int getCount() {
return videosArray.length;
//return 3;
//return NUMBER_OF_PAGES;
}
public void notifyChangeInPosition(int n) {
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((View) view);
}
@Override
public Fragment getItem(int position) {
}
}
public static class FragmentInstance extends Fragment {
private static final String videourlname = "video_url";
private static final String indexs = "position";
String videourl;
Integer position;
static FragmentInstance newInstance(Integer position, String videourl) {
FragmentInstance f = new FragmentInstance();
Bundle args = new Bundle();
args.putString(videourlname, videourl);
args.putInt(indexs, position);
f.setArguments(args);
return f;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_one, container, false);
videoView = v.findViewById(R.id.video_view);
Uri uri = Uri.parse(videourl);
Toast.makeText(getContext(), "url is " + videourl, Toast.LENGTH_SHORT);
videoView.setVideoURI(uri);
Log.i("page", "playing position is " + position);
if (position == 1) {
Log.i("page", "position is different" + position);
videoView.start();
}
videoView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i("page", "clicked position is " + position);
if (videoView.isPlaying()) {
Log.i("page", "playing check position is" + position);
Toast.makeText(getContext(), " clicked position is " + position, Toast.LENGTH_SHORT);
videoView.pause();
} else {
Log.i("page", "playing else position is" + position);
Toast.makeText(getContext(), " clicked position is " + position, Toast.LENGTH_SHORT);
videoView.start();
}
}
});
return v;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
videourl = getArguments() != null ? getArguments().getString(videourlname) : "Null";
position = getArguments() != null ? getArguments().getInt(indexs) : 0;
}
}
}