例如mp3播放器的音乐播放列表,点击一个item后开始播放音乐(OnItemClickListener),长按让其进入编辑模式(不跳转到另一个页面,还在同一个页面),此时用到了OnItemLongClickListener,然后再次长按某一个音乐的条目进行拖动(改变列表顺序),这时需要屏蔽掉上一次长按进入编辑模式的事件。实现代码如下
package com.example.longdoubleclickdemo;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ListView mMusicListView;
private List<MusicItem> musicList;
private MyAdapter mAdapter;
private TextView mTitleName;
private boolean mEditMode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitleName = (TextView)findViewById(R.id.titlebar);
mMusicListView = (ListView)findViewById(R.id.music_list);
initData();
mAdapter = new MyAdapter(this);
mMusicListView.setAdapter(mAdapter);
initListeners();
}
private void initData()
{
musicList = new ArrayList<MusicItem>();
for(int i = 0; i < 20; i++)
{
musicList.add(setMusicItem(i));
}
}
public boolean getEditMode()
{
return mEditMode;
}
public void setEditMode(boolean mEditMode)
{
this.mEditMode = mEditMode;
}
private void initListeners()
{
mMusicListView.setOnItemClickListener(new ListView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//Toast.makeText(getApplicationContext(), "默认模式", Toast.LENGTH_SHORT).show();
}
});
mMusicListView.setOnItemLongClickListener(new ListView.OnItemLongClickListener()
{
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
boolean mEditMode = getEditMode();
if(!mEditMode)
{
setEditMode(true);
Toast.makeText(getApplicationContext(), "进入编辑模式", Toast.LENGTH_SHORT).show();
mTitleName.setText("编辑模式");
}else{
Toast.makeText(getApplicationContext(), "编辑 "+position, Toast.LENGTH_SHORT).show();
}
return false;
}
});
}
public MusicItem setMusicItem(int position)
{
MusicItem musicItem = new MusicItem();
musicItem.setMusicTitle("music "+position);
musicItem.setMusicDragIcon(getResources().getDrawable(
R.drawable.change_order_grip));
return musicItem;
}
static class ViewHolder
{
public TextView musicName;
public ImageView musicDragIcon;
}
class MyAdapter extends BaseAdapter
{
private LayoutInflater mInflater = null;
public MyAdapter(Context context)
{
this.mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return musicList.size();
}
@Override
public Object getItem(int position) {
return musicList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(null == convertView)
{
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.music_list_item, null);
holder.musicName = (TextView)convertView.findViewById(R.id.music_name);
holder.musicDragIcon = (ImageView)convertView.findViewById(R.id.music_drag);
convertView.setTag(holder);
}else
{
holder = (ViewHolder)convertView.getTag();
}
holder.musicName.setText(musicList.get(position).getMusicTitle());
holder.musicDragIcon.setImageDrawable(musicList.get(position).getMusicDragIcon());
return convertView;
}
}
}
运行结果:第一次长按触发进入编辑模式,例如导航栏目的改变,第二次或以后再长按触发item则执行其他的item时间,例如拖拽更改列表顺序(以后更新)