onItemClickListener在服务列表视图中没有响应

时间:2014-04-25 16:26:35

标签: android listview android-listview

我在android中实现了一个包含listview的聊天头。我已经设置了  OnItemClickListener到列表视图但是当我运行应用程序时,服务会显示列表视图,但列表视图不响应单击操作。下面是相关代码,

public class ChatHeadDrawerService extends Service implements OnClickListener,
    OnItemClickListener {

private WindowManager mWindowManager;
private View mChatHead;
private LinearLayout mLayout, chatheadcontacts;
private static int screenWidth;
private static int screenHeight;

private TextView hidetextView;
private ImageView showView;

ListView contacts;
public ArrayList<ContactListItem> contactsList;

@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;
     */

    LayoutInflater 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);

    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.MATCH_PARENT, // Height
            TYPE_PHONE, // Type
            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);
}

@Override
public void onDestroy() {
    super.onDestroy();
    if (mChatHead != null)
        mWindowManager.removeView(mChatHead);
}   @Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.textViewhide:
        chatheadcontacts.setVisibility(View.GONE);
        break;
    case R.id.imageViewShow:
        chatheadcontacts.setVisibility(View.VISIBLE);
        break;
    }

}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_SHORT)
            .show();
}

0 个答案:

没有答案