最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2017年02月11日 08:30:00   作者:丁先森  
本篇文章主要介紹了Android仿微信朋友圈全文收起功能示例(附源碼),非常具有實(shí)用價(jià)值,需要的朋友可以參考下。

在眾多的社交類(lèi)軟件中,朋友圈是必不可少的,可以與好友、同學(xué)等分享自己的日常和有意思的事情,在開(kāi)發(fā)社交類(lèi)App時(shí),朋友圈發(fā)表的內(nèi)容你不可能讓他全部顯示,全部顯示的話(huà)用戶(hù)體驗(yàn)度會(huì)非常不好,這時(shí)就要用到全文、收縮的功能,朋友如果想要看你發(fā)的動(dòng)態(tài),只要點(diǎn)一下全文就可以查看所有的全部的內(nèi)容了,如果不想看,也沒(méi)有必要把這一篇文章全部都滑到底部,才能看下一條內(nèi)容。

下邊將源碼貼出來(lái)供大家參考:(代碼不是最簡(jiǎn)便的,但是功能是可以的)

首先寫(xiě)一個(gè)布局,這個(gè)布局是每個(gè)子項(xiàng)的布局 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>

寫(xiě)一個(gè)Util類(lèi),其實(shí)是存放的數(shù)據(jù),也可以讀取數(shù)據(jù)庫(kù),獲取JSON字符串,這里為了實(shí)現(xiàn)功能就是用固定的數(shù)據(jù)

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òng)作類(lèi)影片中,只要發(fā)生混亂,那么絕對(duì)就有木倉(cāng)戰(zhàn)。現(xiàn)在的技術(shù)越來(lái)越發(fā)達(dá),電影或電視中的特效也做的越來(lái)越逼真,演員們被木倉(cāng)打中的效果也很形象,我們經(jīng)常能看到被木倉(cāng)打中的傷口血淋林的暴露在大屏幕中,從演員的表演中我們能看到木倉(cāng)擊是很痛的,那么你們有想過(guò)被木倉(cāng)打中到底會(huì)有多痛?什么感覺(jué)嗎?網(wǎng)站有網(wǎng)友為我們分享被子彈打中的感覺(jué)\n" +
          "1、“老實(shí)說(shuō),比我想象中的感覺(jué)要輕很多。本來(lái)我以為很痛,可是被打中后就像是被棒球擊中的感覺(jué)一樣,剛開(kāi)始的幾秒鐘沒(méi)什么知覺(jué),過(guò)會(huì)才感到痛\n" +
          "2、“被子彈打到的感覺(jué)就像是一直有人拿針扎你一樣,刺痛刺痛的。”\n" +
          "3、“我當(dāng)初大腿被木倉(cāng)擊中,子彈直接從我的大腿中傳過(guò)去,連帶著我的肌腱也被擊中,那種感覺(jué)我覺(jué)得用疼痛兩個(gè)字已經(jīng)不足以形容了\n" +
          "4、“在我十七歲的時(shí)候,腳被木倉(cāng)擊中,當(dāng)時(shí)我以為是被蜜蜂蟄了,因?yàn)榉路鹇?tīng)到了蜜蜂的聲音,沒(méi)過(guò)幾秒鐘,腳上就傳來(lái)灼熱感,這才知道原來(lái)是被木倉(cāng)擊中了。\n" +
          "5、“我只是聽(tīng)到的木倉(cāng)聲,卻沒(méi)有意識(shí)到自己中木倉(cāng)了。直到血流出來(lái)才意識(shí)到。所以,對(duì)我來(lái)講,被子彈擊中沒(méi)什么感覺(jué)。"
     ,
      "GNOME or KDE desktop\n" +
          " processor with support for AMD Virtualization™ (AMD-V™)"


  };

  /**
   * 獲取文本內(nèi)容根據(jù)下標(biāo)
   *
   * @param position
   * @return
   */

  public static String getContent(int position) {
    return contentArray[position % contentArray.length];
  }

  /**
   * 獲取名稱(chēng)根據(jù)下標(biāo)
   *
   * @param position
   * @return
   */
  public static String getName(int position) {
    return nameArray[position % contentArray.length];
  }
}

設(shè)置適配器

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;//文本行數(shù)不能超過(guò)限定行數(shù)

  private final int STATE_COLLAPSED = 2;//文本行數(shù)超過(guò)限定行數(shù),進(jìn)行折疊

  private final int STATE_EXPANDED = 3;//文本超過(guò)限定行數(shù),被點(diǎn)擊全文展開(kāi)

  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+"");//設(shè)置頭部的文字
    holder.name.setText(Util.getName(position));//設(shè)置名稱(chēng)
    int state=mTextStateList.get(position,STATE_UNKNOW);
