检查listView项的类型

时间:2016-03-22 03:41:34

标签: android listview

我创建了joblog作为listView,并且在listView的每一行中都可以 文字或图像。 我需要检查是否有图像或文本来指定点击事件。

 jobLogListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(final AdapterView<?> parent, View view,final int position, final long id) {
                PopupMenu popup = new PopupMenu(getActivity().getApplicationContext(), jobLogListView);
                popup.getMenuInflater().inflate(R.menu.menu_joblog_share, popup.getMenu());
                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

                    JobLog jobLogItem = (JobLog) parent.getItemAtPosition(position);
                    public boolean onMenuItemClick(MenuItem item) {
                        switch (item.getItemId()) {
                            case R.id.copy_text:
                                copyToClipBoard();
                                break;
                            case R.id.share_text:
                                shareToOtherApp();
                                break;

                            default:
                                break;

                        }
                        return true;
                    }
                });
                popup.show();
                return true;
            }
        });

2 个答案:

答案 0 :(得分:0)

捕获onClick事件有一个很好的技巧,对于ListView中的任何任意视图 - 所以你知道点击了哪个特定的视图,例如TextView或ImageView。虽然它需要扩展BaseAdapter类。

如果你扩展BaseAdapter,一个简单的覆盖(没有视图回收)将如下:

@Override
public View getView(final int position, View convertView, final ViewGroup parent{
  if (convertView==null)  convertView=mLayoutInflator.inflate(R.layout.mylistitem, null)
  //first, create an onclick listener that we can later access from our Activity:
  View.OnClickListener clickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v){
      ((ListView) parent).performItemClick(v, position, 0);
      }
    }
  //then, assign the onClick Listener to all our Views within the ListView...
  (TextView) convertView.findViewById(R.id.mytextview).setOnClickListener(clickListener);
  (ImageView) convertView.findViewById(R.id.myimageview).setOnClickListener(clickListener);
  //repeat for every View you want to capture clicks for...
}

然后在Activity中,您可以访问列表视图中每个项目的onClick,以便根据用户点击的视图更改操作:

ListView lv = (ListView) findViewById(R.id.mylistview);
lv.setOnItemClickListener(new AdapaterView.OnItemClickListener(){
  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position, longid){
    if (View.getId()==R.id.mytextview) {
      //do stuff if textview was clicked 
    } else if (View.getId()==R.id.myimageview) {
      //do stuff if imageview was clicked
    }
  }
});

答案 1 :(得分:-2)

您可能会发现此链接以及本页顶部的答案有用 -

https://www.caveofprogramming.com/guest-posts/custom-listview-with-imageview-and-textview-in-android.html

代码段:

@覆盖     public View getView(final int position,View convertView,ViewGroup parent){         // TODO自动生成的方法存根         持有人=新持有人();         查看rowView;
             rowView = inflater.inflate(R.layout.program_list,null);              holder.tv =(TextView)rowView.findViewById(R.id.textView1);              holder.img =(ImageView)rowView.findViewById(R.id.imageView1);
         holder.tv.setText(结果[位置]);          holder.img.setImageResource(图像标识[位置]);
         rowView.setOnClickListener(new OnClickListener(){
            @覆盖             public void onClick(查看v){                 // TODO自动生成的方法存根                 Toast.makeText(上下文,“你点击”+结果[位置],Toast.LENGTH_LONG)。show();

             if (v.getId() == R.id.image){
               // do something.
             } else if (v.getId() == R.id.textView){
               // do something else.
             }

        }
    });   
    return rowView;