直播软件开发是当下流行的视频社交类软件开发类型的一种,直播平台也是年轻人喜爱的网络平台之一,许多直播平台为了增加用户粘性,推出了任务奖励的功能,这是怎么实现的呢?
接下来以云豹直播系统源码为例,为大家展示如何在直播软件中开发完善的任务奖励机制。
如上图所示,云豹直播系统源码在直播间和个人主页分别添加了两处“每日任务”入口,点击进入后,可以看到每日任务的完成情况和完成条件,用户可以有针对性的进行相应的活动,对于已完成的任务可以领取相应的奖励,所有任务每隔24小时重新刷新。
部分关于实现任务奖励的直播系统源码如下:
public class DailyTaskActivity extends AbsActivity {
@Override
protected int getLayoutId() {
return R.layout.activity_daily_task;
}
@Override
protected void main() {
setTitle(WordUtil.getString(R.string.daily_task));
LiveHttpUtil.getDailyTask("", 0, new HttpCallback() {
@Override
public void onSuccess(int code, String msg, String[] info) {
if (code == 0 && info.length > 0) {
JSONObject obj = JSON.parseObject(info[0]);
TextView tip = findViewById(R.id.tip);
tip.setText(obj.getString("tip_m"));
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
List<DailyTaskBean> list = JSON.parseArray(obj.getString("list"), DailyTaskBean.class);
recyclerView.setAdapter(new DailyTaskAdapter(mContext, list));
}
}
});
}
@Override
protected void onDestroy() {
LiveHttpUtil.cancel(LiveHttpConsts.GET_DAILY_TASK);
LiveHttpUtil.cancel(LiveHttpConsts.DAILY_TASK_REWARD);
super.onDestroy();
}
}
public class DailyTaskDialogFragment extends AbsDialogFragment {
private String mLiveUid;
@Override
protected int getLayoutId() {
return R.layout.dialog_daily_task;
}
@Override
protected int getDialogStyle() {
return R.style.dialog;
}
@Override
protected boolean canCancel() {
return true;
}
@Override
protected void setWindowAttributes(Window window) {
WindowManager.LayoutParams params = window.getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = WindowManager.LayoutParams.MATCH_PARENT;
params.horizontalMargin = 0.1f;
params.verticalMargin = 0.1f;
window.setAttributes(params);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
findViewById(R.id.btn_close).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
LiveHttpUtil.getDailyTask(mLiveUid, 1, new HttpCallback() {
@Override
public void onSuccess(int code, String msg, String[] info) {
if (code == 0 && info.length > 0) {
JSONObject obj = JSON.parseObject(info[0]);
TextView tip = findViewById(R.id.tip);
tip.setText(obj.getString("tip_m"));
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
List<DailyTaskBean> list = JSON.parseArray(obj.getString("list"), DailyTaskBean.class);
recyclerView.setAdapter(new DailyTaskAdapter(mContext, list));
}
}
});
}
```c
```c
public void setLiveUid(String liveUid) {
mLiveUid = liveUid;
}
@Override
public void onDestroy() {
LiveHttpUtil.cancel(LiveHttpConsts.GET_DAILY_TASK);
LiveHttpUtil.cancel(LiveHttpConsts.DAILY_TASK_REWARD);
super.onDestroy();
}
}
public class DailyTaskAdapter extends RefreshAdapter<DailyTaskBean> {
private View.OnClickListener mOnClickListener;
private String mStatus0;
private String mStatus1;
private String mStatus2;
private int mColor0;
private int mColor1;
private Drawable mDrawable0;
private Drawable mDrawable1;
public DailyTaskAdapter(Context context, List<DailyTaskBean> list) {
super(context, list);
mOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = (int) v.getTag();
DailyTaskBean bean = mList.get(position);
if (bean.getStatus() == 1) {//可领取
final int finalPosition = position;
final DailyTaskBean finalBean = bean;
LiveHttpUtil.dailyTaskReward(bean.getId(), new HttpCallback() {
@Override
public void onSuccess(int code, String msg, String[] info) {
if (code == 0) {
finalBean.setStatus(2);
notifyItemChanged(finalPosition, Constants.PAYLOAD);
}
ToastUtil.show(msg);
}
});
}
}
};
mStatus0 = WordUtil.getString(R.string.daily_task_0);
mStatus1 = WordUtil.getString(R.string.daily_task_1);
mStatus2 = WordUtil.getString(R.string.daily_task_2);
mColor0 = ContextCompat.getColor(mContext, R.color.gray3);
mColor1 = ContextCompat.getColor(mContext, R.color.white);
mDrawable0 = ContextCompat.getDrawable(mContext, R.drawable.bg_daily_task_03);
mDrawable1 = ContextCompat.getDrawable(mContext, R.drawable.bg_daily_task_04);
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
return new Vh(mInflater.inflate(R.layout.item_daily_task, viewGroup, false));
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder vh, int i) {
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder vh, int i, @NonNull List payloads) {
Object payload = payloads.size() > 0 ? payloads.get(0) : null;
((Vh) vh).setData(mList.get(i), i, payload);
}
class Vh extends RecyclerView.ViewHolder {
TextView mTitle;
TextView mTip;
TextView mStatus;
View mLine;
public Vh(@NonNull View itemView) {
super(itemView);
mTitle = itemView.findViewById(R.id.title);
mTip = itemView.findViewById(R.id.tip);
mStatus = itemView.findViewById(R.id.btn_status);
mLine = itemView.findViewById(R.id.line);
mStatus.setOnClickListener(mOnClickListener);
}
void setData(DailyTaskBean bean, int position, Object payload) {
if (payload == null) {
mTitle.setText(bean.getTitle());
mTip.setText(bean.getTip());
mStatus.setTag(position);
if (position == mList.size() - 1) {
if (mLine.getVisibility() == View.VISIBLE) {
mLine.setVisibility(View.INVISIBLE);
}
} else {
if (mLine.getVisibility() != View.VISIBLE) {
mLine.setVisibility(View.VISIBLE);
}
}
}
int status = bean.getStatus();
if (status == 0) {
mStatus.setText(mStatus0);
mStatus.setBackground(null);
mStatus.setTextColor(mColor0);
} else {
if (status == 1) {
mStatus.setText(mStatus1);
mStatus.setBackground(mDrawable1);
} else {
mStatus.setText(mStatus2);
mStatus.setBackground(mDrawable0);
}
mStatus.setTextColor(mColor1);
}
}
}
}
这样就在直播系统源码里实现了每日任务功能,现在国内直播软件开发行业人才辈出,总有很多有趣的功能不断放出,小云豹在日后也会逐渐放出一些直播源码和大家共赏,敬请期待吧。