ViewPager中控件的点击事件监听

问题:当我们在做ViewPager加载多个布局文件时,特别是加载多个一样的布局文件,这些布局中的控件点击事件却监听不到了,这该怎么解决呢。

其实我们加载多个布局那么就应该找到对应布局中的控件,也就是那个List中获取对应的布局文件,再从这个布局文件中获取对应的控件,具体在代码中可以在主页面中完成,也可以在适配器中完成,这里只给出适配器中的解决:

在PagerAdapter中的instantiateItem方法中,如下直接将我的工程中的部分代码拷贝出来了,里面还包括当前页面的点击监听

@Override
	public Object instantiateItem(View v, int position) 
	{
		
		switch (position) 
		{
			case 0:
				
				views.get(0).setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Log.e("", "View0");
					}
				});
				
		        /*ImageView  test01 = (ImageView)views.get(0).findViewById(R.id.iv_friend_adapter_head);
		        test01.setOnClickListener(new OnClickListener()
		        {
			        @Override
			        public void onClick(View v) 
			        {
			        	Log.e("", "ImageView0");
			        }
		        });
		
		        TextView test02 = (TextView)views.get(0).findViewById(R.id.tv_friend_adapter_username);
		        test02.setOnClickListener(new OnClickListener()
		        {
		        	@Override
		        	public void onClick(View v)
		        	{
		        		Log.e("", "TextView0");
		        	}
		        });*/
	            break;
			case 1:
				
				views.get(1).setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Log.e("", "View1");
					}
				});
		        /*ImageView  test11 = (ImageView)views.get(1).findViewById(R.id.iv_friend_adapter_head);
		        test11.setOnClickListener(new OnClickListener()
		        {
			        @Override
			        public void onClick(View v) 
			        {
			        	Log.e("", "ImageView1");
			        }
		        });
		
		        TextView test12 = (TextView)views.get(1).findViewById(R.id.tv_friend_adapter_username);
		        test12.setOnClickListener(new OnClickListener()
		        {
		        	@Override
		        	public void onClick(View v)
		        	{
		        		Log.e("", "TextView1");
		        	}
		        });*/
	            break;
			case 2:
				views.get(2).setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Log.e("", "View2");
					}
				});
		        /*ImageView  test21 = (ImageView)views.get(2).findViewById(R.id.iv_friend_adapter_head);
		        test21.setOnClickListener(new OnClickListener()
		        {
			        @Override
			        public void onClick(View v) 
			        {
			        	Log.e("", "ImageView2");
			        }
		        });
		
		        TextView test22 = (TextView)views.get(2).findViewById(R.id.tv_friend_adapter_username);
		        test22.setOnClickListener(new OnClickListener()
		        {
		        	@Override
		        	public void onClick(View v)
		        	{
		        		Log.e("", "TextView2");
		        	}
		        });*/
			case 3:
				views.get(3).setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Log.e("", "View3");
					}
				});
		        /*ImageView  test31 = (ImageView)views.get(3).findViewById(R.id.iv_friend_adapter_head);
		        test31.setOnClickListener(new OnClickListener()
		        {
			        @Override
			        public void onClick(View v) 
			        {
			        	Log.e("", "ImageView3");
			        }
		        });
		
		        TextView test32 = (TextView)views.get(3).findViewById(R.id.tv_friend_adapter_username);
		        test32.setOnClickListener(new OnClickListener()
		        {
		        	@Override
		        	public void onClick(View v)
		        	{
		        		Log.e("", "TextView3");
		        	}
		        });*/
			case 4:
				views.get(4).setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Log.e("", "View4");
					}
				});
		        /*ImageView  test41 = (ImageView)views.get(4).findViewById(R.id.iv_friend_adapter_head);
		        test41.setOnClickListener(new OnClickListener()
		        {
			        @Override
			        public void onClick(View v) 
			        {
			        	Log.e("", "ImageView4");
			        }
		        });
		
		        TextView test42 = (TextView)views.get(4).findViewById(R.id.tv_friend_adapter_username);
		        test42.setOnClickListener(new OnClickListener()
		        {
		        	@Override
		        	public void onClick(View v)
		        	{
		        		Log.e("", "TextView4");
		        	}
		        });*/
	            break;
	        default:
	            break;
        }
		
		((ViewPager) v).addView(views.get(position));
		return views.get(position);
	}


 

你可能感兴趣的:(android界面)