Android显示全文折叠控件使用方法详解
程序员文章站
2023-10-29 12:43:16
一般列表里文字太多的一个折叠效果的空间,效果图如下。
当文字超过设定的行数后就折叠,小于设定行数不显示展开按钮。下面上代码。
先看布局文件:
<...
一般列表里文字太多的一个折叠效果的空间,效果图如下。
当文字超过设定的行数后就折叠,小于设定行数不显示展开按钮。下面上代码。
先看布局文件:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/color_white" > <textview android:id="@+id/desc_tv" style="@style/font2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" /> <textview android:id="@+id/desc_op_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/desc_tv" android:gravity="center_vertical" android:singleline="true" android:text="@string/quan_wen" android:textcolor="#5f897b" android:textsize="16sp" android:visibility="gone" /> </relativelayout>
很简单,上面的textview显示主要的文本内容,下面的就是折叠的时候点击的。
下面是自定义。
package xxx; import android.annotation.suppresslint; import android.content.context; import android.os.handler; import android.os.message; import android.util.attributeset; import android.view.view; import android.widget.linearlayout; import android.widget.textview; import android.widget.textview.buffertype; import xxx.r; /** * 查看全文控件 */ public class collapsibletextview extends linearlayout implements view.onclicklistener { private static final int collapsible_state_none = 0;// 不显示 private static final int collapsible_state_shrinkup = 1;// 显示收起 private static final int collapsible_state_spread = 2;// 显示全文 private int mstate = collapsible_state_spread; private static final string collapsible_state_shrinkup_text = "收起"; private static final string collapsible_state_spread_text = "全文"; private textview mtext; /** * @return returns the mtext. */ public textview getmtext() { return mtext; } public int getmstate() { return mstate; } public void setmstate(int mstate) { this.mstate = mstate; } private textview mtexttip; private changestate changestatecallback; private boolean isneedlayout; private int maxlinecount = 8; private final handler handler = new handler() { @override public void dispatchmessage(message msg) { if (mtext.getlinecount() <= maxlinecount) { // 行数不足不做处理 mstate = collapsible_state_none; mtext.setmaxlines(integer.max_value); mtexttip.setvisibility(view.gone); } else { switch (mstate) { case collapsible_state_spread: // 全文状态 mtext.setmaxlines(maxlinecount); mtexttip.setvisibility(view.visible); mtexttip.settext(collapsible_state_spread_text); break; case collapsible_state_shrinkup: // 收起状态 mtext.setmaxlines(integer.max_value); mtexttip.setvisibility(view.visible); mtexttip.settext(collapsible_state_shrinkup_text); break; default: // 除非发生不可知状态,一般不会执行到这个 mstate = collapsible_state_none; mtext.setmaxlines(integer.max_value); mtexttip.setvisibility(view.gone); break; } } } }; public collapsibletextview(context context) { this(context, null); initview(); } public collapsibletextview(context context, attributeset attrs) { super(context, attrs); initview(); } @suppresslint("newapi") public collapsibletextview(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); initview(); } private void initview() { view view = inflate(getcontext(), r.layout.collapsible_textview, this); view.setpadding(0, -1, 0, 0); mtext = (textview) view.findviewbyid(r.id.desc_tv); mtexttip = (textview) view.findviewbyid(r.id.desc_op_tv); mtexttip.setonclicklistener(this); } /** * 设置文本 * * @param charsequence * @param buffertype */ public final void settext(charsequence charsequence, buffertype buffertype) { isneedlayout = true; mstate = collapsible_state_spread; mtext.settext(charsequence, buffertype); } /** * 设置文本 * * @param charsequence */ public final void settext(charsequence charsequence) { isneedlayout = true; mtext.settext(charsequence); } @override public void onclick(view v) { isneedlayout = true; if (mstate == collapsible_state_spread) { // 如果是全文状态,就改成收起状态 mstate = collapsible_state_shrinkup; requestlayout(); } else if (mstate == collapsible_state_shrinkup) { // 如果是收起状态,就改成全文状态 mstate = collapsible_state_spread; requestlayout(); } if (null != changestatecallback) { changestatecallback.changeflag(v); } } @override protected void onlayout(boolean changed, int l, int t, int r, int b) { super.onlayout(changed, l, t, r, b); if (isneedlayout) { isneedlayout = false; handler.sendmessage(message.obtain()); } } public int getmaxlinecount() { return maxlinecount; } public void setmaxlinecount(int maxlinecount) { this.maxlinecount = maxlinecount; } public changestate getchangestatecallback() { return changestatecallback; } public void setchangestatecallback(changestate changestatecallback) { this.changestatecallback = changestatecallback; } public interface changestate { public void changeflag(view v); } }
点击展开后重新绘制根据状态值触发。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 每天喝菊花茶好吗,有什么好处吗
下一篇: 玫瑰花茶减肥吗?答案全在这里了!