小功能,希望后来者拿去就能用
一:展开的TextView,继承linerlayout,直接上代码了
public class ExpandTextView extends LinearLayout {
public static final int DEFAULT_MAX_LINES = 3;
private TextView contentText;
private TextView textPlus;
private int showLines;
private ExpandStatusListener expandStatusListener;
private boolean isExpand;
public ExpandTextView(Context context) {
super(context);
initView();
}
public ExpandTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initAttrs(attrs);
initView();
}
public ExpandTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initAttrs(attrs);
initView();
}
private void initView() {
setOrientation(LinearLayout.VERTICAL);
LayoutInflater.from(getContext()).inflate(R.layout.layout_magic_text, this);
contentText = (TextView) findViewById(R.id.contentText);
if(showLines > 0){
contentText.setMaxLines(showLines);
}
textPlus = (TextView) findViewById(R.id.textPlus);
textPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String textStr = textPlus.getText().toString().trim();
if("全文".equals(textStr)){
contentText.setMaxLines(Integer.MAX_VALUE);
textPlus.setText("收起");
setExpand(true);
}else{
contentText.setMaxLines(showLines);
textPlus.setText("全文");
setExpand(false);
}
//通知外部状态已变更
if(expandStatusListener != null){
expandStatusListener.statusChange(isExpand());
}
}
});
}
private void initAttrs(AttributeSet attrs) {
TypedArray typedArray = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.ExpandTextView, 0, 0);
try {
showLines = typedArray.getInt(R.styleable.ExpandTextView_showLines, DEFAULT_MAX_LINES);
}finally {
typedArray.recycle();
}
}
public void setText(final CharSequence content){
contentText.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
// 避免重复监听
contentText.getViewTreeObserver().removeOnPreDrawListener(this);
int linCount = contentText.getLineCount();
if(linCount > showLines){
if(isExpand){
contentText.setMaxLines(Integer.MAX_VALUE);
textPlus.setText("收起");
}else{
contentText.setMaxLines(showLines);
textPlus.setText("全文");
}
textPlus.setVisibility(View.VISIBLE);
}else{
textPlus.setVisibility(View.GONE);
}
//Log.d("onPreDraw", "onPreDraw...");
//Log.d("onPreDraw", linCount + "");
return true;
}
});
contentText.setText(content);
// contentText.setMovementMethod(new CircleMovementMethod(getResources().getColor(R.color.name_selector_color)));
}
public void setExpand(boolean isExpand){
this.isExpand = isExpand;
}
public boolean isExpand(){
return this.isExpand;
}
public void setExpandStatusListener(ExpandStatusListener listener){
this.expandStatusListener = listener;
}
public static interface ExpandStatusListener{
void statusChange(boolean isExpand);
}
}
复制代码
布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/contentText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/color_232323"
android:textSize="14sp"
android:text=""
/>
<TextView
android:id="@+id/textPlus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="@color/color_8290AF"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text=""
/>
</LinearLayout>
复制代码
用法
<club.modernedu.expandtextview.ExpandTextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:showLines="2" />
final ExpandTextView tv = (ExpandTextView)findViewById(R.id.tv);
tv.setText("老舍(1899年2月3日—1966年8月24日),原名舒庆春,另有笔名絜青、鸿来、非我等,字舍予。因为老舍生于阴历立春,父母为他取名“庆春”,大概含有庆贺春来、前景美好之意。上学后,自己更名为舒舍予,含有“舍弃自我”,亦即“忘我”的意思。北京满族正红旗人。 [1] 中国现代小说家、作家,语言大师、人民艺术家,新中国第一位获得“人民艺术家”称号的作家。代表作有《骆驼祥子》、《四世同堂》、剧本《茶馆》。\n" +
"老舍的一生,总是忘我地工作,他是文艺界当之无愧的“劳动模范”。1966年,由于受到文化大革命运动中恶毒的攻击和迫害,老舍被逼无奈之下含冤自沉于北京太平湖。 [2] 2017年9月,中国现代文学长篇小说经典《四世同堂》由东方出版中心出版上市。这是该作自发表以来第一次以完整版形式出版 [3] 。");
tv.setExpand(false);
tv.setExpandStatusListener(new ExpandTextView.ExpandStatusListener() {
@Override
public void statusChange(boolean isExpand) {
tv.setExpand(true);
}
});
复制代码
第二个回到顶部,你可能想着ScrollView简单啊,是的没错,不难,但是现在比较推荐用NestedScrollView,用了这个你再调用mScrollView.fullScroll(ScrollView.FOCUS_UP);//滑到就不灵了,为什么了查看一个老外写的说是失效,具体原因不知道最后用 scrollView.fling(0); scrollView.smoothScrollTo(0, 0);完美
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v4.widget.NestedScrollView
android:id="@+id/scrollView"
android:focusable="true"
android:focusableInTouchMode="true"
android:descendantFocusability="beforeDescendants"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<club.modernedu.expandtextview.ExpandTextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:showLines="2" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="一、ScrollView.scrollTo(0,0) 直接置顶,瞬间回到顶部,没有滚动过程,其中Y值可以设置为大于0的值,使Scrollview停在指定位置;
二、ScrollView.fullScroll(View.FOCUS_UP) 类似于手动拖回顶部,有滚动过程;
三、ScrollView.smoothScrollTo(0, 0) 类似于手动拖回顶部,有滚动过程,其中Y值可以设置为大于0的值,使Scrollview停在指定位置。" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="一、ScrollView.scrollTo(0,0) 直接置顶,瞬间回到顶部,没有滚动过程,其中Y值可以设置为大于0的值,使Scrollview停在指定位置;
二、ScrollView.fullScroll(View.FOCUS_UP) 类似于手动拖回顶部,有滚动过程;
三、ScrollView.smoothScrollTo(0, 0) 类似于手动拖回顶部,有滚动过程,其中Y值可以设置为大于0的值,使Scrollview停在指定位置。" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="一、ScrollView.scrollTo(0,0) 直接置顶,瞬间回到顶部,没有滚动过程,其中Y值可以设置为大于0的值,使Scrollview停在指定位置;
二、ScrollView.fullScroll(View.FOCUS_UP) 类似于手动拖回顶部,有滚动过程;
三、ScrollView.smoothScrollTo(0, 0) 类似于手动拖回顶部,有滚动过程,其中Y值可以设置为大于0的值,使Scrollview停在指定位置。" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="一、ScrollView.scrollTo(0,0) 直接置顶,瞬间回到顶部,没有滚动过程,其中Y值可以设置为大于0的值,使Scrollview停在指定位置;
二、ScrollView.fullScroll(View.FOCUS_UP) 类似于手动拖回顶部,有滚动过程;
三、ScrollView.smoothScrollTo(0, 0) 类似于手动拖回顶部,有滚动过程,其中Y值可以设置为大于0的值,使Scrollview停在指定位置。" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
android:id="@+id/floating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom" />
</FrameLayout>
</RelativeLayout>
复制代码
代码有点长,道理简单 逻辑代码如下
FloatingActionButton floating = (FloatingActionButton)findViewById(R.id.floating);
final NestedScrollView scrollView = (NestedScrollView)findViewById(R.id.scrollView);
scrollView.setFocusable(true);
scrollView.setFocusableInTouchMode(true);
scrollView.requestFocus();
floating.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("22","222");
new Handler().post(new Runnable() {
@Override
public void run() {
Log.d("33","333");
scrollView.fling(0);
scrollView.smoothScrollTo(0, 0);
//mScrollView.fullScroll(ScrollView.FOCUS_DOWN);//滑到底部
}
});
}
});
复制代码