Android listview子控件的的点击事件

1.先看图,是否是你想要的

                                       


2.布局文件
xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lin_img"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:descendantFocusability="blocksDescendants"----------------//加上这行
    >
  
        android:id="@+id/hot_listview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:cacheColorHint="#00000000"
    android:listSelector="#00000000"
    >



  descendantFocusability属性的值有三种:

beforeDescendants:viewgroup会优先其子类控件而获取到焦点

      afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点

      blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点

子控件设置:android:focusable="false"

 
  


3.adapter类

public class Videoadapter extends BaseAdapter{
ArrayList mList ;
Context context ; ViewHolder viewHolder= null;

/** 监听对象 */

private MyClickListener mListener ;
public
Videoadapter(ArrayList list ,Context context){
this. mList=list ;
this
. context=context ;
}

public void setOnClickListener( MyClickListener listener) {

mListener = listener ;

}


@Override
public int getCount() {
return mList== null? 0: mList.size() ;

}
 
@Override

public Object getItem( int position) {
return mList== null? 0: mList.get(position) ;
}

@Override
public long getItemId( int position) {
return mList== null? 0:position ;
}

@Override
public View getView( int position , View convertView , ViewGroup parent) {
if(convertView== null){
viewHolder= new ViewHolder() ;
//得到布局
convertView= LayoutInflater. from( context).inflate(R.layout. content_list ,null) ;
//得到控件
viewHolder. img_head= (ImageView) convertView.findViewById(R.id. userhead) ;
viewHolder. img_video= (ImageView) convertView.findViewById(R.id. img_video) ;
viewHolder. img_like= (ImageView) convertView.findViewById(R.id. img_like) ;
viewHolder. txt_uname= (TextView) convertView.findViewById(R.id. username) ;
viewHolder. img_addreess= (ImageView) convertView.findViewById(R.id. address) ;
viewHolder. txt_like= (TextView) convertView.findViewById(R.id. txt_like) ;
viewHolder. txt_address= (TextView) convertView.findViewById(R.id. txt_address) ;
viewHolder. img_comment= (ImageView) convertView.findViewById(R.id. img_comment) ;
viewHolder. txt_comment= (TextView) convertView.findViewById(R.id. txt_comment) ;
viewHolder. btn_attention= (Button) convertView.findViewById(R.id. attention) ;


//设置头像的监听事件
viewHolder. img_head.setOnClickListener( mOnClickListener) ;

//设置播放视屏的监听事件
viewHolder. img_video.setOnClickListener( mOnClickListener) ;

//设置图片点赞的监听事件
viewHolder. img_like.setOnClickListener( mOnClickListener) ;

//设置text的监听事件
viewHolder. txt_like.setOnClickListener( mOnClickListener) ;

//设置点击评论图片的监听事件
viewHolder. img_comment.setOnClickListener( mOnClickListener) ;

//设置点击评论文字的监听事件
viewHolder. txt_comment.setOnClickListener( mOnClickListener) ;

convertView.setTag( viewHolder) ;

} else{
viewHolder= (ViewHolder) convertView.getTag() ;
}


//设置postition

viewHolder. img_head.setTag(position) ;
viewHolder. img_video.setTag(position) ;
viewHolder. img_like.setTag(position) ;
viewHolder. txt_like.setTag(position) ;
viewHolder. img_comment.setTag(position) ;
viewHolder. txt_comment.setTag(position) ;


//设置值

viewHolder. img_head.setImageResource(R.mipmap. head) ;
 
  
 
  
 
  


        ---------------------------初始化ImageLoader--------------------------------

//显示图片的配置(使用ImagerLoader必须先初始化,最好放在Application里面,避免代码冗余)
       // 创建默认的ImageLoader配置参数
        ImageLoaderConfiguration configuration = ImageLoaderConfiguration
        .createDefault(this);

        ImageLoader.getInstance().init(configuration);

        ---------------------------初始化ImageLoader--------------------------------



//加载网络图片
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheInMemory( true).cacheOnDisk( true)
.bitmapConfig(Bitmap.Config. RGB_565).build() ;

ImageLoader. getInstance().displayImage( mList.get(position).getImgUrl() , viewHolder. img_video , options) ;


 
viewHolder. txt_uname.setText( mList.get(position).getUserID()) ;
viewHolder. img_addreess.setImageResource(R.mipmap. china) ;
viewHolder. txt_like.setText( mList.get(position).getLikes() + "") ;
viewHolder. img_comment.setImageResource(R.mipmap. comment) ;
viewHolder. txt_comment.setText( R.string.comment) ;
viewHolder
. txt_address.setText( R.string.address) ;

return convertView ;

}


class ViewHolder{
private ImageView img_head , img_video , img_like , img_comment , img_addreess ;
private
TextView txt_uname , txt_like , txt_comment , txt_address ;
private
Button btn_attention ;
}


   //实现点击事件
private View.OnClickListener mOnClickListener= new View.OnClickListener() {
@Override
public void onClick(View v) {
if ( mListener != null) {
int position = (Integer) v.getTag() ;
switch
(v.getId()) {
case
R.id. img_video:
mListener.onPlayVideo(Videoadapter. this,v ,position) ;
break;

case
R.id. img_like:
mListener.onSetLike(Videoadapter. this, v , position) ;
break;
case
R.id. txt_like:
                       mListener.onSetLike(Videoadapter.this, v, position);                       
                       break;
                case  R.id.txt_like:
                       mListener.onQueryLikeInfo(Videoadapter.this, v, position);
                       break;
                case R.id.img_comment:
                       mListener.onQueryCommentorAddComment(Videoadapter.this, v, position);
                       break;
                case R.id.txt_comment:
                       mListener.onQueryCommentorAddComment(Videoadapter.this,v,position);
                       break;
}
}
}
};


//自定义接口类,方便Activity调用
public  interface  MyClickListener {
    public void  onPlayVideo(BaseAdapter adapter, View view, int position);
    public void  onSetLike(BaseAdapter adapter, View view, int position,Boolean islike);
    public void  onQueryLikeInfo(BaseAdapter adapter, View view, int position);
    public void  onQueryCommentorAddComment(BaseAdapter adapter, View view, int position);

}


4.activity类
public class DiscoverActivity extends Activity {

ArrayList mList;
Videoadapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.discover_activity);
        initView();
    }

   public void initView(){
       videolist = new ArrayList();
 
  
 
  
 
  
 
  
       adapter = new Videoadapter(videolist, getActivity());
       hotListview.setAdapter(adapter);
}

//实现adapter中的接口调用
  adapter.setOnClickListener(new Videoadapter.OnClickListener() {
            @Override
            public void onPlayVideo(BaseAdapter adapter, View view, int position) {
             
            }

            @Override
            public void onSetLike(BaseAdapter adapter, View view, int position) {
               

            }

            @Override
            public void onQueryLikeInfo(BaseAdapter adapter, View view, int position) {

            }


            @Override
            public void onQueryCommentorAddComment(BaseAdapter adapter, View view, int position) {
  
            }
        });

}

到此这篇文章就结束了,希望能帮助各位,每天在工作中学习,在学习中进步,在进步中完成工作,每天迈出一小步,
一年跨出一大步。


你可能感兴趣的:(Android listview子控件的的点击事件)