android listView 控件单击事件

其实大部分内容也是从网上找的,找回之后整理了一下,记录下来给自己添记性啦!


首先:在activity文件中添加listview

main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#f0f0e0" >
    
    <RelativeLayout   
        android:id="@+id/rl_top"  
        android:layout_width="fill_parent"  
        android:layout_alignParentTop="true"  
        android:layout_height="wrap_content">  
        <TextView   
            android:layout_width="fill_parent"  
            android:layout_height="44dp"  
            android:gravity="center"  
            android:textSize="18sp"  
            android:background="#486a9a"  
            android:textColor="@android:color/white"  
            android:text="欢迎使用"/>  
    </RelativeLayout>  
    
    <ListView   
        android:id="@+id/listview"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent" 
        android:layout_below="@id/rl_top"  
        android:layout_marginLeft="10dp"  
        android:layout_marginRight="10dp"  
        android:layout_marginTop="10dp"  
        android:cacheColorHint="#00000000"  
        android:divider="@null"  
        android:listSelector="#00000000"  
        android:dividerHeight="3dp"/>  

</RelativeLayout>


然后是添加listview的布局文件

main_item.xml

?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"  
    android:orientation="vertical"  
    android:paddingBottom="5dp"  
    android:descendantFocusability="blocksDescendants"
    android:layout_height="wrap_content">
    
    <RelativeLayout  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:layout_marginTop="5dp" >
        
        <ImageView  
            android:id="@+id/iv_user_image"  
            android:layout_width="50dp"  
            android:layout_height="50dp"  
            android:layout_alignParentLeft="true"  
            android:layout_alignParentTop="true"  
            android:background="@drawable/mypic"
            /> 
        <ImageView  
            android:id="@+id/iv_run"  
            android:layout_width="50dp"  
            android:layout_height="50dp"  
            android:layout_alignParentRight="true"  
            android:layout_alignParentTop="true"  
            android:background="@drawable/run"
            /> 
        <TextView  
            android:id="@+id/tv_content"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_marginLeft="5dp"  
            android:layout_marginRight="5dp"
            android:layout_toRightOf="@+id/iv_user_image"
            android:layout_toLeftOf="@id/iv_run"  
            android:background="@drawable/chatfrom_bg"  
            android:gravity="center"  
            android:clickable="true"
            android:focusable="false"  
            android:lineSpacingExtra="2dp"  
            android:minHeight="50dp"  
            android:textColor="#ff000000"  
            android:textSize="14sp" /> 
          
        
    </RelativeLayout>  
</LinearLayout>

然后是处理代码了

mainActivity.java

定义控件:

private ListView chatListView = null;  
private List<ChatEntity> chatList = null;  
private ChatAdapter chatAdapter = null;  

添加listview项

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE); 
        setContentView(R.layout.main);
        
        chatListView = (ListView) this.findViewById(R.id.listview); 
        chatList = new ArrayList<ChatEntity>();  
        
        SetChatList();
        
        chatAdapter = new ChatAdapter(this,chatList);  
        chatListView.setAdapter(chatAdapter); 
         
    }
    
    private void SetChatList()//这个里添加列表项,也可以从数据库中插入
    {    	        
        /*chatList.add(NewChatEntity("gif.dm.activity","",true)); 
        chatList.add(NewChatEntity("gif.dm_structure.activity","",false));
        chatList.add(NewChatEntity("gif.dm_rebound.activity","",true));
        chatList.add(NewChatEntity("gif.dm_low_strain.activity","",false));
        chatList.add(NewChatEntity("gif.dm_ultrasonic.activity","",true));*/
    	chatList.add(NewChatEntity("gif.dm.activity","")); 
        chatList.add(NewChatEntity("gif.dm_structure.activity",""));
        chatList.add(NewChatEntity("gif.dm_rebound.activity",""));
        chatList.add(NewChatEntity("gif.dm_low_strain.activity",""));
        chatList.add(NewChatEntity("gif.dm_ultrasonic.activity",""));
        
    }


