一、添加依赖
开源框架xrichtext 只支持图文混编,我们在之上进行扩展。另采用 jiaozivideoplayer 作为播放器。
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.sendtion:XRichText:1.9.1'
implementation 'cn.jzvd:jiaozivideoplayer:7.0.4'
}
二、扩展实现
我们通过修改两个主要类来实现功能:RichTextEditor RichTextView。
由于许多重要方法都设为private,通过继承无法直接使用,所有直接复制类的所有内容,到一个新的类中,‘
并加上我们的方法。
RichTextView
public RelativeLayout createJzvdStd(String videoPath, String videoTitle, String thumbImageUrl) { RelativeLayout relativeLayout = (RelativeLayout) inflater.inflate(R.layout.jzvd_std, null); JzvdStd jzvdStd = relativeLayout.findViewById(R.id.jzvStd); jzvdStd.setTag(viewTagIndex++); jzvdStd.setUp(videoPath, videoTitle); ImageView thumbImageView = jzvdStd.thumbImageView; if (!TextUtils.isEmpty(thumbImageUrl)) VolleyUtil.getInstance().sendImageLoader(thumbImageUrl, thumbImageView); return relativeLayout; } public void addJzvdStdAtIndex(final int index, String videoPath, String videoTitle, String thumbImageUrl) { try { RelativeLayout jzvdStd = createJzvdStd(videoPath, videoTitle, thumbImageUrl); ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams( LayoutParams.MATCH_PARENT, 500); jzvdStd.setLayoutParams(lp); allLayout.addView(jzvdStd, index); } catch (Exception e) { e.printStackTrace(); } }
RichTextEditor
public RelativeLayout createJzvdStd(String videoPath, String videoTitle, String thumbImageUrl) { RelativeLayout relativeLayout = (RelativeLayout) inflater.inflate(R.layout.jzvd_std, null); JzvdStd jzvdStd = relativeLayout.findViewById(R.id.jzvStd); relativeLayout.setTag(viewTagIndex++); jzvdStd.setUp(videoPath, videoTitle); ImageView thumbImageView = jzvdStd.thumbImageView; if (!TextUtils.isEmpty(thumbImageUrl)) VolleyUtil.getInstance().sendImageLoader(thumbImageUrl, thumbImageView); return relativeLayout; } public void insertJzvdStd(String videoPath, String videoTitle, String thumbImageUrl) { if (TextUtils.isEmpty(videoPath)) { return; } try { //lastFocusEdit获取焦点的EditText String lastEditStr = lastFocusEdit.getText().toString(); int cursorIndex = lastFocusEdit.getSelectionStart();//获取光标所在位置 String editStr1 =