0
我在android編程中遇到了一個問題,我需要從服務創建一個listview,listview工作正常,並按預期顯示項目。唯一的問題是我無法處理listview上的項目點擊事件。我試圖谷歌和找出一種方式,但沒有成功。我怎麼解決這個問題?以下是我的代碼,處理列表視圖OnItemClick服務中的事件
@SuppressWarnings("unused")
public class ContactsService extends Service implements OnClickListener,
OnItemClickListener {
private WindowManager mWindowManager;
private View mChatHead;
private LinearLayout mLayout, chatheadcontacts;
private TextView hidetextView;
private ImageView showView, cameraImageView;
ListView contacts;
public ArrayList<ContactListItem> contactsList;
LayoutInflater inflater;
@Override
public IBinder onBind(Intent intent) {
return null;
}
// @SuppressLint("NewApi")
@Override
public void onCreate() {
super.onCreate();
// Typeface tf = Typeface.createFromAsset(getAssets(),
// "Roboto-Light.ttf");
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
Display display = mWindowManager.getDefaultDisplay();
/*
* Point size = new Point(); display.getSize(size); screenWidth =
* size.x; screenHeight = size.y;
*/
inflater = LayoutInflater.from(this);
mChatHead = inflater.inflate(R.layout.chathead, null);
contacts = (ListView) mChatHead
.findViewById(R.id.listView_chathead_contacts);
mLayout = (LinearLayout) mChatHead
.findViewById(R.id.chathead_linearlayout);
chatheadcontacts = (LinearLayout) mChatHead
.findViewById(R.id.chathead_contacts_layout);
cameraImageView = (ImageView)mChatHead.findViewById(R.id.chathead_imageView_camera);
hidetextView = (TextView) mChatHead.findViewById(R.id.textViewhide);
showView = (ImageView) mChatHead.findViewById(R.id.imageViewShow);
// mChatHeadTextView.setTypeface(tf);
contactsList = new ArrayList<ContactListItem>();
contactsList = getContacts();
final WindowManager.LayoutParams parameters = new WindowManager.LayoutParams(
WRAP_CONTENT, // Width
LayoutParams.WRAP_CONTENT, // Height
TYPE_PHONE, // Type
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, // Flag
PixelFormat.TRANSLUCENT // Format
);
parameters.gravity = Gravity.RIGHT;
contacts.setAdapter(new CustomChatheadListAdapter(getBaseContext(),
contactsList));
mWindowManager.addView(mChatHead, parameters);
chatheadcontacts.setVisibility(View.GONE);
hidetextView.setOnClickListener(this);
showView.setOnClickListener(this);
contacts.setOnItemClickListener(this);
cameraImageView.setOnClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(getApplicationContext(), "Clicked "+position, Toast.LENGTH_SHORT)
.show();
Log.i("Clicked",""+position);
// showPopup();
}
}
謝謝。
確保ListView項視圖中不包含任何可聚焦照片喜歡按鈕。 – 2015-02-11 11:15:58
如果我明白你的意思,每個listview項目只有一個imageview。 – 2015-02-11 11:17:55