自定义adapter

private  class ChatAdapter extends BaseAdapter{  
        private Context context = null;  
        private List<ChatEntity> chatList = null;  
        private LayoutInflater inflater = null;  
        private int COME_MSG = 0;  
        private int TO_MSG = 1;  
          
        public ChatAdapter(Context context,List<ChatEntity> chatList){  
            this.context = context;  
            this.chatList = chatList;  
            inflater = LayoutInflater.from(this.context);  
        }  
  
        @Override  
        public int getCount() {  
            return chatList.size();  
        }  
  
        @Override  
        public Object getItem(int position) {  
            return chatList.get(position);  
        }  
  
        @Override  
        public long getItemId(int position) {  
            return position;  
        }  
          
        @Override  
        public int getItemViewType(int position) {  
            // 区别两种view的类型,标注两个不同的变量来分别表示各自的类型  
            ChatEntity entity = chatList.get(position);  
            if (entity.isComeMsg())  
            {  
                return COME_MSG;  
            }else{  
                return TO_MSG;  
            }  
        }  
  
        @Override  
        public int getViewTypeCount() {  
            // 这个方法默认返回1,如果希望listview的item都是一样的就返回1,我们这里有两种风格,返回2  
            return 2;  
        }  
  
        @Override  
        public View getView(int position, View convertView, ViewGroup parent) {  
            //ChatHolder chatHolder = null;  
        	ChatHolder1 chatHolder = null;
            if (convertView == null) {  
                chatHolder = new ChatHolder1();  
                if (chatList.get(position).isComeMsg()) {  
                    //convertView = inflater.inflate(R.layout.chat_from_item, null); 
                	convertView = inflater.inflate(R.layout.main_item, null);
                }else {  
                    //convertView = inflater.inflate(R.layout.chat_to_item, null);
                	convertView = inflater.inflate(R.layout.main_item_r, null);
                }  
                //chatHolder.timeTextView = (TextView) convertView.findViewById(R.id.tv_time);
                chatHolder.flagView = (ImageView) convertView.findViewById(R.id.iv_run);
                chatHolder.contentTextView = (TextView) convertView.findViewById(R.id.tv_content);  
                chatHolder.userImageView = (ImageView) convertView.findViewById(R.id.iv_user_image);  
                convertView.setTag(chatHolder);  
            }else {  
                //chatHolder = (ChatHolder)convertView.getTag();
            	chatHolder = (ChatHolder1)convertView.getTag();
            }  
              
            //chatHolder.timeTextView.setText(chatList.get(position).getChatTime());  
            chatHolder.contentTextView.setText(chatList.get(position).getContent());  
            chatHolder.userImageView.setImageResource(chatList.get(position).getUserImage());
            final String packageName = chatList.get(position).getAppName();
            final int pos = position;
            chatHolder.contentTextView.setOnClickListener(new OnClickListener(){//这就就是控件单击事件处理函数了!
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub					
					//Toast.makeText(context, appName.substring(0,i), Toast.LENGTH_SHORT).show();
					if (chatList.get(pos).isComeMsg())
					{
					    FrameLogic.RunActivity(packageName,context);
					    Exit();
					}
					else
					{
						new AlertDialog.Builder(context).setTitle("错误").setMessage("模块不存在,请先下载安装该模块!").show();
					}
				}            	
            });
              
            return convertView;  
        }  
    }
    private class ChatHolder{  
        private TextView timeTextView;  
        private ImageView userImageView;  
        private TextView contentTextView;  
    } 

这样,基本就可以了,但是要注意的是,非常重要的是:

listView布局文件中控件一定要加上android:focusable="false"  

然后在文件最外面,一定要加上android:descendantFocusability="blocksDescendants"

详细请看上文中的布局文件. 否则listview单击事件将会无效!


运行效果如图:






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhenye1986

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值