Android仿微信朋友圈全文收起功能示例(附源码)

Android仿微信朋友圈全文收起功能示例(附源码),第1张

概述在众多的社交类软件中,朋友圈是必不可少的,可以与好友、同学等分享自己的日常和有意思的事情,在开发社交类App时,朋友圈发表的内容你不可能让他全部显示,全部显示的话用户体验度会非常不好,这时就要用到全文、收

在众多的社交类软件中,朋友圈是必不可少的,可以与好友、同学等分享自己的日常和有意思的事情,在开发社交类App时,朋友圈发表的内容你不可能让他全部显示,全部显示的话用户体验度会非常不好,这时就要用到全文、收缩的功能,朋友如果想要看你发的动态,只要点一下全文就可以查看所有的全部的内容了,如果不想看,也没有必要把这一篇文章全部都滑到底部,才能看下一条内容。

下边将源码贴出来供大家参考:(代码不是最简便的,但是功能是可以的)

首先写一个布局,这个布局是每个子项的布局 item_text_List.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">    <TextVIEw      androID:ID="@+ID/tv_hend"      androID:layout_wIDth="40dp"      androID:layout_height="40dp"      androID:layout_marginRight="16dp"      androID:background="@drawable/circle"      androID:gravity="center"      androID:text="1"      androID:textcolor="@androID:color/white"      androID:textSize="14sp" />    <TextVIEw      androID:ID="@+ID/tv_name"      androID:layout_wIDth="wrap_content"      androID:layout_height="wrap_content"      androID:Alpha="0.87"      androID:text="丁先森"      androID:textcolor="@androID:color/black"      androID:textSize="14sp" />  </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_collapse"      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:layout_marginleft="56dp"    androID:Alpha="0.12"    androID:background="@androID:color/black" /></linearLayout>

写一个Util类,其实是存放的数据,也可以读取数据库,获取JsON字符串,这里为了实现功能就是用固定的数据

Util.class

package com.example.frIEndzhanshou;/** * @author DingChao *     2017/2/10 */public class Util {  private static String[] nameArray = new String[]{      "windows","Mac","linux"  };  private static String[] contentArray = new String[]{      "在动作类影片中,只要发生混乱,那么绝对就有木仓战。现在的技术越来越发达,电影或电视中的特效也做的越来越逼真,演员们被木仓打中的效果也很形象,我们经常能看到被木仓打中的伤口血淋林的暴露在大屏幕中,从演员的表演中我们能看到木仓击是很痛的,那么你们有想过被木仓打中到底会有多痛?什么感觉吗?网站有网友为我们分享被子d打中的感觉\n" +          "1、“老实说,比我想象中的感觉要轻很多。本来我以为很痛,可是被打中后就像是被棒球击中的感觉一样,刚开始的几秒钟没什么知觉,过会才感到痛\n" +          "2、“被子d打到的感觉就像是一直有人拿针扎你一样,刺痛刺痛的。”\n" +          "3、“我当初大腿被木仓击中,子d直接从我的大腿中传过去,连带着我的肌腱也被击中,那种感觉我觉得用疼痛两个字已经不足以形容了\n" +          "4、“在我十七岁的时候,脚被木仓击中,当时我以为是被蜜蜂蛰了,因为仿佛听到了蜜蜂的声音,没过几秒钟,脚上就传来灼热感,这才知道原来是被木仓击中了。\n" +          "5、“我只是听到的木仓声,却没有意识到自己中木仓了。直到血流出来才意识到。所以,对我来讲,被子d击中没什么感觉。","GNOME or KDE desktop\n" +          " processor with support for AMD Virtualization™ (AMD-V™)"  };  /**   * 获取文本内容根据下标   *   * @param position   * @return   */  public static String getContent(int position) {    return contentArray[position % contentArray.length];  }  /**   * 获取名称根据下标   *   * @param position   * @return   */  public static String getname(int position) {    return nameArray[position % contentArray.length];  }}

设置适配器

Textlistadapter.class

