android仿qq微信的消息数量显示功能

本文介绍了如何在Android中仿照QQ和微信实现消息数量显示功能。通过使用帧布局嵌套帧布局,结合动态控制数量的显示和不同数量位数的背景显示,详细展示了代码实现逻辑,包括设置TextView和ImageView来展示消息计数,并根据数量调整图片资源。
下面是显示数量布局的代码。其实很简单,就是帧布局嵌套帧布局就好了。
<LinearLayout
    android:id="@+id/ft_fragment_home_accept_layout"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingBottom="15dp"
    >
    <FrameLayout
        android:layout_width="48dp"
        android:layout_height="58dp"
        android:layout_marginTop="10dp">

    <ImageView

        android:layout_width="50dp"
        android:layout_height="50dp"
        android:scaleType="center"
        android:src="@mipmap/home_dd"
        android:layout_gravity="center"
        />
        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right">
            <ImageView
                android:id="@+id/ft_fragment_home_accept_number_img"

                android:layout_gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scaleType="center"
                android:src="@mipmap/round_1"

                android:visibility="gone"/>
            <TextView
                android:id="@+id/ft_fragment_home_accept_number"
                style="@style/base_text_white_12"
                android:textSize="10sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                android:layout_gravity="center"
                />
        </FrameLayout>

    </FrameLayout>
    <TextView
        style="@style/base_text_grey_light_15"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="17dp"
        android:text="@string/receive2" />
</LinearLayout>
下面是实现逻辑,很简单,就是动态的去控制数量的显示,以及数量是个位数和两位数,三位数不同的背景显示。
void getTotal(final boolean showLoad) {
    Map<String, Object> argMap = HttpUtil.getEncryMap(getActivity());
    if (SharedUtil.getCustomcode(getActivity()) != null) {
        argMap.put("customcode", SharedUtil.getCustomcode(getActivity()));
    }
    ShowLoadTask task = new ShowLoadTask(getActivity(), taskTag, containLayout, loadLayout, "", showLoad, iOnTryClickListener, BaseConstant.getHomepageStatistics, argMap, TaskConstant.POST);
    task.setParseClass(HomeNumberResult.class);
    task.setiOnResultListener(new IOnResultListener() {
                                  @Override
                                  public void onOK(DispatchTask task) {
                                      if (task.resultEntity instanceof HomeNumberResult) {
                                          HomeNumberResult res = (HomeNumberResult) task.resultEntity;
                                          numDataEntity = res.data;
                                          if (numDataEntity == null) {
                                              acceptNumber.setText(""+0);
                                              workorderNumber.setText(""+0);
                                              acceptNumberImg.setVisibility(View.GONE);
                                              acceptNumber.setVisibility(View.GONE);
                                              workorderNumberImg.setVisibility(View.GONE);
                                              workorderNumber.setVisibility(View.GONE);
                                          } else {
                                              orderNumber = numDataEntity.waitReceiveCount;
                                              worknumber = numDataEntity.repairOrderCount + numDataEntity.helpOrderCount;
                                              acceptNumber.setText("" + numDataEntity.waitReceiveCount);
                                              workorderNumber.setText("" + (worknumber));
                                              acceptNumberImg.setVisibility(View.VISIBLE);
                                              acceptNumber.setVisibility(View.VISIBLE);
                                              workorderNumberImg.setVisibility(View.VISIBLE);
                                              workorderNumber.setVisibility(View.VISIBLE);

                                              if (numDataEntity.waitReceiveCount ==0){
                                                  acceptNumberImg.setVisibility(View.GONE);
                                                  acceptNumber.setVisibility(View.GONE);
                                              }else {
                                                  acceptNumberImg.setVisibility(View.VISIBLE);
                                                  acceptNumber.setVisibility(View.VISIBLE);
                                              }
                                              if (worknumber ==0){
                                                  workorderNumberImg.setVisibility(View.GONE);
                                                  workorderNumber.setVisibility(View.GONE);
                                              }else {
                                                  workorderNumberImg.setVisibility(View.VISIBLE);
                                                  workorderNumber.setVisibility(View.VISIBLE);
                                              }
                                              if (numDataEntity.waitReceiveCount < 10) {
                                                  acceptNumberImg.setImageDrawable(getResources().getDrawable(R.mipmap.round_1));
                                              } else if (numDataEntity.waitReceiveCount > 10&&numDataEntity.waitReceiveCount<100) {
                                                  acceptNumberImg.setImageDrawable(getResources().getDrawable(R.mipmap.round_2));
                                              } else if (numDataEntity.waitReceiveCount > 100) {
                                                  acceptNumberImg.setImageDrawable(getResources().getDrawable(R.mipmap.round_3));
                                              }
                                              if (worknumber < 10) {
                                                  workorderNumberImg.setImageDrawable(getResources().getDrawable(R.mipmap.round_1));
                                              } else if (worknumber > 10&&worknumber<100) {
                                                  workorderNumberImg.setImageDrawable(getResources().getDrawable(R.mipmap.round_2));
                                              } else if (worknumber > 100) {
                                                  workorderNumberImg.setImageDrawable(getResources().getDrawable(R.mipmap.round_3));
                                              }
                                          }


                                      }

                                  }

                                  @Override
                                  public void onError(DispatchTask task) {
                                        LogUtil.e("请求失败");
                                  }

                                  @Override
                                  public void onDone(DispatchTask task) {
                                      try {
                                      } catch (Exception e) {
                                          e.printStackTrace();
                                      }
                                  }
                              }

    );
    task.execute();
}
当然如果在其他页面有其他操作导致数量改变,发送个广播,在接收广播里做下逻辑处理就好了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值