唉 调皮的ListView

本次任务是

运用LisTView和自定义Adapter

来实现资料以列表的形式展现

来看代码


布局代码老规矩 直接贴上

   <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/show"
android:orientation="vertical"> </LinearLayout>
<ImageView
android:id="@+id/list_img"
android:src="@drawable/img1"
android:layout_width="76dp"
android:layout_height="89dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:id="@+id/list_name"
android:hint="name"
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <TextView
android:id="@+id/list_age"
android:hint="age"
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/list_hobby"
android:hint="hobby"
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout>
<ListView
android:id="@+id/list_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>

以上是三个界面的布局代码

布局代码都长得差不多 也就不上图了 啦啦啦


然后是主界面的Java代码

来看看Java代码


将对象的属性进行封装,给每个属性设置getter/setter方法 public class NameInfo {
private String name;
private String age;
private String hobby;
private int imgId; public NameInfo( int imgId,String name, String age, String hobby) {
this.name = name;
this.age = age;
this.hobby = hobby;
this.imgId = imgId;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} ......等等 同上种方法
    public InfoAdapter(Context context, List datas) {
this.datas = datas;
this.context = context;
}
@Override
public int getCount() {
return datas.size();//一定要返回list长度!!!!
} @Override
public Object getItem(int i) {
return datas.get(i);
} @Override
public long getItemId(int i) {
return i;
} @Override
public View getView(int i, View view, ViewGroup viewGroup) {
if(view == null){
view = LayoutInflater.from(context).inflate(R.layout.item,null);
} ImageView img = (ImageView)view.findViewById(R.id.list_img);
TextView name =(TextView) view.findViewById(R.id.list_name);
TextView age = (TextView)view.findViewById(R.id.list_age);
TextView hobby = (TextView)view.findViewById(R.id.list_hobby); NameInfo nameminfo = datas.get(i);
img.setImageResource(nameinfo.getImgId());
name.setText(nameinfo.getName());
age.setText(nameinfo.getAge());
hobby.setText(nameinfo.getHobby()); return view;
}
    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager manager = getFragmentManager() ;
transction = manager.beginTransaction();
if(adapterFragment ==null){
adapterFragment = new AdapterFragment();
transction.add(R.id.show,adapterFragment);
}
transction.replace(R.id.show,adapterFragment);
transction.commit();
}

上面这段代码 有点难 Java里的代码每次都折磨我老半天

可能我还有些驾驭不了它

一定要想办法搞定他...

    @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.fragment_adapter,container,false);
List<NameInfo> datas = new ArrayList<>();
datas.add(new NameInfo(R.drawable.img1,"杨洋","25","羊毛"));
datas.add(new NameInfoe(R.drawable.img2,"黄子韬","24","羊毛"));
datas.add(new NameInfo(R.drawable.img3,"郑爽","25","羊毛"));
datas.add(new NameInfo(R.drawable.img4,"白敬亭","23","羊毛"));
datas.add(new NameInfo(R.drawable.img5,"赵丽颖","28","羊毛")); InfoAdapter adapter = new InfoAdapter(getActivity(), datas); ListView listView = (ListView) view.findViewById(R.id.list_view);
listView.setAdapter(adapter);
return view;
}

运行结果图如下

唉 调皮的ListView的更多相关文章

  1. 初探ListView

    ListView可能是Android开发中最常用的一个控件,但要用的纯熟还需要不断的锻炼. 建立简单的ListView 1.在布局文件(.xml)中添加<ListView>标签 2.在Ma ...

  2. 张高兴的 UWP 开发笔记:横向 ListView

    ListView 默认的排列方向是纵向 ( Orientation="Vertical" ) ,但如果我们需要横向显示的 ListView 怎么办? Blend for Visua ...

  3. Android—万能ListView适配器

    ListView是开发中最常用的控件了,但是总是会写重复的代码,浪费时间又没有意义. 最近参考一些资料,发现一个万能ListView适配器,代码量少,节省时间,总结一下分享给大家. 首先有一个自定义的 ...

  4. Android—ListView条目背景为图片时,条目间距问题解决

    ListView是android开发中使用最普遍的控件了,可有的listView条目的内容颇为丰富,甚至为了美观,背景用指定图片,如下图:

  5. Android中ListView实现图文并列并且自定义分割线(完善仿微信APP)

    昨天的(今天凌晨)的博文<Android中Fragment和ViewPager那点事儿>中,我们通过使用Fragment和ViewPager模仿实现了微信的布局框架.今天我们来通过使用Li ...

  6. listview下拉刷新和上拉加载更多的多种实现方案

    listview经常结合下来刷新和上拉加载更多使用,本文总结了三种常用到的方案分别作出说明. 方案一:添加头布局和脚布局        android系统为listview提供了addfootview ...

  7. Android listview和gridview以及view的区别

    GridView 可以指定显示的条目的列数. listview一般显示的条目的列数都是一列 如果是列表(单列多行形式)的使用ListView,如果是多行多列网状形式的优先使用GridView andr ...

  8. mono for android Listview 里面按钮 view Button click 注册方法 并且传值给其他Activity 主要是context

    需求:为Listview的Item里面的按钮Button添加一个事件,单击按钮时通过事件传值并跳转到新的页面. 环境:mono 效果: 布局代码 主布局 <?xml version=" ...

  9. 【腾讯Bugly干货分享】跨平台 ListView 性能优化

    本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:https://mp.weixin.qq.com/s/FbiSLPxFdGqJ00WgpJ94yw 导语 精 ...

随机推荐

  1. 剑指offer——包含min函数的栈

    题目:定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度为O(1)) 该题是自己第一次采用编程的方式来实现Java中栈的功能,故直接借鉴了大牛的代码 import ...

  2. [Linux]系统管理: 进程管理(ps/top/pstree/kill/pkill), 工作管理, 系统资源查看, 系统定时任务

    进程管理:查看与终止 进程查看 1. 进程是正在执行的程序或命令. 2. 进程管理的作用: 判断服务器健康状态, 查看系统中所有进程 杀死进程 3. 查看系统中所有进程 ps aux    # 查看系 ...

  3. python os.path.isfile函数

    最近刚开始学习Python,做了个小练习:扫描当前目录及其子目录中的文件,找出文件名中含有指定关键字的文件并打印文件名.思路很简单,如果是文件则判断是否满足条件:如果是目录则进入目录搜索文件,递归. ...

  4. linux 查看文件方法

    vi 文件名 #编辑方式查看,可修改cat 文件名 #显示全部文件内容more 文件名 #分页显示文件内容tail 文件名 #仅查看尾部,还可以指定行数head 文件名 #仅查看头部,还可以指定行数s ...

  5. Win10 远程桌面连接出现“要求的函数不受支持”的解决办法之修改注册表

    问题起因 笔者自己在阿里云上搞服务器,有一台 Windows Server 必须通过远程桌面连接来管理,由于没能完全关掉 Win10 自带的烦人的系统更新,导致昨天安装完更新后出现了连接远程桌面时“要 ...

  6. 二十二、Command 命令模式

    原理: 时序图: 代码清单: command.Command public interface Command { void execute(); } command.MacroCommand pub ...

  7. sv函数中返回队列

    如果想从函数中,返回队列或者动态数组,我们应该怎么做呢? 答案就是自己用typedef定义一个类型. typedef int queue_of_int[$]; function queue_of_in ...

  8. docker面试题集

    Docker的应用场景 Web 应用的自动化打包和发布. 自动化测试和持续集成.发布. 在服务型环境中部署和调整数据库或其他的后台应用. 从头编译或者扩展现有的OpenShift或Cloud Foun ...

  9. mysql 文件

    慢查询日志 log_query_time 查询时间超过这个值则会出现在慢查询日志中,默认值是10 log_slow_queries  是否开启慢查询 log_queries_not_using_ind ...

  10. 20175126《Java程序设计》第四周学习总结

    # 20175126 2016-2017-2 <Java程序设计>第四周学习总结 ## 教材学习内容总结 - 本周学习方式主要为手动敲打教材代码和观看APP上的视频资源自学. - 学习内容 ...