分享按钮换成recyclerview实现

This commit is contained in:
tongchenfei
2020-05-13 16:01:07 +08:00
parent 9fe9e77698
commit 20a2e8f506
8 changed files with 225 additions and 107 deletions

View File

@@ -34,6 +34,7 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation rootProject.ext.dependencies.kotlinstdlibjdk7
implementation rootProject.ext.dependencies.androidxappcompat
implementation rootProject.ext.dependencies.androidxrecyclerview
implementation rootProject.ext.dependencies.androidxccorektx
implementation rootProject.ext.dependencies.androidxconstraintlayout
implementation rootProject.ext.dependencies.arouter

View File

@@ -0,0 +1,52 @@
package com.mogo.module.share.adapter
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.mogo.module.share.R
import com.mogo.module.share.bean.ShareBtn
import com.mogo.module.share.listener.OnShareBtnClickListener
import com.mogo.utils.glide.GlideApp
import com.mogo.utils.logger.Logger
import kotlinx.android.synthetic.main.item_share_btn.view.*
class ShareBtnAdapter(val context: Context): RecyclerView.Adapter<ShareBtnAdapter.ShareViewHolder>() {
private val TAG = "ShareBtnAdapter"
private val btnList = ArrayList<ShareBtn>()
var onShareBtnClickListener:OnShareBtnClickListener? = null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ShareViewHolder {
return ShareViewHolder(LayoutInflater.from(context).inflate(R.layout.item_share_btn, parent, false))
}
override fun getItemCount(): Int {
return btnList.size
}
override fun onBindViewHolder(holder: ShareViewHolder, position: Int) {
holder.bindData(btnList[position])
}
fun setShareBtnList(btns:List<ShareBtn>){
btnList.clear()
btnList.addAll(btns)
Logger.d(TAG, "setShareBtnList: $btnList")
notifyDataSetChanged()
}
inner class ShareViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
init {
itemView.setOnClickListener {
onShareBtnClickListener?.onShareBtnClick(btnList[adapterPosition])
}
}
fun bindData(shareBtn: ShareBtn) = with(itemView) {
tvShareContent.text = shareBtn.content
GlideApp.with(context).load(shareBtn.iconRes).into(ivShareIcon)
}
}
}

View File

@@ -0,0 +1,9 @@
package com.mogo.module.share.bean
/**
* 分享按钮内容封装
*
* @param icon 分享按钮图标资源,暂时用不到,默认为空,以后如果引入服务端配置按钮,图片资源可以用这个参数
* @param iconRes 分享按钮图标本地资源id默认是-1
*/
data class ShareBtn(var type: Int, var icon: String = "", var iconRes: Int = -1, var content: String)

View File

@@ -20,4 +20,14 @@ public class ShareConstants {
public static final int VOICE_CMD_SERVICE_SEEK_HELP = 1;
public static final String VOICE_CMD_PUB_TROUBLE_HELP = "com.zhidao.auxiliaryDriving.pubTroubleHelp";
public static final int TYPE_BLOCK = 1;
public static final int TYPE_TRAFFIC_CHECK = 2;
public static final int TYPE_CLOSURE = 3;
public static final int TYPE_ACCIDENT = 4;
public static final int TYPE_REAL_TIME_TRAFFIC = 5;
public static final int TYPE_SEEK_HELP = 6;
public static final int TYPE_STAGNANT_WATER = 7;
public static final int TYPE_ROAD_ICY = 8;
public static final int TYPE_DENSE_FOG = 9;
}

View File

