博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用BaseAdapter实现酷我音乐中点击ListView的弹出效果
阅读量:5011 次
发布时间:2019-06-12

本文共 3656 字,大约阅读时间需要 12 分钟。

1.main.xml

 2.list.xml

 3.MainActivity代码

package com.haitun.baseadaptertest;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import android.os.Bundle;import android.app.Activity;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Button;import android.widget.LinearLayout;import android.widget.ListView;import android.widget.TextView;public class MainActivity extends Activity {	private ListView listView=null;	private List
> listMap=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listView=(ListView)findViewById(R.id.listView); listMap=new ArrayList
>(); HashMap
map=null; String[] userName={"user1","user2","user3"}; String[] listText={"HelloAPPPPPPfabNifadg migm afagaegegagege1","HelloAPPPPPPfabNifadg migm afagaegegagege2","HelloAPPPPPPfabNifadg migm afagaegegagege3"}; for(int i=0;i
(); map.put("userName",userName[i]); map.put("text",listText[i]); listMap.add(map); } MyAdapter myAdapter=new MyAdapter(this);// myAdapter.notifyDataSetChanged(); listView.setAdapter(myAdapter); } public final class ViewHolder{ public TextView tv; public Button mButton; public LinearLayout linearLayout; public TextView userText; } private ViewHolder holder=null; class MyAdapter extends BaseAdapter{ private LayoutInflater mInflater=null; public MyAdapter(Context context){ this.mInflater=LayoutInflater.from(context); } private List
holderList=new ArrayList
(); @Override public int getCount() { // TODO Auto-generated method stub return listMap.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub if(convertView==null){ holder=new ViewHolder(); holderList.add(position, holder); //放倒队列中进行缓存 convertView=mInflater.inflate(R.layout.list,null); holder.tv=(TextView)convertView.findViewById(R.id.userName); holder.mButton=(Button)convertView.findViewById(R.id.mButton); holder.linearLayout=(LinearLayout)convertView.findViewById(R.id.info); holder.userText=(TextView)convertView.findViewById(R.id.tv); convertView.setTag(holder); }else{ holder=(ViewHolder)convertView.getTag(); } //数据和控件的匹配 holder.tv.setText((String)listMap.get(position).get("userName")); holder.userText.setText((String)listMap.get(position).get("text")); Listener listener=new Listener(position); //自定义监听器,把position参数传进去 holder.mButton.setOnClickListener(listener); return convertView; } private boolean bchange=false; private ViewHolder viewHolder=null; //用户缓存用户的ViewHolder对象 class Listener implements OnClickListener{ private int position; public Listener(int position){ this.position=position; } @Override public void onClick(View v) { // TODO Auto-generated method stub viewHolder=(ViewHolder)holderList.get(position+1); if(bchange){ bchange=false; viewHolder.linearLayout.setVisibility(LinearLayout.GONE); }else{ bchange=true; viewHolder.linearLayout.setVisibility(LinearLayout.VISIBLE); } listView.setSelection(position); //显示ListView的最后一行 } } } }

 

转载于:https://www.cnblogs.com/haitunzhulang/p/3181035.html

你可能感兴趣的文章
数据加密相关
查看>>
MySQL基础命令
查看>>
mysql添加外键约束变为索引
查看>>
wpf中dropdownButton控件下拉居中。。。
查看>>
[bzoj1251]序列终结者
查看>>
【Python3_进阶系列_009】Python3-迭代器
查看>>
14款下载有用脚本的超酷网站
查看>>
QT进行多传感器(执行器)的编程框架
查看>>
Debug目录、Release目录,bin目录、obj目录,vshost.exe.config文件、.exe.config文件分析【C#】...
查看>>
Swift项目兼容Objective-C问题汇总
查看>>
NuttX 介绍
查看>>
【七】注入框架RoboGuice使用:(Your First Custom Binding)
查看>>
LXC-Linux Containers介绍
查看>>
7.31实习培训日志-docker sql
查看>>
c#中使用servicestackredis操作redis
查看>>
ios app 真机crash报告分析
查看>>
CRC标准以及简记式
查看>>
SEO搜索引擎
查看>>
关于本地使用tomcat部署web应用,浏览器自动跳转为https的问题
查看>>
一、Text To Speech
查看>>