首先在listview的实现中在数据适配下添加
mListView.setOnItemClickListener(this); mListView.setOnItemLongClickListener(this);的实现事件,然后实现重写该方法即可
核心代码
@Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { //点击事件 @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, "你刚才点击的是item:" + position + "id为:" + id, Toast.LENGTH_SHORT).show(); } }); } @Override public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) { mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { //长按事件 @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this,"你刚才长按的是item:" + position + "id为:" + id,Toast.LENGTH_SHORT).show(); return true; } }); return true; }
具体代码如下:
package com.example.dymemory1;
import android.app.Activity;
import android.os.Bundle;
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 implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {
private ListView mListView;
private String[] titles = {"苹果", "蛋糕", "桌子", "猕猴桃", "毛衣"};
private String[] prices = {"10元/kg", "100元/个", "45元/把", "10元/kg", "110元/件"};
private int[] icons = {R.drawable.apple, R.drawable.cake, R.drawable.table, R.drawable.kiwifruit, R.drawable.wireclothes};
protected void onCreate(Bundle saveInstanceState) {
super.onCreate(saveInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.lv);
MyBaseAdaPter mAdapter = new MyBaseAdaPter();
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(this);
mListView.setOnItemLongClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { //点击事件
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "你刚才点击的是item:" + position + "id为:" + id, Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { //长按事件
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this,"你刚才长按的是item:" + position + "id为:" + id,Toast.LENGTH_SHORT).show();
return true;
}
});
return true;
}
class MyBaseAdaPter extends BaseAdapter {
@Override
public int getCount() {
return titles.length;
}
@Override
public Object getItem(int position) {
return titles[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = View.inflate(MainActivity.this, R.layout.list_item, null);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.price = (TextView) convertView.findViewById(R.id.price);
holder.iv = (ImageView) convertView.findViewById(R.id.iv);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.title.setText(titles[position]);
holder.price.setText(prices[position]);
holder.iv.setBackgroundResource(icons[position]);
return convertView;
}
}
static class ViewHolder {
TextView title;
TextView price;
ImageView iv;
}
}
/*mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { //点击事件
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this,"点击的是索引:" + position + "id为:" + id,Toast.LENGTH_SHORT).show();
}
});
*/
/* mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { //长按事件
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this,"点击的是索引:" + position + "id为:" + id,Toast.LENGTH_SHORT).show();
return true;
}
});*/
这是MainActivity.java文件,activity_main.xml和item_xml文件就没有复制上去!
最后看下效果图