Android入门(2)—listview以及textview的折叠展开
先上listview效果图:
这次主要是通过编辑器this.listview.setAdapter对列表进行构建:
代码如下:
private String data[]={"小工具1","小工具2","小工具3","小工具4","小工具5","小工具6","小工具7","小工具7","小工具9","画板功能","查看事务"};
private ListView listview;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.listview=new ListView(this);
this.listview.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,this.data));
//定义适配器,每行显示一条数据,并设置组件内容
super.setContentView(listview);
}
this.listview.setAdapter主要将data中的内容设置到组件中去,我们可以从中体会到适配器的方便快捷和强大。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/ScrollView1" 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=".list" > <LinearLayout android:id="@+id/mylinear" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > </LinearLayout> </ScrollView>
另外,本来想通过对listview的监听,跳转到文本折叠页面,但是还没学会。只能展示一下可折叠的textview
定的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="match_parent" android:orientation="vertical" > <com.example.jiang.ExpandableTextView android:id="@+id/lorem_ipsum" android:textSize="20sp" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
首先通过ExpandableTextView.java设置了文本的折叠和扩展部分:
private CharSequence originalText; //原始部分
private CharSequence trimmedText; //修建部分
private BufferType bufferType;
private boolean trim = true;
private int trimLength;
public ExpandableTextView(Context context) {
this(context, null);
}
//对可扩展部分的设置
public ExpandableTextView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ExpandableTextView);
this.trimLength = typedArray.getInt(R.styleable.ExpandableTextView_trimLength, DEFAULT_TRIM_LENGTH);
typedArray.recycle();
setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
trim = !trim;
setText();
requestFocusFromTouch();
}
});
}
private void setText() {
super.setText(getDisplayableText(), bufferType);
}
//获取可显示部分
private CharSequence getDisplayableText() {
return trim ? trimmedText : originalText;
}
//从所有原文本中获取修剪后的文本
@Override
public void setText(CharSequence text, BufferType type) {
originalText = text;
trimmedText = getTrimmedText(text);
bufferType = type;
setText();
}
private CharSequence getTrimmedText(CharSequence text) {
if (originalText != null && originalText.length() > trimLength) {
return new SpannableStringBuilder(originalText, 0, trimLength + 1).append(ELLIPSIS);
} else {
return originalText;
}
}
public CharSequence getOriginalText() {
return originalText;
}
//设置修剪长度
public void setTrimLength(int trimLength) {
this.trimLength = trimLength;
trimmedText = getTrimmedText(originalText);
setText();
}
//获得修剪长度
public int getTrimLength() {
return trimLength;
}
然后通过MyAactivity将数据插入到textview中:
String yourText = "于结绳记事和契刻记事的不足,人们不得不采用一些其它的、譬如图画的方法来帮助记忆、表达思想,绘画导致了文字的产生"
+ ".唐兰先生在《中国文字学》中说:“文字的产生,本是很自然的,几万年前旧石器时代的人类,已经有很好的绘画,这些画大抵是动物和人像,"
+ "这是文字的前驱.”然而图画发挥文字的作用,转变成文字,只有在“有了较普通、较广泛的语言”之后才有可能.譬如,有人画了一只虎,大家"
+ "见了才会叫它为“虎”;画了一头象,大家见了才会叫它为“象”.久而久之,大家约定俗成,类似于上面说的“虎”和“象”这样的图画,就介于图"
+ "画和文字之间,久而用之了.随着时间的推移,这样的图画越来越多,画得也就不那么逼真了.这样的图画逐渐向文字方向偏移,最终导致文字从图画"
+ "中分离出来.这样,图画就分了家,分成原有的逼真的图画和变成为文字符号的图画文字.图画文字进一步发展为象形文字.正如中国文字学所说:"
+ "“文字本于图画,最初的文字是可以读出来的图画,但图画却不一定都能读.后来,文字跟图画渐渐分歧,差别逐渐显着,文字不再是图画的,而"
+ "是书写的.而书写的技术不需要逼真的描绘,只要把特点写出来,大致不错,使人能认识就够了.”这就是原始的文字. ";
//插入内容
ExpandableTextView expandableTextView = (ExpandableTextView) findViewById(R.id.lorem_ipsum);
expandableTextView.setText(yourText);
效果图:
折叠(末尾有省略号):
点击文字部分即可展开,展开效果: