Commit ed47c127 authored by gaodapeng's avatar gaodapeng

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

parent 70319ef7
......@@ -8,25 +8,24 @@ import android.os.Bundle;
import android.os.Handler;
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.LayoutDirection;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
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.utils.BitmapUtils;
import com.widget.imagebanner.view.ImageVideoFragment;
import com.widget.imagebanner.view.ViewPager;
import java.util.ArrayList;
import java.util.List;
......@@ -41,9 +40,9 @@ import java.util.concurrent.ScheduledExecutorService;
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 ViewPager2 mViewPager;
private ViewPager mViewPager;
private List<BannerBean> mList = new ArrayList<>();
private ViewsPagerAdapter mAdapter;
private int autoCurrIndex = 0;//设置当前 第几个图片 被选中
......@@ -72,8 +71,9 @@ public class ImageVideoBanner extends FrameLayout implements ImageVideoFragment.
private void initView(Context context) {
View view = LayoutInflater.from(context).inflate(R.layout.banner_imge_video, this, true);
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.setOnPageChangeListener(this);
//ViewPager手势滑动禁用
mViewPager.setOnTouchListener(new OnTouchListener() {
@Override
......@@ -81,18 +81,15 @@ public class ImageVideoBanner extends FrameLayout implements ImageVideoFragment.
return true;
}
});
mViewPager.registerOnPageChangeCallback(callback);
}
public void setData(List<com.widget.imagebanner.bean.BannerBean> listBean) {
if (listBean != null) {
mAdapter.setData(listBean);
}
public void replaceData(List<BannerBean> listBean) {
mAdapter.replaceData(listBean);
}
public void addData(List<com.widget.imagebanner.bean.BannerBean> listBean) {
public void addData(List<BannerBean> listBean) {
mAdapter.addData(listBean);
}
......@@ -132,6 +129,7 @@ public class ImageVideoBanner extends FrameLayout implements ImageVideoFragment.
mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ARTIST);
Bitmap b = mmr.getFrameAtTime(); // frame at 2 seconds
bannerBean.setLoadingImage(BitmapUtils.getBytes(b));
b.recycle();
mmr.release();
}
return true;
......@@ -149,29 +147,26 @@ public class ImageVideoBanner extends FrameLayout implements ImageVideoFragment.
}
}
private ViewPager2.OnPageChangeCallback callback = new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
}
@Override
public void onPageSelected(int position) {
autoCurrIndex = position;
@Override
public void onPageSelected(int position) {
autoCurrIndex = position;
// Toast.makeText(context,"position="+position,Toast.LENGTH_SHORT).show();
if (mList.get(position).getType() == 1) {
//如果是视频
stopBanner();
}
if (mList.get(position).getType() == 1) {
//如果是视频
stopBanner();
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
};
@Override
public void onPageScrollStateChanged(int state) {
}
//定时轮播图片,需要在主线程里面修改 UI
private Handler mHandler = new Handler() {
......@@ -209,10 +204,8 @@ public class ImageVideoBanner extends FrameLayout implements ImageVideoFragment.
public void startBanner() {
//如果第一页是视频 不用定时器
if (mList.size() > 0) {
if (mList.get(0).getType() == 0) {
startBanner(period);
}
if (mList.get(0).getType() == 0) {
startBanner(period);
}
}
......@@ -252,22 +245,20 @@ public class ImageVideoBanner extends FrameLayout implements ImageVideoFragment.
startBanner(0);
}
private final class ViewsPagerAdapter extends FragmentStateAdapter {
private final class ViewsPagerAdapter extends FragmentStatePagerAdapter {
private ImageVideoFragment fragment;
private FragmentManager fm;
public ViewsPagerAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
super(fragmentManager, lifecycle);
this.fm = fragmentManager;
public ViewsPagerAdapter(FragmentManager fm) {
super(fm);
this.fm = fm;
}
@NonNull
@Override
public Fragment createFragment(int position) {
public Fragment getItem(int position) {
fragment = new ImageVideoFragment();
fragment.setOnVideoCompletionListener(ImageVideoBanner.this);
com.widget.imagebanner.bean.BannerBean bannerBean = mList.get(position);
BannerBean bannerBean = mList.get(position);
Bundle bundle = new Bundle();
bundle.putSerializable("bannerBean", bannerBean);
bundle.putBoolean("loop", getLoop());
......@@ -275,11 +266,6 @@ public class ImageVideoBanner extends FrameLayout implements ImageVideoFragment.
return fragment;
}
@Override
public int getItemCount() {
return mList.size();
}
/**
* 只有1个视频的时候循环播放
*
......@@ -292,17 +278,37 @@ public class ImageVideoBanner extends FrameLayout implements ImageVideoFragment.
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);
if (!change) {
notifyDataSetChanged();
// startBanner();
return;
}
mList.clear();
addData(listBean);
notifyDataSetChanged();
autoCurrIndex = 0;
if (null != listBean) {
mList.clear();
addData(listBean);
//如果第一张是图片 需要开启定时器
if (listBean.get(0).getType() == 0) {
startBanner();
}
autoCurrIndex = 0;
// startBanner();
}
}
......
......@@ -7,10 +7,6 @@ import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
......@@ -19,12 +15,17 @@ import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.VideoView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.bumptech.glide.Glide;
import com.widget.imagebanner.R;
import com.widget.imagebanner.bean.BannerBean;
import com.widget.imagebanner.utils.BannerLogFileUtils;
import com.widget.imagebanner.utils.BitmapUtils;
import java.io.File;
......@@ -268,7 +269,7 @@ public class ImageVideoFragment extends Fragment {
private void pausePlayer() {
if (null != mVideoView) {
Log.e(TAG, "pausePlayer 当前进度是" + mVideoView.getCurrentPosition());
mVideoView.setBackgroundColor(getResources().getColor(android.R.color.white));
mVideoView.setBackgroundColor(Color.WHITE);
playerPaused = true;
this.currentPosition = mVideoView.getCurrentPosition();
mVideoView.pause();
......
......@@ -4,7 +4,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.viewpager2.widget.ViewPager2
<com.widget.imagebanner.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
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