Android仿微信朋友圈,全文收起功能,附源碼

在衆多的社交類軟件中,朋友圈是必不可少的,能夠與好友、同窗等分享本身的平常和有意思的事情,在開發社交類App時,朋友圈發表的內容你不可能讓他所有顯示,所有顯示的話用戶體驗度會很是很差,這時就要用到全文、收縮的功能,朋友若是想要看你發的動態,只要點一下全文就能夠查看全部的所有的內容了,若是不想看,也沒有必要把這一篇文章所有都滑到底部,才能看下一條內容。android

 

源碼地址:連接:http://pan.baidu.com/s/1boQC7vT 密碼:qfnk數據庫

若是連接失效,請發我郵箱:dingchao7323@qq.com看一下具體的效果圖:app

  

下邊將源碼貼出來供你們參考:(代碼不是最簡便的,可是功能是能夠的)ide

 首先寫一個佈局,這個佈局是每一個子項的佈局 item_text_list.xml佈局

 

 1 <?xml version="1.0" encoding="utf-8"?>  2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  3  android:layout_width="match_parent"  4  android:layout_height="wrap_content"  5  android:orientation="vertical"  6  android:paddingBottom="@dimen/activity_vertical_margin"  7  android:paddingLeft="@dimen/activity_horizontal_margin"  8  android:paddingRight="@dimen/activity_horizontal_margin"  9  android:paddingTop="@dimen/activity_vertical_margin"> 10 11 <LinearLayout 12 android:layout_width="match_parent" 13  android:layout_height="wrap_content" 14  android:layout_gravity="center_vertical" 15  android:orientation="horizontal"> 16 17 <TextView 18 android:id="@+id/tv_hend" 19  android:layout_width="40dp" 20  android:layout_height="40dp" 21  android:layout_marginRight="16dp" 22  android:background="@drawable/circle" 23  android:gravity="center" 24  android:text="1" 25  android:textColor="@android:color/white" 26  android:textSize="14sp" /> 27 28 <TextView 29 android:id="@+id/tv_name" 30  android:layout_width="wrap_content" 31  android:layout_height="wrap_content" 32  android:alpha="0.87" 33  android:text="丁先森" 34  android:textColor="@android:color/black" 35  android:textSize="14sp" /> 36 </LinearLayout> 37 38 <LinearLayout 39 android:layout_width="match_parent" 40  android:layout_height="wrap_content" 41  android:layout_marginLeft="56dp" 42  android:orientation="vertical" 43  android:paddingBottom="8dp"> 44 45 <TextView 46 android:id="@+id/tv_content" 47  android:layout_width="wrap_content" 48  android:layout_height="wrap_content" 49  android:layout_marginBottom="8dp" 50  android:alpha="0.85" 51  android:ellipsize="end" 52  android:text="" 53  android:textColor="@android:color/black" 54  android:textSize="14sp" /> 55 56 <TextView 57 android:id="@+id/tv_expand_or_collapse" 58  android:layout_width="wrap_content" 59  android:layout_height="wrap_content" 60  android:text="全文" 61  android:textColor="@color/colorPrimaryDark" 62  android:textSize="14sp" /> 63 </LinearLayout> 64 65 <View 66 android:layout_width="match_parent" 67  android:layout_height="0.5dp" 68 android:layout_marginLeft="56dp" 69 android:alpha="0.12" 70 android:background="@android:color/black" /> 71 </LinearLayout>

 

 

 

 

 

寫一個Util類,實際上是存放的數據,也能夠讀取數據庫,獲取JSON字符串,這裏爲了實現功能就是用固定的數據網站