package com.example.frIEndzhanshou;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.Adapter;import androID.Widget.TextVIEw;/** * @author DingChao *     2017/2/10 */  public class Textlistadapter extends RecyclerVIEw.Adapter<Textlistadapter.TextHolder> {  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;//文本超过限定行数,被点击全文展开  private SparseArray<Integer> mTextStateList;  public Textlistadapter(Activity context) {    mContent = context;    mTextStateList = new SparseArray<>();  }  @OverrIDe  public TextHolder onCreateVIEwHolder(VIEwGroup parent,int vIEwType) {    return new TextHolder(mContent.getLayoutInflater().inflate(R.layout.item_test_List,parent,false));  }  @OverrIDe  public voID onBindVIEwHolder(final TextHolder holder,final int position) {    holder.hend.setText(position+1+"");//设置头部的文字    holder.name.setText(Util.getname(position));//设置名称    int state=mTextStateList.get(position,STATE_UNKNow);//    如果该itme是第一次初始化,则取获取文本的行数    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.expandOrCollapse.setVisibility(VIEw.VISIBLE);//让其显示全文的文本框状态为显示            holder.expandOrCollapse.setText("全文");//设置其文字为全文            mTextStateList.put(position,STATE_ColLAPSED);          }else{            holder.expandOrCollapse.setVisibility(VIEw.GONE);//显示全文隐藏            mTextStateList.put(position,STATE_NOT_OVERFLOW);//让其不能超过限定的行数          }          return true;        }      });      holder.content.setMaxlines(Integer.MAX_VALUE);//设置文本的最大行数,为整数的最大数值      holder.content.setText(Util.getContent(position));//用Util中的getContent方法获取内容    }else{//      如果之前已经初始化过了,则使用保存的状态,无需在获取一次      switch (state){        case STATE_NOT_OVERFLOW:          holder.expandOrCollapse.setVisibility(VIEw.GONE);          break;        case STATE_ColLAPSED:          holder.content.setMaxlines(MAX_liNE_COUNT);          holder.expandOrCollapse.setVisibility(VIEw.VISIBLE);          holder.expandOrCollapse.setText("全文");          break;        case STATE_EXPANDED:          holder.content.setMaxlines(Integer.MAX_VALUE);          holder.expandOrCollapse.setVisibility(VIEw.VISIBLE);          holder.expandOrCollapse.setText("收起");          break;      }      holder.content.setText(Util.getContent(position));    }//    设置显示和收起的点击事件    holder.expandOrCollapse.setonClickListener(new VIEw.OnClickListener() {      @OverrIDe      public voID onClick(VIEw v) {        int state=mTextStateList.get(position,STATE_UNKNow);        if (state==STATE_ColLAPSED){          holder.content.setMaxlines(Integer.MAX_VALUE);          holder.expandOrCollapse.setText("收起");          mTextStateList.put(position,STATE_EXPANDED);        }else if (state==STATE_EXPANDED){          holder.content.setMaxlines(MAX_liNE_COUNT);          holder.expandOrCollapse.setText("全文");          mTextStateList.put(position,STATE_ColLAPSED);        }      }    });  }  @OverrIDe  public int getItemCount() {    return 15;  }  public class TextHolder extends RecyclerVIEw.VIEwHolder {    public TextVIEw hend;    public TextVIEw name;    public TextVIEw content;    public TextVIEw expandOrCollapse;    public TextHolder(VIEw itemVIEw) {      super(itemVIEw);//      绑定xml布局中的控件      hend = (TextVIEw) itemVIEw.findVIEwByID(R.ID.tv_hend);      name = (TextVIEw) itemVIEw.findVIEwByID(R.ID.tv_name);      content = (TextVIEw) itemVIEw.findVIEwByID(R.ID.tv_content);      expandOrCollapse = (TextVIEw) itemVIEw.findVIEwByID(R.ID.tv_expand_or_collapse);    }  }}

主布局的内容:

<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"  xmlns:tools="http://schemas.androID.com/tools"  androID:ID="@+ID/activity_main"  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"  tools:context="com.example.frIEndzhanshou.MainActivity">  <androID.support.v7.Widget.RecyclerVIEw    androID:ID="@+ID/rv_text_List"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"></androID.support.v7.Widget.RecyclerVIEw></relativeLayout>

MainActivity中的代码也很简单,获取空间,绑定数据源:

package com.example.frIEndzhanshou;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import androID.support.v7.Widget.linearlayoutmanager;import androID.support.v7.Widget.RecyclerVIEw;public class MainActivity extends AppCompatActivity {  private RecyclerVIEw mRvTextList;  @OverrIDe  protected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main);    mRvTextList= (RecyclerVIEw) findVIEwByID(R.ID.rv_text_List);    mRvTextList.setLayoutManager(new linearlayoutmanager(this,linearlayoutmanager.VERTICAL,false));    mRvTextList.setAdapter(new Textlistadapter(this));  }}

demo下载地址:friendzhanshou_jb51.rar

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

总结

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

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存