daimajia / daimajia/AndroidSwipeLayout
Using the prebuilt Adapter Serial, CursorSwipeAdapter.
- Dominant language
- Java
- Stars
- 12.3k
- Forks
- 2.6k
- PR merge metrics
- No merged PRs in 30d
Description
Hi Daimajia!
Wonder library you have here. I've used your library on a previous project using a typical Array Adapter. I'm working on a new project now and using a Cursor Adapter to populate a list of Contacts from your address-book. Everything compiles right but somehow the SwipeLayout action+animation is not triggered, just the default list item handling is present.
Provided below is the my Adapter class that directly extends CursorSwipeAdapter
``` java
public class ContactsAdapter extends CursorSwipeAdapter implements SectionIndexer {
protected LayoutInflater mInflater;
protected AlphabetIndexer mAlphabetIndexer;
protected TextAppearanceSpan highlightTextSpan;
protected String mSearchTerm;
protected ImageLoader mImageLoader;
public ContactsAdapter(Context context, String mSearchTerm, ImageLoader mImageLoader) {
super(context, null, 0);
// Stores inflater for use later
mInflater = LayoutInflater.from(context);
this.mSearchTerm = mSearchTerm;
this.mImageLoader = mImageLoader;
// Loads a string containing the English alphabet. To fully localize the app, provide a
// strings.xml file in res/values- directories, where is a locale. In the file,
// define a string with android:name="alphabet" and contents set to all of the
// alphabetic characters in the language in their proper sort order, in upper case if
// applicable.
final String alphabet = context.getString(R.string.alphabet);
// Instantiates a new AlphabetIndexer bound to the column used to sort contact names.
// The cursor is left null, because it has not yet been retrieved.
mAlphabetIndexer = new AlphabetIndexer(null, ContactsQuery.SORT_KEY, alphabet);
// Defines a span for highlighting the part of a display name that matches the search
// string
highlightTextSpan = new TextAppearanceSpan(context, R.style.searchTextHiglight);
}
public void setmSearchTerm(String mSearchTerm) {
this.mSearchTerm = mSearchTerm;
}
/**
* Identifies the start of the search string in the display name column of a Cursor row.
* E.g. If displayName was "Adam" and search query (mSearchTerm) was "da" this would
* return 1.
*
* @param displayName The contact display name.
* @return The starting position of the search string in the display name, 0-based. The
* method returns -1 if the string is not found in the display name, or if the search
* string is empty or null.
*/
protected int indexOfSearchQuery(String displayName) {
if (!TextUtils.isEmpty(mSearchTerm)) {
return displayName.toLowerCase(Locale.getDefault()).indexOf(
mSearchTerm.toLowerCase(Locale.getDefault()));
}
return -1;
}
/**
* Overrides newView() to inflate the list item views.
*/
@Override
public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
// Inflates the list item layout.
final View itemLayout =
mInflater.inflate(R.layout.list_item_contacts, viewGroup, false);
SwipeLayout swipeLayout = (SwipeLayout) itemLayout.findViewById(R.id.contact_item_swipe_layout);
// Creates a new ViewHolder in which to store handles to each view resource. This
// allows bindView() to retrieve stored references instead of calling findViewById for
// each instance of the layout.
final ViewHolder holder = new ViewHolder();
holder.text1 = (TextView) itemLayout.findViewById(R.id.contact_firstname);
holder.phoneNumbersText = (TextView) itemLayout.findViewById(R.id.phone_numbers_text);
// holder.icon = (QuickContactBadge) itemLayout.findViewById(R.id.contact_avatar);
holder.icon = (CircleImageView) itemLayout.findViewById(R.id.contact_avatar);
holder.swipeLayout = swipeLayout;
// Stores the resourceHolder instance in itemLayout. This makes resourceHolder
// available to bindView and other methods that receive a handle to the item view.
itemLayout.setTag(holder);
// Returns the item layout view
return itemLayout;
}
/**
* Binds data from the Cursor to the provided view.
*/
@Override
public void bindView(View view, Context context, Cursor cursor) {
// Gets handles to individual view resources
final ViewHolder holder = (ViewHolder) view.getTag();
// For Android 3.0 and later, gets the thumbnail image Uri from the current Cursor row.
// For platforms earlier than 3.0, this isn't necessary, because the thumbnail is
// generated from the other fields in the row.
final String photoUri = cursor.getString(ContactsQuery.PHOTO_THUMBNAIL_DATA);
final String displayName = cursor.getString(ContactsQuery.DISPLAY_NAME);
final int startIndex = indexOfSearchQuery(displayName);
//set show mode.
holder.swipeLayout.setShowMode(SwipeLayout.ShowMode.LayDown);
//add drag edge.(If the BottomView has 'layout_gravity' attribute, this line is unnecessary)
holder.swipeLayout.addDrag(SwipeLayout.DragEdge.Left, holder.swipeLayout.findViewById(R.id.bottom_wrapper));
if (startIndex == -1) {
// If the user didn't do a search, or the search string didn't match a display
// name, show the display name without highlighting
holder.text1.setText(displayName);
} else {
// If the search string matched the display name, applies a SpannableString to
// highlight the search string with the displayed display name
// Wraps the display name in the SpannableString
final SpannableString highlightedName = new SpannableString(displayName);
// Sets the span to start at the starting point of the match and end at "length"
// characters beyond the starting point
highlightedName.setSpan(highlightTextSpan, startIndex,
startIndex + mSearchTerm.length(), 0);
// Binds the SpannableString to the display name View object
holder.text1.setText(highlightedName);
}
if (!TextUtils.isEmpty(mSearchTerm)) {
String displayNumber = null;
boolean searchIsInPhoneNumber = false;
final ContentResolver contentResolver = context.getContentResolver();
Cursor pCur = contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{cursor.getString(ContactsQuery.ID)}, null);
ArrayList phoneNumbers = new ArrayList<>();
if (pCur != null) {
while (pCur.moveToNext()) {
// Do something with phones
PhoneNumber phoneNumber = new PhoneNumber();
String number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
phoneNumber.setPhoneNumber(number);
int type = pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
switch (type) {
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
// do something with the Home number here...
Log.d("PhoneCursor", "HOME: " + number);
phoneNumber.setPhoneType(PhoneNumber.TYPE_HOME);
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
// do something with the Mobile number here...
Log.d("PhoneCursor", "MOBILE: " + number);
phoneNumber.setPhoneType(PhoneNumber.TYPE_MOBILE);
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
// do something with the Work number here...
Log.d("PhoneCursor", "WORK: " + number);
phoneNumber.setPhoneType(PhoneNumber.TYPE_WORK);
break;
default:
// do something with the Other number here...
Log.d("PhoneCursor", "Other: " + number);
phoneNumber.setPhoneType(PhoneNumber.TYPE_OTHER);
break;
}
if (phoneNumber.getPhoneNumber().contains(mSearchTerm)) {
displayNumber = phoneNumber.getPhoneNumber();
searchIsInPhoneNumber = true;
}
phoneNumbers.add(phoneNumber);
}
}
if (displayNumber == null) {
StringBuilder stringBuilder = new StringBuilder();
for (PhoneNumber phoneNumber : phoneNumbers) {
stringBuilder.append(String.format(" %s;", phoneNumber.getPhoneNumber()));
}
}
if (pCur != null) {
pCur.close();
}
if (searchIsInPhoneNumber) {
final int startIndexPhoneNumber = indexOfSearchQuery(displayNumber);
final SpannableString highlightedNumber = new SpannableString(displayNumber);
// Sets the span to start at the starting point of the match and end at "length"
// characters beyond the starting point
highlightedNumber.setSpan(highlightTextSpan, startIndexPhoneNumber,
startIndexPhoneNumber + mSearchTerm.length(), 0);
// Binds the SpannableString to the display name View object
holder.phoneNumbersText.setText(highlightedNumber);
} else {
holder.phoneNumbersText.setText(displayNumber);
}
holder.phoneNumbersText.setVisibility(View.VISIBLE);
} else {
holder.phoneNumbersText.setVisibility(View.GONE);
}
// Loads the thumbnail image pointed to by photoUri into the QuickContactBadge in a
// background worker thread
if (mImageLoader != null)
mImageLoader.loadImage(photoUri, holder.icon);
}
/**
* Overrides swapCursor to move the new Cursor into the AlphabetIndex as well as the
* CursorAdapter.
*/
@Override
public Cursor swapCursor(Cursor newCursor) {
// Update the AlphabetIndexer with new cursor as well
mAlphabetIndexer.setCursor(newCursor);
return super.swapCursor(newCursor);
}
/**
* An override of getCount that simplifies accessing the Cursor. If the Cursor is null,
* getCount returns zero. As a result, no test for Cursor == null is needed.
*/
@Override
public int getCount() {
if (getCursor() == null) {
return 0;
}
return super.getCount();
}
/**
* Defines the SectionIndexer.getSections() interface.
*/
@Override
public Object[] getSections() {
return mAlphabetIndexer.getSections();
}
/**
* Defines the SectionIndexer.getPositionForSection() interface.
*/
@Override
public int getPositionForSection(int i) {
if (getCursor() == null) {
return 0;
}
return mAlphabetIndexer.getPositionForSection(i);
}
/**
* Defines the SectionIndexer.getSectionForPosition() interface.
*/
@Override
public int getSectionForPosition(int i) {
if (getCursor() == null) {
return 0;
}
return mAlphabetIndexer.getSectionForPosition(i);
}
@Override
public void closeAllItems() {
}
@Override
public int getSwipeLayoutResourceId(int position) {
return R.id.contact_item_swipe_layout;
}
/**
* A class that defines fields for each resource ID in the list item layout. This allows
* ContactsAdapter.newView() to store the IDs once, when it inflates the layout, instead of
* calling findViewById in each iteration of bindView.
*/
private class ViewHolder {
TextView text1;
TextView phoneNumbersText;
SwipeLayout swipeLayout;
CircleImageView icon;
}
}
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.