Util.classthis

 1 package com.example.friendzhanshou;  2  3 /**  4  * @author DingChao  5  * 2017/2/10  6 */  7  8 public class Util {  9 private static String[] nameArray = new String[]{ 10 "Windows", "Mac", "Linux" 11  }; 12 private static String[] contentArray = new String[]{ 13 "在動做類影片中,只要發生混亂,那麼絕對就有木倉戰。如今的技術愈來愈發達,電影或電視中的特效也作的愈來愈逼真,演員們被木倉打中的效果也很形象,咱們常常能看到被木倉打中的傷口血淋林的暴露在大屏幕中,從演員的表演中咱們能看到木倉擊是很痛的,那麼大家有想過被木倉打中到底會有多痛?什麼感受嗎?網站有網友爲咱們分享被子彈打中的感受\n" + 14 "一、「老實說,比我想象中的感受要輕不少。原本我覺得很痛,但是被打中後就像是被棒球擊中的感受同樣,剛開始的幾秒鐘沒什麼知覺,過會才感到痛\n" + 15 "二、「被子彈打到的感受就像是一直有人拿針扎你同樣,刺痛刺痛的。」\n" + 16 "三、「我當初大腿被木倉擊中,子彈直接從個人大腿中傳過去,連帶着個人肌腱也被擊中,那種感受我以爲用疼痛兩個字已經不足以形容了\n" + 17 "四、「在我十七歲的時候,腳被木倉擊中,當時我覺得是被蜜蜂蟄了,由於彷彿聽到了蜜蜂的聲音,沒過幾秒鐘,腳上就傳來灼熱感,這才知道原來是被木倉擊中了。\n" + 18 "五、「我只是聽到的木倉聲,卻沒有意識到本身中木倉了。直到血流出來才意識到。因此,對我來說,被子彈擊中沒什麼感受。" 19  , 20 "GNOME or KDE desktop\n" + 21 " processor with support for AMD Virtualization™ (AMD-V™)" 22 23 24  }; 25 26 /** 27  * 獲取文本內容根據下標 28  * 29  * @param position 30  * @return 31 */ 32 33 public static String getContent(int position) { 34 return contentArray[position % contentArray.length]; 35  } 36 37 /** 38  * 獲取名稱根據下標 39  * 40  * @param position 41  * @return 42 */ 43 public static String getName(int position) { 44 return nameArray[position % contentArray.length]; 45  } 46 }

 

設置適配器spa

