Android简易音乐重构MVVM Java版-新增轮播图网页链接跳转展示(十五)

Posted 雪の星空朝酱

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android简易音乐重构MVVM Java版-新增轮播图网页链接跳转展示(十五)相关的知识,希望对你有一定的参考价值。

android简易音乐重构MVVM Java版-新增轮播图网页链接跳转展示(十五)

关于

  本篇主要讲述轮播图跳转web网页及个人新版webviewLib使用。

效果图

新增WebActivity页面

  添加webview引用:

implementation 'com.github.Tobeyr1:webLoading:1.0.1'

  该lib使用详情参考《WebLoading开源库,支持https访问》
  添加activity_web.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.WebActivity">

    <include layout="@layout/ui_common_title"
        android:id="@+id/view_title"/>
    <com.tobery.weblib.WebViewLoading
        android:id="@+id/web"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_0"
        app:layout_constraintTop_toBottomOf="@id/view_title"
         />


</androidx.constraintlayout.widget.ConstraintLayout>

修改WebActivity

  修改Constant.java

public static final String BANNER_URI = "BANNER_URI";
public class WebActivity extends BaseActivity 

    private ActivityWebBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        binding = ActivityWebBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initView();
    

    private void initView() 
        binding.web.setSettings(this);
        String url = getIntent().getStringExtra(BANNER_URI);
        binding.web.getTitle(title ->
            binding.viewTitle.tvTitle.setText(title);
            binding.viewTitle.tvTitle.setVisibility(View.VISIBLE);
            return null;
        );
        binding.web.setUrl(url);
        binding.viewTitle.ivBack.setOnClickListener(view -> 
            if (ClickUtil.enableClick())
                finish();
            
        );
    

    @Override
    protected void onPause() 
        super.onPause();
        binding.web.setPause();
    

    @Override
    protected void onResume() 
        super.onResume();
        binding.web.setResume();
    

    @Override
    protected void onDestroy() 
        super.onDestroy();
        binding.web.setDestroy();
    

修改DiscoverFragment

private void initData(List<BannerExtInfoEntity.BannersEntity> banners) 
        binding.bannerImg.setAdapter(new bannerAdapter(banners))
                .addBannerLifecycleObserver(getViewLifecycleOwner())
                .setIntercept(false) //不拦截事件
                .setBannerRound(10f)//圆角
                .setIndicator(new RectangleIndicator(getContext())) //线条指示器
                .setIndicatorHeight(5)
                .setIndicatorWidth(6,6)//选中下宽度一致
                .setIndicatorGravity(IndicatorConfig.Direction.CENTER)
                .setOnBannerListener(new OnBannerListener() 
                    @Override
                    public void OnBannerClick(Object data, int position) 
                        if (banners.get(position).getUrl() != null)
                            startActivity(new Intent(getActivity(), WebActivity.class)
                                    .putExtra(BANNER_URI,banners.get(position).getUrl()));
                        
                    
                );
    

新增BindingAdapter

  新增BindingAdapter用来databinding中页面的自定义加载:

public class BindingAdapter 

    //加载圆形图片
    @androidx.databinding.BindingAdapter("imSrc", "error")
    public static void loadUrl(ImageView view, @Nullable String url, Drawable error) 
        if (url != null) 
            RequestOptions options = new RequestOptions()
                    .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                    .circleCrop();
            Glide.with(view.getContext())
                    .load(url)
                    .transition(DrawableTransitionOptions.withCrossFade())
                    .error(error)
                    .apply(options)
                    .into(view);
        
    
    //加载圆角图片
    @androidx.databinding.BindingAdapter(value = "rectangleSrc")
    public static void loadRadiusImage(ImageView view,@Nullable String url)
        RequestOptions options = new RequestOptions()
                .placeholder(R.drawable.ic_banner_loading)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .transform(new CenterCrop(),new RoundedCorners(10))
                .error(R.mipmap.ic_launcher);
        Glide.with(view.getContext())
                .load(url)
                .transition(DrawableTransitionOptions.withCrossFade())
                .apply(options)
                .into(view);
    

    @androidx.databinding.BindingAdapter(value = "onsingleclick")
    public static void onSingleClick(View view, final Function0  function)
        view.setOnClickListener(v -> 
            if (ClickUtil.enableClick())
                function.invoke();
            
        );
    

修改MgcAdapter与LookAdapter

//使用其替换注释代码
BindingAdapter.loadRadiusImage(holder.imRecommend,bean.getUiElement().getImage().getImageUrl());
/*RequestOptions options = new RequestOptions()
                .placeholder(R.drawable.ic_banner_loading)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .transform(new CenterCrop(),new RoundedCorners(10))
                .error(R.mipmap.ic_launcher);
        Glide.with(mContext)
                .load(bean.getUiElement().getImage().getImageUrl())
                .transition(new DrawableTransitionOptions().crossFade())
                .apply(options)
                .into(holder.imRecommend);*/

下一篇主要介绍封装的MusicPlay播放功能。

以上是关于Android简易音乐重构MVVM Java版-新增轮播图网页链接跳转展示(十五)的主要内容,如果未能解决你的问题,请参考以下文章

Android简易音乐重构MVVM Java版-新增启动动画

Android简易音乐重构MVVM Java版-新增推荐菜单及侧边栏展示

Android简易音乐重构MVVM Java版-BottomNavigationView+viewpager主界面结构

Android简易音乐重构MVVM Java版-新增推荐雷达歌单详情列表界面(十八)

Android简易音乐重构MVVM Java版-新增推荐雷达歌单详情列表界面(十八)

Android简易音乐重构MVVM Java版-新增歌曲播放界面+状态栏黑科技(十七)