时间:2021-05-20
本文实例讲述了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); 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相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android布局layout技巧总结》、《Android视图View技巧总结》、《Android编程之activity操作技巧总结》、《Android操作json格式数据技巧总结》、《Android资源操作技巧汇总》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了CSS实现带有小图片的LI图标列表菜单。分享给大家供大家参考。具体如下:这是一款带有小图片的Li列表菜单,竖向的图标菜单,鼠标放上会变色,对鼠标的
在“视图”菜单上,指向“工具栏”,然后单击“控件工具箱”。 在“控件工具箱”上,单击“其他控件”(带有锤子和扳手图标的按钮)。 在列表中,向下滚动并单击
本文实例讲述了Android实现带有边框的ListView和item的方法。分享给大家供大家参考,具体如下:想为ListView和item四周添加边框有两种方法
最近试着做了个Android带有弹出收缩动画的扇形菜单,留个笔记记录一下。效果如下publicclassMainActivityextendsAppCompat
本文实例为大家分享了Android实现带有进度条按钮效果的具体代码,供大家参考,具体内容如下安卓中带有进度条效果的按钮,如下图:1.布局文件如下activity