TextListAdapter.class

  1 package com.example.friendzhanshou;  2  3 import android.app.Activity;  4 import android.support.v7.widget.RecyclerView;  5 import android.util.SparseArray;  6 import android.view.View;  7 import android.view.ViewGroup;  8 import android.view.ViewTreeObserver;  9 import android.widget.Adapter;  10 import android.widget.TextView;  11  12 /**  13  * @author DingChao  14  * 2017/2/10  15 */  16  17 public class TextListAdapter extends RecyclerView.Adapter<TextListAdapter.TextHolder> {  18 private Activity mContent;  19  20 private final int MAX_LINE_COUNT = 3;  21  22 private final int STATE_UNKNOW = -1;  23  24 private final int STATE_NOT_OVERFLOW = 1;//文本行數不能超過限定行數  25  26 private final int STATE_COLLAPSED = 2;//文本行數超過限定行數,進行摺疊  27  28 private final int STATE_EXPANDED = 3;//文本超過限定行數,被點擊全文展開  29  30 private SparseArray<Integer> mTextStateList;  31  32 public TextListAdapter(Activity context) {  33 mContent = context;  34 mTextStateList = new SparseArray<>();  35  }  36  37  @Override  38 public TextHolder onCreateViewHolder(ViewGroup parent, int viewType) {  39 return new TextHolder(mContent.getLayoutInflater().inflate(R.layout.item_test_list, parent, false));  40  }  41  42  @Override  43 public void onBindViewHolder(final TextHolder holder,final int position) {  44 holder.hend.setText(position+1+"");//設置頭部的文字  45 holder.name.setText(Util.getName(position));//設置名稱  46 int state=mTextStateList.get(position,STATE_UNKNOW);  47 // 若是該itme是第一次初始化,則取獲取文本的行數  48 if (state==STATE_UNKNOW){  49 holder.content.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {  50  @Override  51 public boolean onPreDraw() {  52 // 這個回掉會調用屢次,獲取玩行數後記得註銷監聽  53 holder.content.getViewTreeObserver().removeOnPreDrawListener(this);  54 // holder.content.getViewTreeObserver().addOnPreDrawListener(null);  55 // 若是內容顯示的行數大於限定顯示行數  56 if (holder.content.getLineCount()>MAX_LINE_COUNT) {  57 holder.content.setMaxLines(MAX_LINE_COUNT);//設置最大顯示行數  58 holder.expandOrCollapse.setVisibility(View.VISIBLE);//讓其顯示全文的文本框狀態爲顯示  59 holder.expandOrCollapse.setText("全文");//設置其文字爲全文  60  mTextStateList.put(position, STATE_COLLAPSED);  61 }else{  62 holder.expandOrCollapse.setVisibility(View.GONE);//顯示全文隱藏  63 mTextStateList.put(position,STATE_NOT_OVERFLOW);//讓其不能超過限定的行數  64  }  65 return true;  66  }  67  });  68  69 holder.content.setMaxLines(Integer.MAX_VALUE);//設置文本的最大行數,爲整數的最大數值  70 holder.content.setText(Util.getContent(position));//用Util中的getContent方法獲取內容  71 }else{ 72 // 若是以前已經初始化過了,則使用保存的狀態,無需在獲取一次 73 switch (state){ 74 case STATE_NOT_OVERFLOW: 75 holder.expandOrCollapse.setVisibility(View.GONE); 76 break; 77 case STATE_COLLAPSED: 78 holder.content.setMaxLines(MAX_LINE_COUNT); 79 holder.expandOrCollapse.setVisibility(View.VISIBLE); 80 holder.expandOrCollapse.setText("全文"); 81 break; 82 case STATE_EXPANDED: 83 holder.content.setMaxLines(Integer.MAX_VALUE); 84 holder.expandOrCollapse.setVisibility(View.VISIBLE); 85 holder.expandOrCollapse.setText("收起"); 86 break; 87 } 88 holder.content.setText(Util.getContent(position)); 89 } 90 91 92 // 設置顯示和收起的點擊事件 93 holder.expandOrCollapse.setOnClickListener(new View.OnClickListener() { 94 @Override 95 public void onClick(View v) { 96 int state=mTextStateList.get(position,STATE_UNKNOW); 97 if (state==STATE_COLLAPSED){ 98 holder.content.setMaxLines(Integer.MAX_VALUE); 99 holder.expandOrCollapse.setText("收起"); 100 mTextStateList.put(position,STATE_EXPANDED); 101 }else if (state==STATE_EXPANDED){ 102 holder.content.setMaxLines(MAX_LINE_COUNT); 103 holder.expandOrCollapse.setText("全文"); 104 mTextStateList.put(position,STATE_COLLAPSED); 105 } 106 } 107 }); 108 109 } 110 111 @Override 112 public int getItemCount() { 113 return 15; 114 } 115 116 117 public class TextHolder extends RecyclerView.ViewHolder { 118 public TextView hend; 119 public TextView name; 120 public TextView content; 121 public TextView expandOrCollapse; 122 123 public TextHolder(View itemView) { 124 super(itemView); 125 // 綁定xml佈局中的控件 126 hend = (TextView) itemView.findViewById(R.id.tv_hend); 127 name = (TextView) itemView.findViewById(R.id.tv_name); 128 content = (TextView) itemView.findViewById(R.id.tv_content); 129 expandOrCollapse = (TextView) itemView.findViewById(R.id.tv_expand_or_collapse); 130 } 131 } 132 }

主佈局的內容:3d

 1 <?xml version="1.0" encoding="utf-8"?>  2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  3  xmlns:tools="http://schemas.android.com/tools"  4  android:id="@+id/activity_main"  5  android:layout_width="match_parent"  6  android:layout_height="match_parent"  7  android:paddingBottom="@dimen/activity_vertical_margin"  8  android:paddingLeft="@dimen/activity_horizontal_margin"  9  android:paddingRight="@dimen/activity_horizontal_margin" 10  android:paddingTop="@dimen/activity_vertical_margin" 11  tools:context="com.example.friendzhanshou.MainActivity"> 12 13 <android.support.v7.widget.RecyclerView 14 android:id="@+id/rv_text_list" 15  android:layout_width="match_parent" 16  android:layout_height="match_parent"></android.support.v7.widget.RecyclerView> 17 </RelativeLayout>

MainActivity中的代碼也很簡單,獲取空間,綁定數據源:code

 1 package com.example.friendzhanshou;  2  3 import android.support.v7.app.AppCompatActivity;  4 import android.os.Bundle;  5 import android.support.v7.widget.LinearLayoutManager;  6 import android.support.v7.widget.RecyclerView;  7  8 public class MainActivity extends AppCompatActivity {  9 private RecyclerView mRvTextList; 10 11  @Override 12 protected void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14  setContentView(R.layout.activity_main); 15 mRvTextList= (RecyclerView) findViewById(R.id.rv_text_list); 16 mRvTextList.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)); 17 mRvTextList.setAdapter(new TextListAdapter(this)); 18  } 19 }
這些就是主要的實現代碼,若是想要項目,上邊有源碼的地址。
相關文章
相關標籤/搜索