18.Listview结合SimplterAdapter最全面的使用
本章主要讲解在listview中适配中用户想要的布局,通过simpleAdpter实现
第一步:编写你要适配到listview中的布局
<?xml version="1.0" encoding="utf-8"?> <!--发表动态评论转发页面 通过Adapter,data将这个模板展示在listview中--> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/blue"> <!--用户头像--> <ImageView android:id="@+id/user_head" android:layout_width="70sp" android:layout_height="70sp" android:src="@drawable/usericon" android:layout_marginLeft="5dp" android:layout_marginTop="5dp"/> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" > <!--右边框架,用户姓名和用户是否通过认证的图标--> <LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="horizontal" android:layout_marginLeft="10dp"> <TextView android:id="@+id/uer_name" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/app_name" android:textSize="20dp" android:layout_marginTop="10dp" android:textColor="@color/black" /> <ImageView android:id="@+id/auto_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/v" android:layout_marginTop="10dp" android:layout_marginLeft="6dp"/> <!--相对布局,用户发布时间--> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="15dp" > <TextView android:id="@+id/time_put" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/app_name" android:layout_alignParentRight="true" /> </RelativeLayout> </LinearLayout> <!--用户发布的消息--> <TextView android:id="@+id/user_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/content" android:layout_marginTop="10dp" android:layout_marginRight="10dp"/> <ImageView android:id="@+id/user_head_one" android:layout_width="55sp" android:layout_height="55sp" android:src ="@drawable/usericon" android:layout_marginTop="5dp"/> <!-- 微博对话内容,这里运用到了xxx.9图片,可以拉伸,效果好 --> <LinearLayout android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/popup"> <TextView android:id="@+id/user_content_one" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/content" android:layout_marginTop="10dp" android:layout_marginRight="10dp"/> <ImageView android:id="@+id/user_head_two" android:layout_width="55sp" android:layout_height="55sp" android:src ="@drawable/usericon" android:layout_marginTop="5dp"/> </LinearLayout> <LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_marginTop="10dp" android:orientation="horizontal" android:weightSum="1"> <!--用户姓名--> <TextView android:id="@+id/textView_src" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="来自:Touch Android" android:textSize="12dp" android:textColor="@color/hui" /> <!--用户评论次数和转发的次数对于 LinearLayout: 当 android:orientation="vertical" 时, 只有水平方向的设置才起作用,垂直方向的设置不起作用。即:left,right,center_horizontal 是生效的。 当 android:orientation="horizontal" 时, 只有垂直方向的设置才起作用,水平方向的设置不起作用。即:top,bottom,center_vertical 是生效的。 android:drawableLeft=“”在textview中设置图片在文字左边--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="right"> <TextView android:id="@+id/text_view_comment" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="right" android:text="10" android:textSize="12dp" android:drawableLeft="@drawable/redirect_icon" /> <TextView android:id="@+id/text_view_forword" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:drawableLeft="@drawable/comment_icon" android:text="100" android:textSize="12dp" /> </LinearLayout> </LinearLayout> </LinearLayout> </LinearLayout>
第二部:编写JAVAbean用于存储ListView中用到的控件对象
package Bean; import java.util.List; /**用于显示listview中的数据 * Created by Administrator on 2016/9/4. */ public class HomeMsgBean {public String name; //文字对象 public String content; public String content_child; public int head; //图片对象 public int commont; public int commont_head; public int getCommont_head() {return commont_head; }public void setCommont_head(int commont_head) {this.commont_head = commont_head; }public int getCommont() {return commont; }public void setCommont(int commont) {this.commont = commont; }public int getHead() {return head; }public void setHead(int head) {this.head = head; }public HomeMsgBean(String name, String content, String content_child,int head,int commont,int commont_head) {this.name = name; this.content = content; this.content_child = content_child; this.head=head; this.commont=commont; this.commont_head=commont_head; }public void setContent_child(String content_child) {this.content_child = content_child; }public void setContent(String content) {this.content = content; }public void setName(String name) {this.name = name; }public String getContent_child() {return content_child; }public String getContent() {return content; }public String getName() {return name; }}
第三部:编写adapter页面
/**用于 * Created by Administrator on 2016/9/4. *十八章的重点:将超链接转换成不是超链接的数据,并且解析,得到你想要的数据 * holder.txt_wb_item_from.setMovementMethod(LinkMovementMethod.getInstance()); * holder.txt_wb_item_from.setText("来着:"+Html.fromHtml(s.getSource())); */
package Adapter; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import java.util.List; import Bean.HomeMsgBean; import app.coolweather.com.weibo.R;
public class HomeMsgAdapter extends BaseAdapter {private Context context; private List<HomeMsgBean> list; public HomeMsgAdapter(Context context,List<HomeMsgBean> list){this.context=context; this.list=list; }@Override public int getCount() {return list==null?0:list.size(); }@Override public Object getItem(int position) {return list==null?null:list.get(position); }@Override public long getItemId(int position) {return position; }@Override public View getView(int position, View convertView, ViewGroup parent) {View view = convertView; if(view==null){Holder holder=new Holder(); view = LayoutInflater.from(context).inflate(R.layout.msg_write_layout, null);//加载布局文件,该布局文件为listview中的文件,返回v对象,即自己的模板文件 //创建对象 holder.user_content_textView= (TextView) view.findViewById(R.id.user_content); holder.user_head= (ImageView) view.findViewById(R.id.user_head); holder.commont_head=(ImageView)view.findViewById(R.id.user_head_one); holder.user_time=(TextView)view.findViewById(R.id.time_put); holder.commont_child=(TextView)view.findViewById(R.id.user_content_one); holder.commont_head_two=(ImageView)view.findViewById(R.id.user_head_two); //给各个对象赋值 holder.user_content_textView.setText(list.get(position).getName()); holder.user_head.setImageResource(list.get(position).getHead()); //setImageResource()中间填写R.drawable.picture holder.commont_head.setImageResource(list.get(position).getCommont()); holder.user_time.setText(list.get(position).getContent()); holder.commont_child.setText(list.get(position).getContent_child()); holder.commont_head_two.setImageResource(list.get(position).getCommont_head()); //setImageResource()中间填写R.drawable.picture }return view; }//静态类用于声明各个控件的对象,实现性能的优化 private static class Holder{TextView user_content_textView; ImageView user_head; ImageView commont_head; TextView user_time; TextView commont_child; ImageView commont_head_two; } }
第四部:
1.创建Adapter中使用的list对象,创建lisview对象
private List<HomeMsgBean> list=new ArrayList<HomeMsgBean>();
listView= (ListView) findViewById(R.id.list_view_home);
2.给adapter赋值
public void initAdapter(){HomeMsgBean homeMsgBean_1=new HomeMsgBean("壬戌之秋,七月既望,苏子与客泛舟游于赤壁之下","9月1日","青青园中葵,朝露待日晞",R.drawable.time,R.drawable.time_1,R.drawable.time_3); list.add(homeMsgBean_1); HomeMsgBean homeMsgBean_2=new HomeMsgBean("清风徐来,水波不兴。举酒属客,诵明月之诗,歌窈窕之章","9月2日","阳春布德泽,万物生光辉",R.drawable.time,R.drawable.time_2,R.drawable.time_4); list.add(homeMsgBean_2); HomeMsgBean homeMsgBean_3=new HomeMsgBean("少焉,月出于东山之上,徘徊于斗牛之间","9月3日","常恐秋节至,焜黄华叶衰",R.drawable.time,R.drawable.time_3,R.drawable.time_2); list.add(homeMsgBean_3); HomeMsgBean homeMsgBean_4=new HomeMsgBean("白露横江,水光接天。纵一苇之所如,凌万顷之茫然","9月4日","常恐秋节至,焜黄华叶衰",R.drawable.time,R.drawable.time_4,R.drawable.time_1); list.add(homeMsgBean_4); }
3.加载适配器
@Override public void refresh(Object... params) {progressView.setVisibility(View.GONE); //因为这里是整个控件包括progress和text所以用setVisibility,GONE为影藏,V为可见,INV为不可见 //加载适配器和Adapter HomeMsgAdapter homeMsgAdapter=new HomeMsgAdapter(this,list); listView.setAdapter(homeMsgAdapter);
最终效果:
18.Listview结合SimplterAdapter最全面的使用相关推荐
- Android攻城狮ListView
适配器: 数据适配器:把复杂的数据填充在指定的视图界面上ArrayAdapter 数组适配器 用于绑定格式单一的数据 数据源:可以是集合或数组 SimpleAdapter 简单适配器:用于绑定格式复杂 ...
- android 简易时间轴(实质是ListView)
ListView的应用 1.在很多时候是要用到时间轴的,有些处理的时间轴比较复杂,这里就给出一个比较简单的时间轴,其实就是ListView里面的Item的设计. 直接上代码: ListView,ite ...
- 使用Google 官方的控件SwipeRefreshLayout实现下拉刷新功能
之前做东西的时候,经常会用到下拉刷新的功能,之前大家都在使用Github上的一个很著名的开源项目 PullToRefresh 但是,现在好消息来了,google在19.1版本的support-v4兼容 ...
- 18、ListView显示图片
[size=medium] ListView显示图片(只有文字显得很傻),很多了,效果图就不找了,这里主要是网络图片,利用上节网络图片获取的类. 先定义了一个类,ModelList,主要是存放List ...
- ListView style
步骤一:在使用的ListView的activiey里使用android:theme="@style/Theme的名字" 步骤二:创建Themes.xml 在Themes.xml里定 ...
- Android使用ListView控件问题
Android使用ListView控件问题: The application has stopped unexpectedly, please try again. 开发环境:android 1.6 ...
- 在winform中从外部拖动节点到树形结构(treeview和listview相互拖动)(一)
最近一个项目要用到从listview向treeview拖动item,达到从外部拖动图标成为树形结构的一部分,通过查阅资料总结了一些实现方式,分享给大家.这是winform中的例子. 在进行拖放操作之前 ...
- Dictionary作为数据源绑定,调用c++库中返回为BYTE*的函数,listView项排序
最近在做一个电子档案管理的项目.现在还处于初期,只是做一个简单demo拿去跟客户演示.至于最后谈不谈得下来,到底做不做,反正我是不看好,但没因为这样就马马虎虎.草草了事.这个项目算是b/s加c/s混合 ...
- android 在 ListView 的 item 中插入 GridView 仿微信朋友圈图片显示。
转载请声明出处(http://www.cnblogs.com/linguanh/) 先上张效果图: 1,思路简述 这个肯定是要重写 baseAdapter的了,这里我分了两个数据适配器,一个是自定义的 ...
最新文章
- PHP中可变变量和php可变函数到底有什么用?
- Content-Disposition
- 关于规则引擎一些基本理论的积累
- mybatis 取list第一个_Mybatis(五)- 多对一
- tkmybatis 子查询_日均20亿流量:携程机票查询系统的架构升级
- ArcSDE for SQL Server安装及在ArcMap中创建ArcSDE连接
- 无法显示添加端口对话框 服务器,服务器添加开放端口
- R语言中的数据处理包dplyr、tidyr笔记
- Sentry的安装搭建与使用
- Dynamic Rankings(整体二分)
- python交通流预测算法_基于遗传算法优化LSTM神经网络的交通流预测方法与流程...
- 如何制做计算机病毒,电脑病毒制作-怎么制作电脑病毒请教高手,怎么做病毒? – 手机爱问...
- 条码专题--条码技术应用
- C语言pthread.h运用
- 2020网易游戏测试面试二面面经(上海春招)
- 如何禁用电脑文件共享
- arxiv202210 | cTransformer:基于Transformer的De Novo Molecular Design生成模型
- 【无标题】西门子smart触摸屏连接1200PLC
- 机器学习-GridSearchCV scoring 参数设置!
- 解读:大硕德乡饮耆宾贠尚忠翁芳行碑