@@ -12,12 +12,17 @@ import android.view.WindowManager;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.alibaba.android.arouter.launcher.ARouter;
import com.mogo.module.common.utils.CarSeries;
import com.mogo.module.share.R;
import com.mogo.module.share.ShareControl;
import com.mogo.module.share.adapter.ShareBtnAdapter;
import com.mogo.module.share.bean.ShareBtn;
import com.mogo.module.share.constant.ShareConstants;
import com.mogo.module.share.listener.OnShareBtnClickListener;
import com.mogo.module.share.manager.ISeekHelpListener;
import com.mogo.module.share.manager.SeekHelpManager;
import com.mogo.service.IMogoServiceApis;
@@ -27,7 +32,11 @@ import com.mogo.service.statusmanager.IMogoStatusManager;
import com.mogo.utils.WindowUtils;
import com.mogo.utils.logger.Logger;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -43,14 +52,23 @@ public class LaucherShareDialog implements View.OnClickListener {
private boolean isShown = false;
private TextView tvBlock;
private TextView tvTrafficCheck;
private TextView tvClosure;
private TextView tvNeedHelp;
// private TextView tvBlock;
// private TextView tvTrafficCheck;
// private TextView tvClosure;
// private TextView tvNeedHelp;
//
// private TextView tvAccident;
// private TextView tvRealTimeTraffic;
// private TextView tvStagnantWater;
// private TextView tvRoadIcy;
// private TextView tvDenseFog;
private Context mContext;
private IMogoAnalytics mAnalytics;
private IMogoStatusManager mStatusManager;
private IMogoServiceApis mApis;
private RecyclerView rvShareBtnContainer;
private ShareBtnAdapter adapter;
private WindowManager windowManager;
@@ -72,55 +90,104 @@ public class LaucherShareDialog implements View.OnClickListener {
dismiss();
}
});
tvBlock = body.findViewById(R.id.tvBlock);
tvTrafficCheck = body.findViewById(R.id.tvTrafficCheck);
tvClosure = body.findViewById(R.id.tvClosure);
tvNeedHelp = body.findViewById(R.id.tvNeedHelp);
rvShareBtnContainer = body.findViewById(R.id.rvShareBtnContainer);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext,RecyclerView.HORIZONTAL,false);
rvShareBtnContainer.setLayoutManager(linearLayoutManager);
adapter = new ShareBtnAdapter(mContext);
rvShareBtnContainer.setAdapter(adapter);
// tvBlock = body.findViewById(R.id.tvBlock);
// tvTrafficCheck = body.findViewById(R.id.tvTrafficCheck);
// tvClosure = body.findViewById(R.id.tvClosure);
// tvNeedHelp = body.findViewById(R.id.tvNeedHelp);
}
private void initListener() {
tvBlock.setOnClickListener(this);
tvNeedHelp.setOnClickListener(this);
tvTrafficCheck.setOnClickListener(this);
tvClosure.setOnClickListener(this);
adapter.setOnShareBtnClickListener(new OnShareBtnClickListener() {
@Override
public void onShareBtnClick(@NotNull ShareBtn shareBtn) {
Logger.d(TAG,"分享按钮点击: "+shareBtn);
// traceData("1");
// if (shareBtn.getType() == ShareConstants.TYPE_SEEK_HELP) {
// // 点击发起求助
// SeekHelpManager.INSTANCE.seekHelp(mContext,seekListener,true);
// }else{
// sendShareReceiver(shareBtn.getType() + "");
// dismiss();
// }
}
});
// tvBlock.setOnClickListener(this);
// tvNeedHelp.setOnClickListener(this);
// tvTrafficCheck.setOnClickListener(this);
// tvClosure.setOnClickListener(this);
}
/**
* 初始化默认的分享按钮
*/
private void initShareBtn(){
List<ShareBtn> btns = new ArrayList<>();
ShareBtn btn = new ShareBtn(ShareConstants.TYPE_BLOCK, "", R.drawable.share_block_up, "上报拥堵");
btns.add(btn);
btn = new ShareBtn(ShareConstants.TYPE_TRAFFIC_CHECK, "", R.drawable.share_traffic_check, "上报交通检查");
btns.add(btn);
btn = new ShareBtn(ShareConstants.TYPE_CLOSURE, "", R.drawable.share_road_closure, "上报封路");
btns.add(btn);
btn = new ShareBtn(ShareConstants.TYPE_ACCIDENT, "", R.drawable.share_seek_help, "上报事故");
btns.add(btn);
btn = new ShareBtn(ShareConstants.TYPE_REAL_TIME_TRAFFIC, "", R.drawable.share_seek_help, "实时路况");
btns.add(btn);
btn = new ShareBtn(ShareConstants.TYPE_SEEK_HELP, "", R.drawable.share_seek_help, "发起求助");
btns.add(btn);
btn = new ShareBtn(ShareConstants.TYPE_STAGNANT_WATER, "", R.drawable.share_seek_help, "道路积水");
btns.add(btn);
btn = new ShareBtn(ShareConstants.TYPE_ROAD_ICY, "", R.drawable.share_seek_help, "道路结冰");
btns.add(btn);
btn = new ShareBtn(ShareConstants.TYPE_DENSE_FOG, "", R.drawable.share_seek_help, "浓雾");
btns.add(btn);
adapter.setShareBtnList(btns);
}
@Override
public void onClick(View view) {
int id = view.getId();
Logger.d(TAG, "onClick: " + id);
if (id == R.id.tvBlock) {
//拥堵
traceTanluData("1");
sendShareReceiver("1");
traceTypeData("1");
dismiss();
} else if (id == R.id.oil_price_layout) {
// 分享油价,入口被屏蔽了
traceData("1");
Intent intent = new Intent();
intent.setData(Uri.parse("freshthing://com.zhidao.fresh.things/shareOilPrice"));
mContext.startActivity(intent);
traceTypeData("2");
dismiss();
} else if (id == R.id.tvTrafficCheck) {
//交通检查
traceData("1");
sendShareReceiver("2");
traceTypeData("3");
dismiss();
} else if (id == R.id.tvClosure) {
//封路
traceData("1");
sendShareReceiver("3");
traceTypeData("4");
dismiss();
} else if (id == R.id.tvNeedHelp) {
// 故障求助
SeekHelpManager.INSTANCE.seekHelp(mContext,seekListener,true);
}
// if (id == R.id.tvBlock) {
// //拥堵
// traceTanluData("1");
// sendShareReceiver("1");
// traceTypeData("1");
// dismiss();
// } else if (id == R.id.oil_price_layout) {
// // 分享油价,入口被屏蔽了
// traceData("1");
// Intent intent = new Intent();
// intent.setData(Uri.parse("freshthing://com.zhidao.fresh.things/shareOilPrice"));
// mContext.startActivity(intent);
// traceTypeData("2");
// dismiss();
// } else if (id == R.id.tvTrafficCheck) {
// //交通检查
// traceData("1");
// sendShareReceiver("2");
// traceTypeData("3");
// dismiss();
// } else if (id == R.id.tvClosure) {
// //封路
// traceData("1");
// sendShareReceiver("3");
// traceTypeData("4");
// dismiss();
// } else if (id == R.id.tvNeedHelp) {
// // 故障求助
// SeekHelpManager.INSTANCE.seekHelp(mContext,seekListener,true);
// }
}
@@ -214,6 +281,7 @@ public class LaucherShareDialog implements View.OnClickListener {
layoutParams.y = 0;
initView();
initListener();
initShareBtn();
windowManager.addView(body, layoutParams);
isShown = true;
}

