欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android编程实现带有图标的ListView并带有长按菜单效果示例

程序员文章站 2022-06-11 09:31:33
本文实例讲述了android编程实现带有图标的listview并带有长按菜单效果。分享给大家供大家参考,具体如下: mainactivity.java pack...

本文实例讲述了android编程实现带有图标的listview并带有长按菜单效果。分享给大家供大家参考,具体如下:

mainactivity.java

package com.zxl;
import java.util.arraylist;
import java.util.list;
import org.w3c.dom.ls.lsexception;
import android.app.activity;
import android.os.bundle;
import android.view.contextmenu;
import android.view.contextmenu.contextmenuinfo;
import android.view.menuitem;
import android.view.view;
import android.widget.adapterview;
import android.widget.adapterview.adaptercontextmenuinfo;
import android.widget.listview;
import android.widget.adapterview.onitemclicklistener;
import android.widget.toast;
public class mainactivity extends activity implements onitemclicklistener {
  /** called when the activity is first created. */
  listview lv;
  list<listviewitem> items;
  customlistviewadapter adapter;
  @override
  public void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.main);
    lv=(listview)findviewbyid(r.id.listview1);
    items=new arraylist<mainactivity.listviewitem>();
    items.add(new listviewitem(){{
      thumbnailresource=r.drawable.ic_launcher;
      title="item1";
      subtitle="item1 description";
    }});
    items.add(new listviewitem(){{
      thumbnailresource=r.drawable.ic_launcher;
      title="item2";
      subtitle="item2 description";
    }});
    items.add(new listviewitem(){{
      thumbnailresource=r.drawable.ic_launcher;
      title="item3";
      subtitle="item3 description";
    }});
    items.add(new listviewitem(){{
      thumbnailresource=r.drawable.ic_launcher;
      title="item4";
      subtitle="item4 description";
    }});
    adapter=new customlistviewadapter(this, items);
    lv.setadapter(adapter);
    registerforcontextmenu(lv);
    lv.setonitemclicklistener(this);
  }
  class listviewitem{
    public int thumbnailresource;
    public string title;
    public string subtitle;
  }
  public void onitemclick(adapterview<?> parent, view view, int position, long id) {
    // todo auto-generated method stub
/*   listviewitem item=items.get(position); //删除
    items.remove(item);
    adapter=new customlistviewadapter(this, items);
    lv.setadapter(adapter);*/
    listviewitem item=items.get(position);
    toast.maketext(mainactivity.this, "this is :"+item, toast.length_long).show();
  }
  @override
  public void oncreatecontextmenu(contextmenu menu, view v, contextmenuinfo menuinfo) {
    // todo auto-generated method stub
    super.oncreatecontextmenu(menu, v, menuinfo);
    menu.add("add");
    menu.add("update");
    menu.add("delete");
  }
  @override
  public boolean oncontextitemselected(menuitem item) {
    // todo auto-generated method stub
    super.oncontextitemselected(item);
    if(item.gettitle()=="add"){
      toast.maketext(mainactivity.this, "add", toast.length_long).show();
      items.add(new listviewitem(){{
        thumbnailresource=r.drawable.ic_launcher;
        title="item new";
        subtitle="item new description";
      }});
      adapter=new customlistviewadapter(this, items);
      lv.setadapter(adapter);
    }
    else if(item.gettitle()=="update"){
      //do somthing
    }
    else if(item.gettitle()=="delete"){
      contextmenuinfo info = item.getmenuinfo();
      adapterview.adaptercontextmenuinfo contextmenuinfo = (adaptercontextmenuinfo) info;
       // 获取选中行位置
      int position = contextmenuinfo.position;
      listviewitem itema=items.get(position);
      items.remove(itema);
      adapter=new customlistviewadapter(this, items);
      lv.setadapter(adapter);
    }
    return true;
  }
}

customlistviewadapter.java

package com.zxl;
import java.util.list;
import com.zxl.mainactivity.listviewitem;
import android.app.activity;
import android.content.context;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
import android.widget.baseadapter;
import android.widget.imageview;
import android.widget.listview;
import android.widget.textview;
public class customlistviewadapter extends baseadapter
{
  layoutinflater inflater;
  list<listviewitem> items;
  public customlistviewadapter(activity context, list<listviewitem> items) {
    super();
    this.items = items;
    this.inflater = (layoutinflater)context.getsystemservice(context.layout_inflater_service);
  }
  public int getcount() {
    // todo auto-generated method stub
    return items.size();
  }
  public object getitem(int position) {
    // todo auto-generated method stub
    return null;
  }
  public long getitemid(int position) {
    // todo auto-generated method stub
    return 0;
  }
  public view getview(final int position, view convertview, viewgroup parent) {
    // todo auto-generated method stub
    //items
    listviewitem item=items.get(position);
    view vi=convertview;
    if(convertview==null)
      vi = inflater.inflate(r.layout.item_row, null);
    imageview imgthumbnail=(imageview) vi.findviewbyid(r.id.imgthumbnail);
    textview txttitle =(textview) vi.findviewbyid(r.id.txttitle);
    textview txtsubtitle=(textview) vi.findviewbyid(r.id.txtsubtitle);
    imgthumbnail.setimageresource(item.thumbnailresource);
    txttitle.settext(item.title);
    txtsubtitle.settext(item.subtitle);
    return vi;
  }
}

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:orientation="vertical" >
  <textview
    android:id="@+id/textview1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />
  <listview
    android:id="@+id/listview1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignparentleft="true"
    android:layout_below="@+id/textview1"
    android:layout_alignparenttop="true" >
  </listview>
</relativelayout>

item_row.xml

<?xml version="1.0" encoding="utf-8"?>
<relativelayout
  android:id="@+id/relativelayout1"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:padding="5dip">
  <imageview
    android:layout_width="78dip"
    android:layout_height="78dip"
    android:id="@+id/imgthumbnail"
    android:layout_alignparentleft="true"
    android:layout_centerinparent="true"
    android:layout_marginleft="-3dip"
    android:scaletype="centerinside">
  </imageview>
  <textview
    android:textappearance="?android:attr/textappearancelarge"
    android:layout_height="wrap_content"
    android:text="textview"
    android:layout_width="wrap_content"
    android:id="@+id/txttitle"
    android:layout_torightof="@+id/imgthumbnail"
    android:layout_margintop="6dip"
    android:layout_marginleft="6dip">
  </textview>
  <textview
    android:layout_height="wrap_content"
    android:text="textview"
    android:layout_width="wrap_content"
    android:id="@+id/txtsubtitle"
    android:layout_torightof="@+id/imgthumbnail"
    android:layout_below="@+id/txttitle"
    android:layout_margintop="3dip"
    android:layout_marginleft="6dip">
  </textview>
</relativelayout>

文件结构:

Android编程实现带有图标的ListView并带有长按菜单效果示例Android编程实现带有图标的ListView并带有长按菜单效果示例

更多关于android相关内容感兴趣的读者可查看本站专题:《android开发入门与进阶教程》、《android布局layout技巧总结》、《android视图view技巧总结》、《android编程之activity操作技巧总结》、《android操作json格式数据技巧总结》、《android资源操作技巧汇总》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。