Android高级控件系列之ListView的用法
来源:互联网 发布:思科通过mac地址查询ip 编辑:程序博客网 时间:2024/05/21 02:34
效果图:
布局代码:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.zking.laci.android08.MainActivity"> <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/lv_list" android:layout_weight="1" ></ListView></LinearLayout>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:descendantFocusability="blocksDescendants" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/res_iv" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/res_tv" android:text="xx" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下载" android:id="@+id/res_bt" /></LinearLayout>
注意:如果 不写android:descendantFocusability="blocksDescendants"的话,就会有文字点击没有效果了
Java代码:
ItemTag:
package com.zking.laci.android08;import android.widget.Button;import android.widget.ImageView;import android.widget.TextView;/** * Created by Laci on 2017/6/9. */public class ItemTag { public ImageView iv; public TextView tv; public Button bt;}
package com.zking.laci.android08;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;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.SimpleAdapter;import android.widget.TextView;import android.widget.Toast;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;public class MainActivity extends AppCompatActivity { private ListView lv; private int images[]={R.drawable.bird,R.drawable.cat,R.drawable.chicken}; private String titles[]={"小鸟","猫猫","火鸡"}; private Button bt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv= (ListView) findViewById(R.id.lv_list); lv.setAdapter(new MyAdapter()); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, "跳转到"+titles[position%titles.length], Toast.LENGTH_SHORT).show(); } }); } class MyAdapter extends BaseAdapter{ //指定的view有多少个 @Override public int getCount() { return 1000; } //内容 @Override public Object getItem(int position) { return titles[position%titles.length]; } //每一行的下标 @Override public long getItemId(int position) { return position; } //拿到每一行的view @Override public View getView(final int position, View convertView, ViewGroup parent) { //把布局文件转成view if(convertView==null){ convertView=getLayoutInflater().inflate(R.layout.res,null); //实例化自己写的实体类 ItemTag it=new ItemTag(); it.iv= (ImageView) convertView.findViewById(R.id.res_iv); it.tv= (TextView) convertView.findViewById(R.id.res_tv); it.bt= (Button) convertView.findViewById(R.id.res_bt); convertView.setTag(it); } ItemTag itg= (ItemTag) convertView.getTag(); //设置 itg.iv.setImageResource(images[position%titles.length]); itg.tv.setText(titles[position%titles.length]); itg.bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "状态下载"+titles[position%titles.length], Toast.LENGTH_SHORT).show(); } }); return convertView; } }}
阅读全文
0 0
- Android高级控件系列之ListView的用法
- Android高级控件之ListView
- Android高级控件之ExpandableListView可以展开的listview
- android高级控件ListView
- ListView android高级控件
- android高级控件-ListView
- Android高级控件系列四之RecyclerView的使用
- Android高级控件系列六之ViewPager的使用
- android listview高级用法
- Android高级控件系列八之碎片
- Android列表用法之二:实战ListView高级用法
- Android 控件 ListView 用法
- android listView控件用法
- Android高级控件系列二之第三方控件PullToRefreshListView下拉刷新的使用
- Android高级控件之AutoCompleteTextView,MultiAutopleteTextView,Spinner,ListView,ExpandableListView
- Android高级控件之ListView的优化以及下拉刷新页面
- 【Android基本控件】ListView 的用法
- 【Android】41、ListView控件的简单用法
- iOS开发生存技能:学会正确阅读Xcode文档
- iOS-多线程编程之GCD
- C++中string和int的互相转化方式
- java.lang.RuntimeException: Invalid action class configuration that references an unknown class name
- 初识JavaScript(基础篇)
- Android高级控件系列之ListView的用法
- SparkSQL下Parquet中PushDown的实现
- 美团-大众点评-春招面经
- 基于Qt的图像处理技术和算法
- java语言基础(61)——java System类的使用
- 欢迎使用CSDN-markdown编辑器 使用方法
- 5-23 还原二叉树 (25分)
- disable back button of browsers 禁用浏览器后退键
- Tools