Android可收缩/扩展的TextView【2】

我写的可扩展/收缩的TextView:PhilExpandableTextView使用方法在附录文章1中已经说明。简单的使用,可以直接写在布局中,初始默认是收缩成3行(可修改继续定制),当用户从触发expand事件后,PhilExpandableTextView将伸展成完全的行数。
PhilExpandableTextView在ListView一类的使用中,有些特殊,现在以PhilExpandableTextView在Android RecyclerView中的使用为例加以说明。
测试的主Activity MainActivity.java:

package zhangphil.text;import java.util.ArrayList;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.RecyclerView;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import android.widget.LinearLayout;public class MainActivity extends Activity {private ArrayList<Boolean> flags;private MyRecyclerViewAdapter adapter;private String test_str = "";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 测试的字符串for (int i = 0; i < 100; i++)test_str = test_str + " " + i;setContentView(R.layout.activity_main);RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);mLayoutManager.setOrientation(LinearLayout.VERTICAL);mRecyclerView.setLayoutManager(mLayoutManager);adapter = new MyRecyclerViewAdapter(this);mRecyclerView.setAdapter(adapter);}private class MyViewHolder extends RecyclerView.ViewHolder {private PhilExpandableTextView text;private Button button;public MyViewHolder(View itemView) {super(itemView);text = (PhilExpandableTextView) itemView.findViewById(R.id.textView);button = (Button) itemView.findViewById(R.id.button);}}private class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyViewHolder> {private LayoutInflater inflater;public MyRecyclerViewAdapter(Context context) {inflater = LayoutInflater.from(context);flags = new ArrayList<Boolean>();for (int i = 0; i < this.getItemCount(); i++) {flags.add(false);}}@Overridepublic int getItemCount() {return 100;}@Overridepublic void onBindViewHolder(MyViewHolder holder, final int pos) {holder.text.setText("position:" + pos + " -> " + test_str);holder.button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {boolean b = flags.get(pos);b = !b;flags.set(pos, b);adapter.notifyDataSetChanged();}});boolean b = flags.get(pos);holder.text.setExpandable(b);String s = "";if (b)s = "收缩 " + pos;elses = "展开 " + pos;holder.button.setText(s);}@Overridepublic MyViewHolder onCreateViewHolder(ViewGroup group, int pos) {View v = inflater.inflate(R.layout.item, null);MyViewHolder holder = new MyViewHolder(v);return holder;}}}


MainActivity.java需要的布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="zhangphil.text.MainActivity" >    <android.support.v7.widget.RecyclerView        android:id="@+id/recyclerView"        android:divider="#ff5252"        android:dividerHeight="10dip"        android:layout_width="match_parent"        android:layout_height="match_parent"/></RelativeLayout>



item.xml布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_margin="5dip"    android:background="@android:color/white"    tools:context="zhangphil.text.MainActivity" >    <zhangphil.text.PhilExpandableTextView        android:id="@+id/textView"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_margin="10dip"        android:background="#03a9f4"        android:textColor="@android:color/white" />    <Button        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_below="@+id/textView"        android:text="展开" /></RelativeLayout>


我写的PhilExpandableTextView.java:

package zhangphil.text;import android.content.Context;import android.util.AttributeSet;import android.view.ViewTreeObserver;import android.widget.TextView;public class PhilExpandableTextView extends TextView {// 最大的行,默认只显示3行private final int MAX = 3;// 如果完全伸展需要多少行?private int lines;private PhilExpandableTextView mPhilTextView;// 标记当前TextView的展开/收缩状态// true,已经展开// false,以及收缩private boolean expandableStatus = false;public PhilExpandableTextView(Context context, AttributeSet attrs) {super(context, attrs);mPhilTextView = this;init();}private void init() {// ViewTreeObserver View观察者,在View即将绘制但还未绘制的时候执行的,在onDraw之前final ViewTreeObserver mViewTreeObserver = this.getViewTreeObserver();mViewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {@Overridepublic boolean onPreDraw() {// 避免重复监听mPhilTextView.getViewTreeObserver().removeOnPreDrawListener(this);lines = getLineCount();// Log.d(this.getClass().getName(), lines+"");return true;}});setMaxLines(MAX);//setEllipsize(TextUtils.TruncateAt.END);}// 是否展开或者收缩,// true,展开;// false,不展开public void setExpandable(boolean isExpand) {if (isExpand) {setMaxLines(lines + 1);} elsesetMaxLines(MAX);expandableStatus = isExpand;}public boolean getExpandableStatus() {return expandableStatus;}}


代码运行结果:


附录我写的相关文章:
【文章1】《Android可收缩/扩展的TextView【1】》链接地址:http://blog.csdn.net/zhangphil/article/details/50088465

更多相关文章

  1. Android学习笔记——各种常用布局管理器的使用
  2. 安卓基础到入门学习(复习笔记)
  3. Android(安卓)蓝牙开发(四)OPP传输文件
  4. Android(安卓)SDK中tools详解
  5. Android(安卓)利用命令生成keystore文件
  6. Android逆向之旅—解析编译之后的Resource.arsc文件格式
  7. 基于树莓派的 Android(安卓)Things 开发环境
  8. android关于百度地图显示网格问题
  9. Android(安卓)UI开发第二十篇——仿launcher的左右滑动(用ViewPag

随机推荐

  1. Android(安卓)TTS学习——保存对你的喜欢
  2. Android安装应用后点击,打开.(Open)带来
  3. android开发环境 国内镜像 及Android(安
  4. Android-Fresco系列3 Producer
  5. Android中怎样获取SD卡路径
  6. Android(安卓)UI学习 - Tab的学习和使用
  7. Android实现RecyclerView显示json数据并
  8. Delphi XE5 android 图解为Android应用制
  9. Android自动化测试Espresso+UIAutomator
  10. Android_notifyDataSetChanged()