Commit ed47c127 authored by gaodapeng's avatar gaodapeng

重新写了banner,还是继续使用之前的viewpager。

parent 70319ef7
...@@ -8,25 +8,24 @@ import android.os.Bundle; ...@@ -8,25 +8,24 @@ import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Message; import android.os.Message;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.LayoutDirection;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import com.widget.imagebanner.view.ImageVideoFragment; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import com.widget.imagebanner.R;
import com.widget.imagebanner.bean.BannerBean; import com.widget.imagebanner.bean.BannerBean;
import com.widget.imagebanner.utils.BitmapUtils; import com.widget.imagebanner.utils.BitmapUtils;
import com.widget.imagebanner.view.ImageVideoFragment;
import com.widget.imagebanner.view.ViewPager;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -41,9 +40,9 @@ import java.util.concurrent.ScheduledExecutorService; ...@@ -41,9 +40,9 @@ import java.util.concurrent.ScheduledExecutorService;
import wseemann.media.FFmpegMediaMetadataRetriever; import wseemann.media.FFmpegMediaMetadataRetriever;
public class ImageVideoBanner extends FrameLayout implements ImageVideoFragment.OnVideoCompletionListener { public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageChangeListener, ImageVideoFragment.OnVideoCompletionListener {
private static final int UPTATE_VIEWPAGER = 0; private static final int UPTATE_VIEWPAGER = 0;
private ViewPager2 mViewPager; private ViewPager mViewPager;
private List<BannerBean> mList = new ArrayList<>(); private List<BannerBean> mList = new ArrayList<>();
private ViewsPagerAdapter mAdapter; private ViewsPagerAdapter mAdapter;
private int autoCurrIndex = 0;//设置当前 第几个图片 被选中 private int autoCurrIndex = 0;//设置当前 第几个图片 被选中
...@@ -72,8 +71,9 @@ public class ImageVideoBanner extends FrameLayout implements ImageVideoFragment. ...@@ -72,8 +71,9 @@ public class ImageVideoBanner extends FrameLayout implements ImageVideoFragment.
private void initView(Context context) { private void initView(Context context) {
View view = LayoutInflater.from(context).inflate(R.layout.banner_imge_video, this, true); View view = LayoutInflater.from(context).inflate(R.layout.banner_imge_video, this, true);
mViewPager = view.findViewById(R.id.view_pager); mViewPager = view.findViewById(R.id.view_pager);
mAdapter = new ViewsPagerAdapter(((FragmentActivity) context).getSupportFragmentManager(), ((FragmentActivity) context).getLifecycle()); mAdapter = new ViewsPagerAdapter(((FragmentActivity) context).getSupportFragmentManager());
mViewPager.setAdapter(mAdapter); mViewPager.setAdapter(mAdapter);
mViewPager.setOnPageChangeListener(this);
//ViewPager手势滑动禁用 //ViewPager手势滑动禁用
mViewPager.setOnTouchListener(new OnTouchListener() { mViewPager.setOnTouchListener(new OnTouchListener() {
@Override @Override
...@@ -81,18 +81,15 @@ public class ImageVideoBanner extends FrameLayout implements ImageVideoFragment. ...@@ -81,18 +81,15 @@ public class ImageVideoBanner extends FrameLayout implements ImageVideoFragment.
return true; return true;
} }
}); });
mViewPager.registerOnPageChangeCallback(callback);
} }
public void setData(List<com.widget.imagebanner.bean.BannerBean> listBean) { public void replaceData(List<BannerBean> listBean) {
if (listBean != null) { mAdapter.replaceData(listBean);
mAdapter.setData(listBean);
}
} }
public void addData(List<com.widget.imagebanner.bean.BannerBean> listBean) { public void addData(List<BannerBean> listBean) {
mAdapter.addData(listBean); mAdapter.addData(listBean);
} }
...@@ -132,6 +129,7 @@ public class ImageVideoBanner extends FrameLayout implements ImageVideoFragment. ...@@ -132,6 +129,7 @@ public class ImageVideoBanner extends FrameLayout implements ImageVideoFragment.
mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ARTIST); mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ARTIST);
Bitmap b = mmr.getFrameAtTime(); // frame at 2 seconds Bitmap b = mmr.getFrameAtTime(); // frame at 2 seconds
bannerBean.setLoadingImage(BitmapUtils.getBytes(b)); bannerBean.setLoadingImage(BitmapUtils.getBytes(b));
b.recycle();
mmr.release(); mmr.release();
} }
return true; return true;
...@@ -149,7 +147,6 @@ public class ImageVideoBanner extends FrameLayout implements ImageVideoFragment. ...@@ -149,7 +147,6 @@ public class ImageVideoBanner extends FrameLayout implements ImageVideoFragment.
} }
} }
private ViewPager2.OnPageChangeCallback callback = new ViewPager2.OnPageChangeCallback() {
@Override @Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
...@@ -170,8 +167,6 @@ public class ImageVideoBanner extends FrameLayout implements ImageVideoFragment. ...@@ -170,8 +167,6 @@ public class ImageVideoBanner extends FrameLayout implements ImageVideoFragment.
public void onPageScrollStateChanged(int state) { public void onPageScrollStateChanged(int state) {
} }
};
//定时轮播图片,需要在主线程里面修改 UI //定时轮播图片,需要在主线程里面修改 UI
private Handler mHandler = new Handler() { private Handler mHandler = new Handler() {
...@@ -209,12 +204,10 @@ public class ImageVideoBanner extends FrameLayout implements ImageVideoFragment. ...@@ -209,12 +204,10 @@ public class ImageVideoBanner extends FrameLayout implements ImageVideoFragment.
public void startBanner() { public void startBanner() {
//如果第一页是视频 不用定时器 //如果第一页是视频 不用定时器
if (mList.size() > 0) {
if (mList.get(0).getType() == 0) { if (mList.get(0).getType() == 0) {
startBanner(period); startBanner(period);
} }
} }
}
public void startBanner(long delay) { public void startBanner(long delay) {
stopBanner(); stopBanner();
...@@ -252,22 +245,20 @@ public class ImageVideoBanner extends FrameLayout implements ImageVideoFragment. ...@@ -252,22 +245,20 @@ public class ImageVideoBanner extends FrameLayout implements ImageVideoFragment.
startBanner(0); startBanner(0);
} }
private final class ViewsPagerAdapter extends FragmentStateAdapter { private final class ViewsPagerAdapter extends FragmentStatePagerAdapter {
private ImageVideoFragment fragment; private ImageVideoFragment fragment;
private FragmentManager fm; private FragmentManager fm;
public ViewsPagerAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) { public ViewsPagerAdapter(FragmentManager fm) {
super(fragmentManager, lifecycle); super(fm);
this.fm = fragmentManager; this.fm = fm;
} }
@NonNull
@Override @Override
public Fragment createFragment(int position) { public Fragment getItem(int position) {
fragment = new ImageVideoFragment(); fragment = new ImageVideoFragment();
fragment.setOnVideoCompletionListener(ImageVideoBanner.this); fragment.setOnVideoCompletionListener(ImageVideoBanner.this);
com.widget.imagebanner.bean.BannerBean bannerBean = mList.get(position); BannerBean bannerBean = mList.get(position);
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putSerializable("bannerBean", bannerBean); bundle.putSerializable("bannerBean", bannerBean);
bundle.putBoolean("loop", getLoop()); bundle.putBoolean("loop", getLoop());
...@@ -275,11 +266,6 @@ public class ImageVideoBanner extends FrameLayout implements ImageVideoFragment. ...@@ -275,11 +266,6 @@ public class ImageVideoBanner extends FrameLayout implements ImageVideoFragment.
return fragment; return fragment;
} }
@Override
public int getItemCount() {
return mList.size();
}
/** /**
* 只有1个视频的时候循环播放 * 只有1个视频的时候循环播放
* *
...@@ -292,17 +278,37 @@ public class ImageVideoBanner extends FrameLayout implements ImageVideoFragment. ...@@ -292,17 +278,37 @@ public class ImageVideoBanner extends FrameLayout implements ImageVideoFragment.
return false; return false;
} }
public void setData(@NonNull List<BannerBean> listBean) { @Override
public int getCount() {
return mList.size();
}
@Override
public int getItemPosition(Object object) {
if (mList.size() > 0) {
return POSITION_NONE;
}
return super.getItemPosition(object);
}
public void replaceData(List<BannerBean> listBean) {
// 新数据和原来数据对比,不一致才去刷新 // 新数据和原来数据对比,不一致才去刷新
boolean change = compareData(listBean); boolean change = compareData(listBean);
if (!change) { if (!change) {
notifyDataSetChanged(); notifyDataSetChanged();
// startBanner();
return; return;
} }
if (null != listBean) {
mList.clear(); mList.clear();
addData(listBean); addData(listBean);
notifyDataSetChanged(); //如果第一张是图片 需要开启定时器
if (listBean.get(0).getType() == 0) {
startBanner();
}
autoCurrIndex = 0; autoCurrIndex = 0;
// startBanner();
}
} }
......
...@@ -7,10 +7,6 @@ import android.os.Bundle; ...@@ -7,10 +7,6 @@ import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Message; import android.os.Message;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
...@@ -19,12 +15,17 @@ import android.widget.ImageView; ...@@ -19,12 +15,17 @@ import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.VideoView; import android.widget.VideoView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import com.widget.imagebanner.R; import com.widget.imagebanner.R;
import com.widget.imagebanner.bean.BannerBean; import com.widget.imagebanner.bean.BannerBean;
import com.widget.imagebanner.utils.BannerLogFileUtils; import com.widget.imagebanner.utils.BannerLogFileUtils;
import com.widget.imagebanner.utils.BitmapUtils; import com.widget.imagebanner.utils.BitmapUtils;
import java.io.File; import java.io.File;
...@@ -268,7 +269,7 @@ public class ImageVideoFragment extends Fragment { ...@@ -268,7 +269,7 @@ public class ImageVideoFragment extends Fragment {
private void pausePlayer() { private void pausePlayer() {
if (null != mVideoView) { if (null != mVideoView) {
Log.e(TAG, "pausePlayer 当前进度是" + mVideoView.getCurrentPosition()); Log.e(TAG, "pausePlayer 当前进度是" + mVideoView.getCurrentPosition());
mVideoView.setBackgroundColor(getResources().getColor(android.R.color.white)); mVideoView.setBackgroundColor(Color.WHITE);
playerPaused = true; playerPaused = true;
this.currentPosition = mVideoView.getCurrentPosition(); this.currentPosition = mVideoView.getCurrentPosition();
mVideoView.pause(); mVideoView.pause();
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
<androidx.viewpager2.widget.ViewPager2 <com.widget.imagebanner.view.ViewPager
android:id="@+id/view_pager" android:id="@+id/view_pager"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment