Android仿微信朋友圈全文、收起功能的实例代码

Android仿微信朋友圈全文、收起功能的实例代码,第1张

概述前言一般在社交APP中都有类似朋友圈的功能,其中发表的动态内容很长的时候不可能让它全部显示。这里就需要做一个仿微信朋友圈全文、收起功能来解决该问题。在网上看到一个例子-->https://www.oudahe.com/p/24642

前言

一般在社交APP中都有类似朋友圈的功能,其中发表的动态内容很长的时候不可能让它全部显示。这里就需要做一个仿微信朋友圈全文、收起功能来解决该问题。在网上看到一个例子-->https://www.oudahe.com/p/24642/ ,写的很不错,但是有个BUG,他这个Demo只有在条目固定的时候才正常,当增加、删除条目的时候会出现全文、收起显示混乱的问题。原因是他使用了固定的position作为key来保存当前显示的状态。这篇文章在他的基础上进行优化。

效果图

具体代码

(详细解释在代码注释中都有,这里就省略了)

MainActivity.java

package com.wildma.wildmaexpandfoldtext;import androID.os.Bundle;import androID.support.v7.app.AppCompatActivity;import androID.support.v7.Widget.linearlayoutmanager;import androID.support.v7.Widget.RecyclerVIEw;import java.util.ArrayList;import java.util.List;/** * Author   wildma * DATE    2017/8/3 * Des    ${Todo} */public class MainActivity extends AppCompatActivity {  private RecyclerVIEw mRecyclerVIEw;  List<ExpandFoldTextBean> mList = new ArrayList<>();  @OverrIDe  protected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main);    initData();    ExpandFoldTextAdapter adapter = new ExpandFoldTextAdapter(mList,this);    mRecyclerVIEw = (RecyclerVIEw) findVIEwByID(R.ID.recyclervIEw);    mRecyclerVIEw.setLayoutManager(new linearlayoutmanager(this,linearlayoutmanager.VERTICAL,false));    mRecyclerVIEw.setAdapter(adapter);  }  /**   * 初始化数据   */  private voID initData() {    String longContent = "-->游泳、快走、慢跑、骑自行车,及一切有氧运动都能锻炼心脏。有氧运动好处多:能锻炼心肺、增强循环系统功能、燃烧脂肪、加大肺活量、降低血压,甚至能预防糖尿病,减少心脏病的发生。美国运动医学院建议,想知道有氧运动强度是否合适,可在运动后测试心率,以达到最高心率的60%―90%为宜。如果想通过有氧运动来减肥,可以选择低度到中度的运动强度,同时延长运动时间,这种方法消耗的热量更多。运动频率每周3―5次,每次20―60分钟。想要锻炼肌肉,可以练举重、做体 *** 以及其他重复伸、屈肌肉的运动。肌肉锻炼可以燃烧热量、增强骨密度、减少受伤,尤其是关节受伤的几率,还能预防骨质疏松。 在做举重运动前,先测一下,如果连续举8次你最多能举多重的东西,就从这个重量开始练习。当你可以连续12次举起这个重量时,试试增加5%的重量。注意每次练习时,要连续举8―12次,这样可以达到肌肉最大耐力的70%―80%,锻炼效果较好。每周2―3次,但要避免连续两天锻炼同一组肌肉群, 以便让肌肉有充分的恢复时间。";    String shortContent = "-->健身是一种体育项目,如各种徒手健美 *** 、韵律 *** 、形体 *** 以及各种自抗力动作。";    for (int i = 0; i < 20; i++) {      ExpandFoldTextBean bean = new ExpandFoldTextBean();      if (i % 2 == 0) {        bean.setContent(i + shortContent);        bean.setID(i);      } else {        bean.setContent(i + longContent);        bean.setID(i);      }      mList.add(bean);    }  }}

ExpandFoldTextAdapter.java

package com.wildma.wildmaexpandfoldtext;import androID.app.Activity;import androID.support.v7.Widget.RecyclerVIEw;import androID.util.SparseArray;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.vIEw.VIEwTreeObserver;import androID.Widget.TextVIEw;import java.util.List;/** * Author   wildma * DATE    2017/8/3 * Des    ${展开折叠文本适配器} */public class ExpandFoldTextAdapter extends RecyclerVIEw.Adapter<ExpandFoldTextAdapter.MyVIEwHolder> {  private Activity mContent;  private final int MAX_liNE_COUNT = 3;//最大显示行数  private final int STATE_UNKNow = -1;//未知状态  private final int STATE_NOT_OVERFLOW = 1;//文本行数小于最大可显示行数  private final int STATE_ColLAPSED = 2;//折叠状态  private final int STATE_EXPANDED = 3;//展开状态  /**   * 注意:保存文本状态集合的key一定要是唯一的,如果用position。   * 如果使用position作为key,则删除、增加条目的时候会出现显示错乱   */  private SparseArray<Integer> mTextStateList;//保存文本状态集合  List<ExpandFoldTextBean> mList;  public ExpandFoldTextAdapter(List<ExpandFoldTextBean> List,Activity context) {    mContent = context;    this.mList = List;    mTextStateList = new SparseArray<>();  }  @OverrIDe  public MyVIEwHolder onCreateVIEwHolder(VIEwGroup parent,int vIEwType) {    return new MyVIEwHolder(mContent.getLayoutInflater().inflate(R.layout.item_expand_fold_text,parent,false));  }  @OverrIDe  public voID onBindVIEwHolder(final MyVIEwHolder holder,final int position) {    int state = mTextStateList.get(mList.get(position).getID(),STATE_UNKNow);    //第一次初始化,未知状态    if (state == STATE_UNKNow) {      holder.content.getVIEwTreeObserver().addOnPreDrawListener(new VIEwTreeObserver.OnPreDrawListener() {        @OverrIDe        public boolean onPreDraw() {          //这个回掉会调用多次,获取完行数后记得注销监听          holder.content.getVIEwTreeObserver().removeOnPreDrawListener(this);          //holder.content.getVIEwTreeObserver().addOnPreDrawListener(null);          //如果内容显示的行数大于最大显示行数          if (holder.content.getlineCount() > MAX_liNE_COUNT) {            holder.content.setMaxlines(MAX_liNE_COUNT);//设置最大显示行数            holder.expandOrFold.setVisibility(VIEw.VISIBLE);//显示“全文”            holder.expandOrFold.setText("全文");            mTextStateList.put(mList.get(position).getID(),STATE_ColLAPSED);//保存状态          } else {            holder.expandOrFold.setVisibility(VIEw.GONE);            mTextStateList.put(mList.get(position).getID(),STATE_NOT_OVERFLOW);          }          return true;        }      });      holder.content.setMaxlines(Integer.MAX_VALUE);//设置文本的最大行数,为整数的最大数值      holder.content.setText(mList.get(position).getContent());    } else {      //如果之前已经初始化过了,则使用保存的状态。      switch (state) {        case STATE_NOT_OVERFLOW:          holder.expandOrFold.setVisibility(VIEw.GONE);          break;        case STATE_ColLAPSED:          holder.content.setMaxlines(MAX_liNE_COUNT);          holder.expandOrFold.setVisibility(VIEw.VISIBLE);          holder.expandOrFold.setText("全文");          break;        case STATE_EXPANDED:          holder.content.setMaxlines(Integer.MAX_VALUE);          holder.expandOrFold.setVisibility(VIEw.VISIBLE);          holder.expandOrFold.setText("收起");          break;      }      holder.content.setText(mList.get(position).getContent());    }    //全文和收起的点击事件    holder.expandOrFold.setonClickListener(new VIEw.OnClickListener() {      @OverrIDe      public voID onClick(VIEw v) {        int state = mTextStateList.get(mList.get(position).getID(),STATE_UNKNow);        if (state == STATE_ColLAPSED) {          holder.content.setMaxlines(Integer.MAX_VALUE);          holder.expandOrFold.setText("收起");          mTextStateList.put(mList.get(position).getID(),STATE_EXPANDED);        } else if (state == STATE_EXPANDED) {          holder.content.setMaxlines(MAX_liNE_COUNT);          holder.expandOrFold.setText("全文");          mTextStateList.put(mList.get(position).getID(),STATE_ColLAPSED);        }      }    });    //删除点击事件    holder.delete.setonClickListener(new VIEw.OnClickListener() {      @OverrIDe      public voID onClick(VIEw vIEw) {        mList.remove(position);        notifyDataSetChanged();      }    });  }  @OverrIDe  public int getItemCount() {    return mList.size();  }  public class MyVIEwHolder extends RecyclerVIEw.VIEwHolder {    public TextVIEw nickname;    public TextVIEw content;    public TextVIEw delete;    public TextVIEw expandOrFold;    public MyVIEwHolder(VIEw itemVIEw) {      super(itemVIEw);      nickname = (TextVIEw) itemVIEw.findVIEwByID(R.ID.tv_nickname);      content = (TextVIEw) itemVIEw.findVIEwByID(R.ID.tv_content);      delete = (TextVIEw) itemVIEw.findVIEwByID(R.ID.tv_delete);      expandOrFold = (TextVIEw) itemVIEw.findVIEwByID(R.ID.tv_expand_or_fold);    }  }}

ExpandFoldTextBean.java

package com.wildma.wildmaexpandfoldtext;/** * Author   wildma * DATE    2017/8/3 * Des    ${Todo} */public class ExpandFoldTextBean {  private String content;//内容  private int ID;//该条数据的ID  public String getContent() {    return content;  }  public voID setContent(String content) {    this.content = content;  }  public int getID() {    return ID;  }  public voID setID(int ID) {    this.ID = ID;  }}

activity_main.xml

<?xml version="1.0" enCoding="utf-8"?><relativeLayout  androID:ID="@+ID/activity_main"  xmlns:androID="http://schemas.androID.com/apk/res/androID"  androID:layout_wIDth="match_parent"  androID:layout_height="match_parent"  androID:paddingBottom="@dimen/activity_vertical_margin"  androID:paddingleft="@dimen/activity_horizontal_margin"  androID:paddingRight="@dimen/activity_horizontal_margin"  androID:paddingtop="@dimen/activity_vertical_margin">  <androID.support.v7.Widget.RecyclerVIEw    androID:ID="@+ID/recyclervIEw"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent">  </androID.support.v7.Widget.RecyclerVIEw></relativeLayout>

item_expand_fold_text.xml

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"       androID:layout_wIDth="match_parent"       androID:layout_height="wrap_content"       androID:orIEntation="vertical"       androID:paddingBottom="@dimen/activity_vertical_margin"       androID:paddingleft="@dimen/activity_horizontal_margin"       androID:paddingRight="@dimen/activity_horizontal_margin"       androID:paddingtop="@dimen/activity_vertical_margin">  <linearLayout    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:layout_gravity="center_vertical"    androID:orIEntation="horizontal">    <ImageVIEw      androID:layout_wIDth="40dp"      androID:layout_height="40dp"      androID:layout_marginRight="16dp"      androID:gravity="center"      androID:scaleType="centerCrop"      androID:src="@mipmap/ic_launcher"/>    <relativeLayout      androID:layout_wIDth="match_parent"      androID:layout_height="wrap_content">      <TextVIEw        androID:ID="@+ID/tv_nickname"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:text="wildma"        androID:textcolor="@androID:color/black"        androID:textSize="14sp"/>      <TextVIEw        androID:ID="@+ID/tv_delete"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_alignParentRight="true"        androID:layout_marginleft="12dp"        androID:paddingleft="5dp"        androID:paddingRight="5dp"        androID:text="删除"        androID:textcolor="@androID:color/holo_red_dark"        androID:textSize="14sp"/>    </relativeLayout>  </linearLayout>  <linearLayout    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:layout_marginleft="56dp"    androID:orIEntation="vertical"    androID:paddingBottom="8dp">    <TextVIEw      androID:ID="@+ID/tv_content"      androID:layout_wIDth="wrap_content"      androID:layout_height="wrap_content"      androID:layout_marginBottom="8dp"      androID:Alpha="0.85"      androID:ellipsize="end"      androID:text="内容"      androID:textcolor="@androID:color/black"      androID:textSize="14sp"/>    <TextVIEw      androID:ID="@+ID/tv_expand_or_fold"      androID:layout_wIDth="wrap_content"      androID:layout_height="wrap_content"      androID:text="全文"      androID:textcolor="@color/colorPrimaryDark"      androID:textSize="14sp"/>  </linearLayout>  <VIEw    androID:layout_wIDth="match_parent"    androID:layout_height="0.5dp"    androID:background="@androID:color/black"/></linearLayout>

源码地址:https://github.com/wildma/WildmaExpandFoldText

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android仿微信朋友圈全文、收起功能的实例代码全部内容,希望文章能够帮你解决Android仿微信朋友圈全文、收起功能的实例代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1144944.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存