📄 contactlisttreeadapter.java
字号:
public long getGroupId(int groupPosition) { if (isPosForOngoingConversation(groupPosition) || isPosForSubscription(groupPosition)) { return 0; } else { return mAdapter.getGroupId(getChildAdapterPosition(groupPosition)); } } public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { if (isPosForOngoingConversation(groupPosition) || isPosForSubscription(groupPosition)) { View v; if (convertView != null) { v = convertView; } else { v = newGroupView(parent); } TextView text1 = (TextView)v.findViewById(R.id.text1); TextView text2 = (TextView)v.findViewById(R.id.text2); Resources r = v.getResources(); ImApp app = ImApp.getApplication(mActivity); BrandingResources brandingRes = app.getBrandingResource(mProviderId); String text = isPosForOngoingConversation(groupPosition) ? brandingRes.getString( BrandingResourceIDs.STRING_ONGOING_CONVERSATION, getOngoingConversationCount()) : r.getString(R.string.subscriptions); text1.setText(text); text2.setVisibility(View.GONE); return v; } else { return mAdapter.getGroupView(getChildAdapterPosition(groupPosition), isExpanded, convertView, parent); } } public boolean isChildSelectable(int groupPosition, int childPosition) { if (isPosForOngoingConversation(groupPosition)) { // "Empty" TextView is not selectable if (getOngoingConversationCount()==0) return false; return true; } if (isPosForSubscription(groupPosition)) return true; return mAdapter.isChildSelectable(getChildAdapterPosition(groupPosition), childPosition); } public boolean stableIds() { return true; } @Override public void registerDataSetObserver(DataSetObserver observer) { mAdapter.registerDataSetObserver(observer); super.registerDataSetObserver(observer); } @Override public void unregisterDataSetObserver(DataSetObserver observer) { mAdapter.unregisterDataSetObserver(observer); super.unregisterDataSetObserver(observer); } public boolean hasStableIds() { return true; } @Override public void onGroupCollapsed(int groupPosition) { super.onGroupCollapsed(groupPosition); mExpandedGroups.remove(Integer.valueOf(groupPosition)); int pos = getChildAdapterPosition(groupPosition); if (pos >= 0) { mAdapter.onGroupCollapsed(pos); } } @Override public void onGroupExpanded(int groupPosition) { super.onGroupExpanded(groupPosition); mExpandedGroups.add(groupPosition); int pos = getChildAdapterPosition(groupPosition); if (pos >= 0) { mAdapter.onGroupExpanded(pos); } } public int[] getExpandedGroups() { ArrayList<Integer> expandedGroups = mExpandedGroups; int size = expandedGroups.size(); int[] res = new int[size]; for (int i = 0; i < size; i++) { res[i] = expandedGroups.get(i); } return res; } View newChildView(ViewGroup parent) { return mInflate.inflate(R.layout.contact_view, parent, false); } View newEmptyView(ViewGroup parent) { return mInflate.inflate(R.layout.empty_conversation_group_view, parent, false); } View newGroupView(ViewGroup parent) { return mInflate.inflate(R.layout.group_view, parent, false); } private synchronized Cursor getOngoingConversations() { if (mOngoingConversations == null) { startQueryOngoingConversations(); } return mOngoingConversations; } synchronized void setOngoingConversations(Cursor c) { if (mOngoingConversations != null) { mOngoingConversations.unregisterContentObserver(mContentObserver); mOngoingConversations.unregisterDataSetObserver(mDataSetObserver); mOngoingConversations.close(); } c.registerContentObserver(mContentObserver); c.registerDataSetObserver(mDataSetObserver); mOngoingConversations = c; } private int getOngoingConversationCount() { Cursor c = getOngoingConversations(); return c == null ? 0 : c.getCount(); } private synchronized Cursor getSubscriptions() { if (mSubscriptions == null) { startQuerySubscriptions(); } return mSubscriptions; } synchronized void setSubscriptions(Cursor c) { if (mSubscriptions != null) { mSubscriptions.close(); } // we don't need to register observers on mSubscriptions because // we already have observers on mOngoingConversations and they // will be notified if there is any changes of subscription // since the two cursors come from the same table. mSubscriptions = c; } private int getSubscriptionCount() { Cursor c = getSubscriptions(); return c == null ? 0 : c.getCount(); } public boolean isPosForOngoingConversation(int groupPosition) { return groupPosition == 0; } public boolean isPosForSubscription(int groupPosition) { return groupPosition == 1 && getSubscriptionCount() > 0; } private int getChildAdapterPosition(int groupPosition) { if (getSubscriptionCount() > 0) { return groupPosition - 2; } else { return groupPosition - 1; } } private Cursor moveTo(Cursor cursor, int position) { if (cursor.moveToPosition(position)) { return cursor; } return null; } private long getId(Cursor cursor, int position) { if (cursor.moveToPosition(position)) { return cursor.getLong(ContactView.COLUMN_CONTACT_ID); } return 0; } class ListTreeAdapter extends CursorTreeAdapter { public ListTreeAdapter(Cursor cursor) { super(cursor, mActivity); } @Override protected void bindChildView(View view, Context context, Cursor cursor, boolean isLastChild) { // binding when child is text view for an empty group if (view instanceof TextView) { ((TextView) view).setText(mActivity.getText(R.string.empty_contact_group)); } else { ((ContactView) view).bind(cursor, null, isScrolling()); } } @Override protected void bindGroupView(View view, Context context, Cursor cursor, boolean isExpanded) { TextView text1 = (TextView)view.findViewById(R.id.text1); TextView text2 = (TextView)view.findViewById(R.id.text2); Resources r = view.getResources(); text1.setText(cursor.getString(COLUMN_CONTACT_LIST_NAME)); text2.setVisibility(View.VISIBLE); text2.setText(r.getString(R.string.online_count, getOnlineChildCount(cursor))); } View newEmptyView(ViewGroup parent) { return mInflate.inflate(R.layout.empty_contact_group_view, parent, false); } // if the group is empty, provide a text view. The infrastructure provides a "convertView" // as a possible suggestion to reuse an existing view's data. It may be null, it may be a // TextView, or it may be a ContactView, so we need to test the possible cases. @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { // Provide a TextView if the group is empty if (super.getChildrenCount(groupPosition)==0) { if (convertView != null) { if (convertView instanceof TextView) { ((TextView) convertView).setText( mActivity.getText(R.string.empty_contact_group)); return convertView; } } return newEmptyView(parent); } if ( !(convertView instanceof ContactView) ) { convertView = null; } return super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent); } @Override protected Cursor getChildrenCursor(Cursor groupCursor) { long listId = groupCursor.getLong(COLUMN_CONTACT_LIST_ID); startQueryContacts(listId); return null; } // return a TextView for empty groups @Override protected View newChildView(Context context, Cursor cursor, boolean isLastChild, ViewGroup parent) { if (cursor.getCount() == 0) { return newEmptyView(parent); } else { return ContactListTreeAdapter.this.newChildView(parent); } } @Override protected View newGroupView(Context context, Cursor cursor, boolean isExpanded, ViewGroup parent) { return ContactListTreeAdapter.this.newGroupView(parent); } private int getOnlineChildCount(Cursor groupCursor) { long listId = groupCursor.getLong(COLUMN_CONTACT_LIST_ID); if (mOnlineContactsCountMap == null) { String where = Im.Contacts.ACCOUNT + "=" + mAccountId; ContentResolver cr = mActivity.getContentResolver(); Cursor c = cr.query(Im.Contacts.CONTENT_URI_ONLINE_COUNT, CONTACT_COUNT_PROJECTION, where, null, null); mOnlineContactsCountMap = new ContentQueryMap(c, Im.Contacts.CONTACTLIST, true, mHandler); mOnlineContactsCountMap.addObserver(new Observer(){ public void update(Observable observable, Object data) { notifyDataSetChanged(); }}); } ContentValues value = mOnlineContactsCountMap.getValues(String.valueOf(listId)); return value == null ? 0 : value.getAsInteger(Im.Contacts._COUNT); } @Override public int getChildrenCount(int groupPosition) { int children = super.getChildrenCount(groupPosition); if (children == 0) { // Count the empty group text item as a child return 1; } return children; } // Don't allow the empty group text item to be selected @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return (super.getChildrenCount(groupPosition) > 0); } } private class MyContentObserver extends ContentObserver { public MyContentObserver() { super(mHandler); } @Override public void onChange(boolean selfChange) { if (Log.isLoggable(ImApp.LOG_TAG, Log.DEBUG)){ log("MyContentObserver.onChange() autoRequery=" + mAutoRequery); } // Don't requery when fling. We will schedule a requery when the fling is complete. if (isScrolling()) { return; } if (mAutoRequery) { startQueryOngoingConversations(); } else { mRequeryPending = true; } } } private class MyDataSetObserver extends DataSetObserver { public MyDataSetObserver() { } @Override public void onChanged() { if (Log.isLoggable(ImApp.LOG_TAG, Log.DEBUG)){ log("MyDataSetObserver.onChanged()"); } mDataValid = true; notifyDataSetChanged(); } @Override public void onInvalidated() { if (Log.isLoggable(ImApp.LOG_TAG, Log.DEBUG)){ log("MyDataSetObserver.onInvalidated()"); } mDataValid = false; notifyDataSetInvalidated(); } } public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // no op } public void onScrollStateChanged(AbsListView view, int scrollState) { int oldState = mScrollState; mScrollState = scrollState; // If we just finished a fling then some items may not have an icon // So force a full redraw now that the fling is complete if (oldState == OnScrollListener.SCROLL_STATE_FLING) { notifyDataSetChanged(); } } public boolean isScrolling() { return mScrollState == OnScrollListener.SCROLL_STATE_FLING; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -