listview改变字体大小实例讲解
程序员文章站
2023-12-10 22:24:46
效果:点击字体,字体变大 主要利用的getview()方法和setonitemclicklistener()方法 listtext.java 复制代码 代码如下: pack...
效果:点击字体,字体变大
主要利用的getview()方法和setonitemclicklistener()方法
listtext.java
复制代码 代码如下:
package lt.com;
import java.util.arraylist;
import java.util.hashmap;
import java.util.list;
import java.util.map;
import android.app.activity;
import android.app.alertdialog;
import android.app.listactivity;
import android.content.context;
import android.content.dialoginterface;
import android.os.bundle;
import android.util.log;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
import android.widget.adapterview;
import android.widget.baseadapter;
import android.widget.button;
import android.widget.imageview;
import android.widget.listview;
import android.widget.textview;
import android.widget.adapterview.onitemclicklistener;
public class listtext extends activity {
list<map<string,object>> mdata;
public static int select_item = -1;
//myadapter adapter;
/** called when the activity is first created. */
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
listview lv=(listview)findviewbyid(r.id.lv) ;
mdata= getdate();
final myadapter adapter =new myadapter(this);
lv.setadapter(adapter);
log.v("tag", "100");
//点击事件
lv.setonitemclicklistener(new onitemclicklistener(){
public void onitemclick(adapterview<?> arg0, view arg1, int arg2,
long arg3) {
select_item = arg2; //当前选择的item
adapter.notifydatasetchanged(); //通知adapter刷新数据
log.v("tag", "1");
}
public void onnothingselected(adapterview<?> arg0) {
}
});
}
//item相关信息 名称 图片
public list<map<string,object>> getdate(){
list<map<string,object>> list=new arraylist<map<string,object>>(); //存在一个大仓库,摆放着很多抽屉 ,list相当把抽屉放进仓库。
// 这是upcast 或者arraylist<map<string,object>> list=new arraylist<map<string,object>>();也行
map<string,object> map=new hashmap<string,object>();//抽屉,里面有东西。
map.put("text", "中国");//把东西放到抽屉里面
list.add(map);//把抽屉放到仓库里
hashmap<string,object> map1=new hashmap<string,object>();
map1.put("text", "美国");
list.add(map1);
hashmap<string,object> map2=new hashmap<string,object>();
map2.put("text", "日本");
list.add(map2);
return list;
}
//自定义适配器
public class myadapter extends baseadapter{
private layoutinflater minflater;//instantiates a layout xml file into its corresponding view objects.
private int select_item;
public myadapter(context context){
this.minflater = layoutinflater.from(context);//obtains the layoutinflater from the given context.
}
//item的数量
public int getcount() {
// todo auto-generated method stub
return mdata.size();
}
public object getitem(int arg0) {
// todo auto-generated method stub
return null;
}
public long getitemid(int position) {
// todo auto-generated method stub
return 0;
}
//convertview是复用的view,如果没有旧的就新建个新的view;parent是listview
public view getview(int position, view convertview, viewgroup parent) {
// todo auto-generated method stub
viewholder holder = null;
if(convertview==null){
holder=new viewholder();
convertview=minflater.inflate(r.layout.main, null);//inflate a new view hierarchy from the specified xml resource.
holder.texta = (textview)convertview.findviewbyid(r.id.text);
convertview.settag(holder);//sets the tag associated with this view , a tag can be used to mark a view in its hierarchy and does not have to be unique within the hierarchy.
//这个view是holder绘制的
}
else{
holder = (viewholder)convertview.gettag();//get tag
}
holder.texta.settext((string)mdata.get(position).get("text"));
this.select_item = listtext.select_item;
try{
if(this.select_item == position){
holder.texta.settextsize(50); //选中的item字体:50px
log.v("tag", "3");
}
else
holder.texta.settextsize(20); //未选中的item字体:10px
log.v("tag", "2");
}catch(exception ex){
ex.printstacktrace();
}
return convertview;
}
}
/** listview 中某项被选中后的逻辑
protected void onlistitemclick(listview l, view v, int position, long id) {
select_item = position; //当前选择的item
// adapter.notifydatasetchanged(); //通知adapter刷新数据
log.v("tag", "1");
}
*/
public final class viewholder{
textview texta;
}
}
main.xml
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<listview
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</listview>
<textview
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</linearlayout>