-
安卓之高级控件Listview的优化
XML代码(activity_main)如下:
[html] view plain copy
-
- android:orientation="vertical" android:layout_width="match_parent"
- android:layout_height="match_parent">
-
-
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/lv_main_list">
-
XML代码(itemtag)如下(用来优化的):
[html] view plain copy
-
- android:orientation="horizontal"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:descendantFocusability="blocksDescendants">
-
-
-
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/iv_main_image"/>
-
-
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:id="@+id/tv_main_word"/>
-
-
JAVA代码如下(ItemTag):
可以把它想象成一个实体类
[html] view plain copy
- public class ItemTag {
- public ImageView imageView;
- public TextView textView;
- public Button button;
- }
Java代码如下(MainActivity):
[html] view plain copy
- public class MainActivity extends AppCompatActivity {
-
- private ListView lv_main_list;
- private int images[]={R.drawable.bird,R.drawable.cat,R.drawable.chicken,R.drawable.duck,R.drawable.elephant};
- private String titles[]={"小鸟","猫咪","小鸡","鸭子","大象"};
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- lv_main_list = (ListView) findViewById(R.id.lv_main_list);
- lv_main_list.setAdapter(new MyAdapter());
-
- lv_main_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView> parent, View view, int position, long id) {
- Toast.makeText(MainActivity.this, "跳转到"+titles[position]+"页面", Toast.LENGTH_SHORT).show();
- }
- });
- }
-
- class MyAdapter extends BaseAdapter{
-
- @Override
- public int getCount() {
- return titles.length;
- }
-
- @Override
- public Object getItem(int position) {
- return titles[position];
- }
-
- @Override
- public long getItemId(int position) {
- return position;
- }
-
- @Override
- public View getView(final int position, View convertView, ViewGroup parent) {
- if(convertView==null){
- convertView=getLayoutInflater().inflate(R.layout.itemtag,null);
- ItemTag itemTag=new ItemTag();
- itemTag.imageView= (ImageView) convertView.findViewById(R.id.iv_main_image);
- itemTag.textView= (TextView) convertView.findViewById(R.id.tv_main_word);
- itemTag.button = (Button) convertView.findViewById(R.id.bu_main_bu);
- convertView.setTag(itemTag);
- }
-
- ItemTag itemTag= (ItemTag) convertView.getTag();
- itemTag.imageView.setImageResource(images[position]);
- itemTag.textView.setText(titles[position]);
- itemTag.button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast.makeText(MainActivity.this, "正在下载"+titles[position], Toast.LENGTH_SHORT).show();
- }
- });
-
- return convertView;
- }
- }
- }
-
相关阅读:
输电线路巡视无人机故障坠落防护装置:守护飞行安全的防线
Zookeeper高级特性与API
SentenceTransformer 之论文解读
YOLOv5 加入SE注意力机制
【Vue2.x源码系列06】计算属性computed原理
Java、Spring、Dubbo三者SPI机制原理与区别
如何在指定文件夹下安装python的虚拟环境
虹科小课堂 | 在各种恶劣环境下,光纤传感器毫不畏惧、C位出道!
word文档怎么转换成pdf?几个实用文档转换方法
字符串专项练习
-
原文地址:https://blog.csdn.net/liuliuhelingdao/article/details/127141773