实现ListView删除某行功能,主界面简单的一个ListView视图 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#888888"/> </RelativeLayout> 列表项为一个Textview显示内容,ImageView用来点击显示Popupmenu <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:textAppearance="?android:attr/textAppearanceMedium" /> <ImageView android:id="@+id/image_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:src="@drawable/ic_overflow" /> </RelativeLayout> PopupMenu菜单选项 <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/remove" android:title="@string/remove"/> </menu> 主要逻辑,加载每一个列表项,对列表项的图片设置监听事件。当该图片被点击时,显式Popupmenu菜单。点击Remove选项将改行删除。 public class MainActivity extends AppCompatActivity { private ArrayList<String> items; private MyAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); items = new ArrayList<>(); for (int i = 0; i < Cheeses.CHEESES.length; i++){ items.add(Cheeses.CHEESES[i]); } adapter = new MyAdapter(MainActivity.this); ListView listView = (ListView) findViewById(R.id.list_view); listView.setAdapter(adapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, "Item Clicked:" + items.get(position), Toast.LENGTH_SHORT).show(); } }); } class MyAdapter extends BaseAdapter{ private Context context; public MyAdapter(Context context) { super(); this.context = context; } @Override public int getCount() { return items.size(); } @Override public Object getItem(int position) { return items.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; if (convertView == null){ convertView = LayoutInflater.from(context).inflate(R.layout.list_item,null); viewHolder = new ViewHolder(); viewHolder.text_view = (TextView) convertView.findViewById(R.id.text_view); viewHolder.image_view = (ImageView) convertView.findViewById(R.id.image_view); convertView.setTag(viewHolder); }else { viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.text_view.setText(items.get(position)); viewHolder.image_view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showPopupMenu(v, position); } }); return convertView; } } private void showPopupMenu(View view, final int position) { final PopupMenu popup = new PopupMenu(MainActivity.this, view); popup.getMenuInflater().inflate(R.menu.popup, popup.getMenu()); popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem menuItem) { switch (menuItem.getItemId()) { case R.id.remove: items.remove(position); adapter.notifyDataSetChanged(); break; } return false; } }); popup.show(); } public class ViewHolder{ TextView text_view; ImageView image_view; } }(责任编辑:好模板) |