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

来源:互联网 发布:win7共享输入网络凭据 编辑:程序博客网 时间:2024/04/28 18:40


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 0
原创粉丝点击