View File

@@ -0,0 +1,7 @@
package com.mogo.module.share.listener
import com.mogo.module.share.bean.ShareBtn
interface OnShareBtnClickListener {
fun onShareBtnClick(shareBtn: ShareBtn)
}

View File

@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="@dimen/dp_160"
android:layout_height="@dimen/dp_160"
android:id="@+id/ivShareIcon"
android:src="@drawable/share_road_closure" />
<Space
android:layout_width="wrap_content"
android:layout_height="@dimen/share_module_tv_margin_top" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvShareContent"
android:text="上报拥堵"
android:textColor="@color/white"
android:textSize="@dimen/share_module_item"
android:textStyle="bold" />
</LinearLayout>

View File

@@ -31,73 +31,16 @@
app:layout_constraintStart_toStartOf="@+id/vBg"
app:layout_constraintTop_toTopOf="@+id/vBg" />
<TextView
android:id="@+id/tvBlock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/share_block_up"
android:drawablePadding="@dimen/share_module_tv_margin_top"
android:gravity="center"
android:text="上报拥堵"
app:layout_constraintHorizontal_chainStyle="spread_inside"
android:layout_marginStart="@dimen/dp_76"
android:textColor="@color/white"
android:textSize="@dimen/share_module_item"
android:textStyle="bold"
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvShareBtnContainer"
android:layout_marginTop="@dimen/dp_64"
app:layout_constraintLeft_toLeftOf="@+id/vBg"
app:layout_constraintRight_toLeftOf="@+id/tvTrafficCheck"
app:layout_constraintTop_toBottomOf="@+id/btn_share_title" />
<TextView
android:id="@+id/tvTrafficCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/share_traffic_check"
android:drawablePadding="@dimen/share_module_tv_margin_top"
android:gravity="center"
android:text="上报交通检查"
android:textColor="@color/white"
android:textSize="@dimen/share_module_item"
android:textStyle="bold"
app:layout_constraintLeft_toRightOf="@+id/tvBlock"
app:layout_constraintRight_toLeftOf="@+id/tvClosure"
android:layout_width="0dp"
android:layout_height="@dimen/dp_235"
app:layout_constraintRight_toRightOf="@+id/vBg"
app:layout_constraintTop_toTopOf="@+id/tvBlock" />
<TextView
android:id="@+id/tvClosure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/share_road_closure"
android:drawablePadding="@dimen/share_module_tv_margin_top"
android:gravity="center"
android:text="上报封路"
android:textColor="@color/white"
android:textSize="@dimen/share_module_item"
android:textStyle="bold"
app:layout_constraintLeft_toRightOf="@+id/tvTrafficCheck"
app:layout_constraintRight_toLeftOf="@+id/tvNeedHelp"
app:layout_constraintRight_toRightOf="@+id/vBg"
app:layout_constraintTop_toTopOf="@+id/tvTrafficCheck" />
<TextView
android:id="@+id/tvNeedHelp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/share_seek_help"
android:drawablePadding="@dimen/share_module_tv_margin_top"
android:gravity="center"
android:text="发起求助"
android:textColor="@color/white"
android:layout_marginEnd="@dimen/dp_76"
android:textSize="@dimen/share_module_item"
android:textStyle="bold"
app:layout_constraintLeft_toRightOf="@+id/tvClosure"
app:layout_constraintRight_toRightOf="@+id/vBg"
app:layout_constraintTop_toTopOf="@+id/tvClosure" />
app:layout_constraintTop_toBottomOf="@+id/btn_share_title"
app:layout_constraintLeft_toLeftOf="@+id/vBg"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
@@ -108,5 +51,5 @@
android:layout_marginTop="@dimen/dp_55"
app:layout_constraintEnd_toEndOf="@+id/vBg"
app:layout_constraintStart_toStartOf="@+id/vBg"
app:layout_constraintTop_toBottomOf="@+id/tvBlock" />
app:layout_constraintTop_toBottomOf="@+id/rvShareBtnContainer" />
</androidx.constraintlayout.widget.ConstraintLayout>