//    如果該itme是第一次初始化,則取獲取文本的行數(shù)
    if (state==STATE_UNKNOW){
      holder.content.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
//          這個(gè)回掉會(huì)調(diào)用多次,獲取玩行數(shù)后記得注銷(xiāo)監(jiān)聽(tīng)
          holder.content.getViewTreeObserver().removeOnPreDrawListener(this);
//          holder.content.getViewTreeObserver().addOnPreDrawListener(null);
//          如果內(nèi)容顯示的行數(shù)大于限定顯示行數(shù)
          if (holder.content.getLineCount()>MAX_LINE_COUNT) {
            holder.content.setMaxLines(MAX_LINE_COUNT);//設(shè)置最大顯示行數(shù)
            holder.expandOrCollapse.setVisibility(View.VISIBLE);//讓其顯示全文的文本框狀態(tài)為顯示
            holder.expandOrCollapse.setText("全文");//設(shè)置其文字為全文
            mTextStateList.put(position, STATE_COLLAPSED);
          }else{
            holder.expandOrCollapse.setVisibility(View.GONE);//顯示全文隱藏
            mTextStateList.put(position,STATE_NOT_OVERFLOW);//讓其不能超過(guò)限定的行數(shù)
          }
          return true;
        }
      });

      holder.content.setMaxLines(Integer.MAX_VALUE);//設(shè)置文本的最大行數(shù),為整數(shù)的最大數(shù)值
      holder.content.setText(Util.getContent(position));//用Util中的getContent方法獲取內(nèi)容
    }else{
//      如果之前已經(jīng)初始化過(guò)了,則使用保存的狀態(tài),無(wú)需在獲取一次
      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));
    }


//    設(shè)置顯示和收起的點(diǎn)擊事件
    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);
    }
  }
}

主布局的內(nèi)容:

<?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中的代碼也很簡(jiǎn)單,獲取空間,綁定數(shù)據(jù)源:

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android簡(jiǎn)易音樂(lè)播放器實(shí)現(xiàn)代碼

    Android簡(jiǎn)易音樂(lè)播放器實(shí)現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了Android簡(jiǎn)易音樂(lè)播放器的實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • Android 開(kāi)發(fā)與代碼無(wú)關(guān)技巧詳解

    Android 開(kāi)發(fā)與代碼無(wú)關(guān)技巧詳解

    這篇文章主要為大家介紹了Android 開(kāi)發(fā)與代碼無(wú)關(guān)技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • android中創(chuàng)建通知欄Notification代碼實(shí)例

    android中創(chuàng)建通知欄Notification代碼實(shí)例

    這篇文章主要介紹了android中創(chuàng)建通知欄Notification代碼實(shí)例,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2015-05-05
  • Android實(shí)現(xiàn)自定義圓角對(duì)話(huà)框Dialog的示例代碼

    Android實(shí)現(xiàn)自定義圓角對(duì)話(huà)框Dialog的示例代碼

    項(xiàng)目中多處用到對(duì)話(huà)框,本篇文章主要介紹了Android實(shí)現(xiàn)圓角對(duì)話(huà)框Dialog的示例代碼,有興趣的可以了解一下。
    2017-03-03
  • Android?Camera開(kāi)發(fā)實(shí)現(xiàn)可復(fù)用的相機(jī)組件

    Android?Camera開(kāi)發(fā)實(shí)現(xiàn)可復(fù)用的相機(jī)組件

    這篇文章主要為大家詳細(xì)介紹了Android?Camera開(kāi)發(fā)實(shí)現(xiàn)可復(fù)用的相機(jī)組件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Android筆記整理之常見(jiàn)錯(cuò)誤及解決方案匯總

    Android筆記整理之常見(jiàn)錯(cuò)誤及解決方案匯總

    本文是小編日常整理的筆記關(guān)于android常見(jiàn)的錯(cuò)誤及解決方案,本文對(duì)大家學(xué)習(xí)android相關(guān)知識(shí)非常有幫助,遇到問(wèn)題能夠很好的定位問(wèn)題解決問(wèn)題,感興趣的朋友了解了解吧
    2015-11-11
  • Android AlertDialog的幾種用法詳解

    Android AlertDialog的幾種用法詳解

    這篇文章主要介紹了Android AlertDialog的幾種用法詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • Android仿微博首頁(yè)Tab加號(hào)彈窗功能

    Android仿微博首頁(yè)Tab加號(hào)彈窗功能

    這篇文章主要為大家詳細(xì)介紹了Android仿微博首頁(yè)Tab加號(hào)彈窗功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Kotlin封裝RecyclerView Adapter實(shí)例教程

    Kotlin封裝RecyclerView Adapter實(shí)例教程

    這篇文章主要給大家介紹了關(guān)于Kotlin封裝RecyclerView Adapter的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-08-08
  • Andriod 獲取電池的信息實(shí)例代碼

    Andriod 獲取電池的信息實(shí)例代碼

    通過(guò)本段實(shí)例代碼給大家介紹Andriod 獲取電池的信息的相關(guān)知識(shí),對(duì)android獲取電池信息相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧
    2016-03-03

最新評(píng)論

民丰县| 台州市| 西藏| 宝山区| 罗定市| 龙胜| 吉隆县| 武隆县| 防城港市| 永城市| 从化市| 庐江县| 天气| 伊金霍洛旗| 仪陇县| 庆云县| 滁州市| 房山区| 息烽县| 瑞昌市| 岢岚县| 道孚县| 平顺县| 永州市| 泽州县| 犍为县| 镇宁| 无棣县| 贵州省| 佛教| 周口市| 宁武县| 古丈县| 合水县| 柳州市| 蓝田县| 黑龙江省| 兰考县| 南宁市| 峨眉山市| 根河市|