Example usage for android.widget ListAdapter isEnabled

List of usage examples for android.widget ListAdapter isEnabled

Introduction

In this page you can find the example usage for android.widget ListAdapter isEnabled.

Prototype

boolean isEnabled(int position);

Source Link

Document

Returns true if the item at the specified position is not a separator.

Usage

From source file:com.ultramegasoft.flavordex2.dialog.AppChooserDialog.java

@NonNull
@Override//from  w  w w.  j a va  2 s.com
@SuppressLint("InflateParams")
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Context context = getContext();
    if (context == null) {
        return super.onCreateDialog(savedInstanceState);
    }

    final Bundle args = getArguments();
    final boolean multiChoice = args != null && args.getBoolean(ARG_MULTI_CHOICE);
    final View root = LayoutInflater.from(context).inflate(R.layout.dialog_app_chooser, null);

    mListView = root.findViewById(R.id.list);
    mListView.setAdapter(new AppListAdapter(context, multiChoice));

    final int count;
    if (multiChoice) {
        count = mListView.getCount();
        mListView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                invalidateButtons();
            }
        });

        final ListAdapter adapter = mListView.getAdapter();
        for (int i = 0; i < count; i++) {
            mListView.setItemChecked(i, adapter.isEnabled(i));
        }
    } else {
        count = 1;
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectItem(id);
            }
        });
    }

    final TextView header = root.findViewById(R.id.header);
    final String appsString = getResources().getQuantityString(R.plurals.app, count);
    header.setText(getString(R.string.header_select_app, appsString));

    final AlertDialog.Builder builder = new AlertDialog.Builder(context).setTitle(R.string.title_import_app)
            .setIcon(R.drawable.ic_import).setView(root).setNegativeButton(R.string.button_cancel, null);

    if (multiChoice) {
        builder.setPositiveButton(R.string.button_import, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                importSelected();
            }
        });
    }

    return builder.create();
}

From source file:android.support.v7.internal.widget.ListViewCompat.java

/**
 * Find a position that can be selected (i.e., is not a separator).
 *
 * @param position The starting position to look at.
 * @param lookDown Whether to look down for other positions.
 * @return The next selectable position starting at position and then searching either up or
 *         down. Returns {@link #INVALID_POSITION} if nothing can be found.
 *//*from   w  ww .j ava2  s  .c  o m*/
public int lookForSelectablePosition(int position, boolean lookDown) {
    final ListAdapter adapter = getAdapter();
    if (adapter == null || isInTouchMode()) {
        return INVALID_POSITION;
    }

    final int count = adapter.getCount();
    if (!getAdapter().areAllItemsEnabled()) {
        if (lookDown) {
            position = Math.max(0, position);
            while (position < count && !adapter.isEnabled(position)) {
                position++;
            }
        } else {
            position = Math.min(position, count - 1);
            while (position >= 0 && !adapter.isEnabled(position)) {
                position--;
            }
        }

        if (position < 0 || position >= count) {
            return INVALID_POSITION;
        }
        return position;
    } else {
        if (position < 0 || position >= count) {
            return INVALID_POSITION;
        }
        return position;
    }
}

From source file:com.library.core.view.HorizontalListView.java

/**
 * Find a position that can be selected (i.e., is not a separator).
 * /*from   ww w  .j a  v a 2  s.  com*/
 * @param position
 *            The starting position to look at.
 * @param lookDown
 *            Whether to look down for other positions.
 * @return The next selectable position starting at position and then
 *         searching either up or down. Returns {@link #INVALID_POSITION} if
 *         nothing can be found.
 */
int lookForSelectablePosition(int position, boolean lookDown) {
    final ListAdapter adapter = mAdapter;
    if (adapter == null || isInTouchMode()) {
        return INVALID_POSITION;
    }

    final int count = adapter.getCount();
    if (!adapter.areAllItemsEnabled()) {
        if (lookDown) {
            position = Math.max(0, position);
            while (position < count && !adapter.isEnabled(position)) {
                position++;
            }
        } else {
            position = Math.min(position, count - 1);
            while (position >= 0 && !adapter.isEnabled(position)) {
                position--;
            }
        }

        if (position < 0 || position >= count) {
            return INVALID_POSITION;
        }
        return position;
    } else {
        if (position < 0 || position >= count) {
            return INVALID_POSITION;
        }
        return position;
    }
}

From source file:com.tr.ui.widgets.HorizontalListView.java

/**
* Find a position that can be selected (i.e., is not a separator).
*
* @param position The starting position to look at.
* @param lookDown Whether to look down for other positions.
* @return The next selectable position starting at position and then searching either up or
* down. Returns {@link #INVALID_POSITION} if nothing can be found.
*///from ww w .j  a  v a  2 s .c  o  m
private int lookForSelectablePosition(int position, boolean lookDown) {
    final ListAdapter adapter = mAdapter;
    if (adapter == null || isInTouchMode()) {
        return INVALID_POSITION;
    }

    final int count = adapter.getCount();
    if (!adapter.areAllItemsEnabled()) {
        if (lookDown) {
            position = Math.max(0, position);
            while (position < count && !adapter.isEnabled(position)) {
                position++;
            }
        } else {
            position = Math.min(position, count - 1);
            while (position >= 0 && !adapter.isEnabled(position)) {
                position--;
            }
        }

        if (position < 0 || position >= count) {
            return INVALID_POSITION;
        }
        return position;
    } else {
        if (position < 0 || position >= count) {
            return INVALID_POSITION;
        }
        return position;
    }
}

From source file:com.juick.android.MessageMenu.java

protected void runActions() {
    if (!isDialogMode()) {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        final CharSequence[] items = new CharSequence[menuActions.size()];
        for (int j = 0; j < items.length; j++) {
            items[j] = menuActions.get(j).title;
        }/* ww  w  . j  a v  a2s  . c  o m*/
        builder.setItems(items, this);
        final AlertDialog alertDialog = builder.create();
        alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                ColorsTheme.ColorTheme colorTheme = JuickMessagesAdapter.getColorTheme(activity);
                ColorDrawable divider = new ColorDrawable(
                        colorTheme.getColor(ColorsTheme.ColorKey.COMMON_BACKGROUND, 0xFFFFFFFF));
                alertDialog.getListView().setDivider(divider);
                alertDialog.getListView().setDividerHeight(1);
            }
        });
        alertDialog.show();

        final ListAdapter adapter = alertDialog.getListView().getAdapter();
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(activity);
        float menuFontScale = 1;
        try {
            menuFontScale = Float.parseFloat(sp.getString("menuFontScale", "1.0"));
        } catch (Exception e) {
            e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        }
        final boolean compressedMenu = sp.getBoolean("compressedMenu", false);
        final boolean singleLineMenu = sp.getBoolean("singleLineMenu", false);
        final float finalMenuFontScale = menuFontScale;
        final int screenHeight = activity.getWindow().getWindowManager().getDefaultDisplay().getHeight();
        alertDialog.getListView().setAdapter(new ListAdapter() {
            @Override
            public boolean areAllItemsEnabled() {
                return adapter.areAllItemsEnabled();
            }

            @Override
            public boolean isEnabled(int position) {
                return adapter.isEnabled(position);
            }

            @Override
            public void registerDataSetObserver(DataSetObserver observer) {
                adapter.registerDataSetObserver(observer);
            }

            @Override
            public void unregisterDataSetObserver(DataSetObserver observer) {
                adapter.unregisterDataSetObserver(observer);
            }

            @Override
            public int getCount() {
                return items.length;
            }

            @Override
            public Object getItem(int position) {
                return adapter.getItem(position);
            }

            @Override
            public long getItemId(int position) {
                return adapter.getItemId(position);
            }

            @Override
            public boolean hasStableIds() {
                return adapter.hasStableIds();
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View retval = adapter.getView(position, null, parent);
                if (retval instanceof TextView) {
                    TextView tv = (TextView) retval;
                    if (compressedMenu) {
                        int minHeight = (int) ((screenHeight * 0.7) / getCount());
                        tv.setMinHeight(minHeight);
                        tv.setMinimumHeight(minHeight);
                    }
                    if (singleLineMenu) {
                        tv.setSingleLine(true);
                        tv.setEllipsize(TextUtils.TruncateAt.MIDDLE);
                    }
                    tv.setTextSize(22 * finalMenuFontScale);
                }
                return retval;
            }

            @Override
            public int getItemViewType(int position) {
                return adapter.getItemViewType(position);
            }

            @Override
            public int getViewTypeCount() {
                return adapter.getViewTypeCount();
            }

            @Override
            public boolean isEmpty() {
                return adapter.isEmpty();
            }
        });
    } else {
        AlertDialog.Builder builder = new AlertDialog.Builder(
                new ContextThemeWrapper(activity, R.style.Theme_Sherlock));
        View dialogView = activity.getLayoutInflater().inflate(R.layout.message_menu2, null);
        builder.setView(dialogView);
        builder.setCancelable(true);
        int width = activity.getWindowManager().getDefaultDisplay().getWidth();
        View scrollView = dialogView.findViewById(R.id.scrollView);
        scrollView.getLayoutParams().width = (int) (width * 0.90);
        final AlertDialog alertDialog = builder.create();
        alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                //MainActivity.restyleChildrenOrWidget(alertDialog.getWindow().getDecorView());
            }
        });
        TextView messageNo = (TextView) dialogView.findViewById(R.id.message_no);
        messageNo.setText(listSelectedItem.getDisplayMessageNo());
        Spinner openUrl = (Spinner) dialogView.findViewById(R.id.open_url);
        Button singleURL = (Button) dialogView.findViewById(R.id.single_url);
        if (urls != null && urls.size() == 1) {
            singleURL.setVisibility(View.VISIBLE);
            openUrl.setVisibility(View.GONE);
            SpannableStringBuilder sb = new SpannableStringBuilder();
            sb.append(urls.get(0));
            sb.setSpan(new UnderlineSpan(), 0, sb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            singleURL.setText(sb);
            singleURL.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    alertDialog.dismiss();
                    launchURL(listSelectedItem.getMID(), urls.get(0));
                }
            });
        } else if (urls != null && urls.size() > 0) {
            singleURL.setVisibility(View.GONE);
            openUrl.setVisibility(View.VISIBLE);
            openUrl.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    if (position != 0) {
                        alertDialog.dismiss();
                        launchURL(listSelectedItem.getMID(), urls.get(position));
                    }
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                    //To change body of implemented methods use File | Settings | File Templates.
                }
            });
            urls.add(0, activity.getString(R.string.ClickToSelectURL));
            openUrl.setAdapter(new BaseAdapter() {
                @Override
                public int getCount() {
                    return urls.size();
                }

                @Override
                public Object getItem(int position) {
                    return position;
                }

                @Override
                public long getItemId(int position) {
                    return position;
                }

                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                    View rowView = activity.getLayoutInflater().inflate(android.R.layout.simple_list_item_1,
                            null);
                    TextView textView = (TextView) rowView.findViewById(android.R.id.text1);
                    textView.setSingleLine(false);
                    textView.setMaxLines(5);
                    SpannableStringBuilder sb = new SpannableStringBuilder();
                    sb.append(urls.get(position));
                    if (position == 0) {
                        textView.setTextColor(0xFF808080);
                    } else {
                        sb.setSpan(new UnderlineSpan(), 0, sb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    }
                    textView.setText(sb);
                    return rowView;
                }
            });
        } else {
            openUrl.setVisibility(View.GONE);
            singleURL.setVisibility(View.GONE);
        }
        final String UName = listSelectedItem.User.UName;
        View recommendMessage = dialogView.findViewById(R.id.recommend_message);
        View deleteMessage = dialogView.findViewById(R.id.delete_message);
        View saveMessage = dialogView.findViewById(R.id.save_message);
        View unsaveMessage = dialogView.findViewById(R.id.unsave_message);
        //View subscribeUser = dialogView.findViewById(R.id.subscribe_user);
        View subscribeMessage = dialogView.findViewById(R.id.subscribe_message);
        //View unsubscribeUser = dialogView.findViewById(R.id.unsubscribe_user);
        View unsubscribeMessage = dialogView.findViewById(R.id.unsubscribe_message);
        View translateMessage = dialogView.findViewById(R.id.translate_message);
        View shareMessage = dialogView.findViewById(R.id.share_message);
        //View blacklistUser = dialogView.findViewById(R.id.blacklist_user);
        //View filterUser = dialogView.findViewById(R.id.filter_user);
        //View userBlog = dialogView.findViewById(R.id.user_blog);
        //View userStats = dialogView.findViewById(R.id.user_stats);
        View openMessageInBrowser = dialogView.findViewById(R.id.open_message_in_browser);
        Button userCenter = (Button) dialogView.findViewById(R.id.user_center);
        if (null == dialogView.findViewById(R.id.column_3)) {
            // only for portrait
            userCenter.setText("@" + listSelectedItem.User.UName + " " + userCenter.getText());
        }

        unsubscribeMessage.setEnabled(listSelectedItem.getRID() == 0);
        subscribeMessage.setEnabled(listSelectedItem.getRID() == 0);
        unsaveMessage.setEnabled(listSelectedItem.getRID() == 0);
        recommendMessage.setEnabled(listSelectedItem.getRID() == 0);

        if (UName.equalsIgnoreCase(JuickAPIAuthorizer.getJuickAccountName(activity.getApplicationContext()))) {
            recommendMessage.setVisibility(View.GONE);
        } else {
            deleteMessage.setVisibility(View.GONE);
        }
        if (messagesSource instanceof SavedMessagesSource) {
            saveMessage.setVisibility(View.GONE);
        } else {
            unsaveMessage.setVisibility(View.GONE);
        }
        recommendMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                actionRecommendMessage();
            }
        });
        deleteMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                actionDeleteMessage();
            }
        });
        saveMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                actionSaveMessage();
            }
        });
        unsaveMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                actionUnsaveMessage();
            }
        });
        //            subscribeUser.setOnClickListener(new View.OnClickListener() {
        //                @Override
        //                public void onClick(View v) {
        //                    alertDialog.dismiss();
        //                    actionSubscribeUser();
        //                }
        //            });
        subscribeMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                actionSubscribeMessage();
            }
        });
        //            unsubscribeUser.setOnClickListener(new View.OnClickListener() {
        //                @Override
        //                public void onClick(View v) {
        //                    alertDialog.dismiss();
        //                    actionUnsubscribeUser();
        //                }
        //            });
        unsubscribeMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                actionUnsubscribeMessage();
            }
        });
        translateMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                actionTranslateMessage();
            }
        });
        shareMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                actionShareMessage();
            }
        });
        //            blacklistUser.setOnClickListener(new View.OnClickListener() {
        //                @Override
        //                public void onClick(View v) {
        //                    alertDialog.dismiss();
        //                    actionBlacklistUser();
        //                }
        //            });
        //            filterUser.setOnClickListener(new View.OnClickListener() {
        //                @Override
        //                public void onClick(View v) {
        //                    alertDialog.dismiss();
        //                    actionFilterUser(UName);
        //                }
        //            });
        //            userBlog.setOnClickListener(new View.OnClickListener() {
        //                @Override
        //                public void onClick(View v) {
        //                    alertDialog.dismiss();
        //                    actionUserBlog();
        //                }
        //            });
        //            userStats.setOnClickListener(new View.OnClickListener() {
        //                @Override
        //                public void onClick(View v) {
        //                    alertDialog.dismiss();
        //                    actionUserStats();
        //                }
        //            });
        openMessageInBrowser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                actionOpenMessageInBrowser();
            }
        });
        userCenter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                actionUserCenter();
            }
        });
        completeInitDialogMode(alertDialog, dialogView);
        alertDialog.show();
    }
}

From source file:com.huewu.pla.lib.internal.PLAListView.java

@Override
public boolean dispatchPopulateAccessibilityEvent(final AccessibilityEvent event) {
    final boolean populated = super.dispatchPopulateAccessibilityEvent(event);

    // If the item count is less than 15 then subtract disabled items from
    // the count and
    // position. Otherwise ignore disabled items.
    if (!populated) {
        int itemCount = 0;
        int currentItemIndex = getSelectedItemPosition();

        final ListAdapter adapter = getAdapter();
        if (adapter != null) {
            final int count = adapter.getCount();
            if (count < 15) {
                for (int i = 0; i < count; i++) {
                    if (adapter.isEnabled(i)) {
                        itemCount++;/*from  w  w w .ja va 2s .co  m*/
                    } else if (i <= currentItemIndex) {
                        currentItemIndex--;
                    }
                }
            } else {
                itemCount = count;
            }
        }

        event.setItemCount(itemCount);
        event.setCurrentItemIndex(currentItemIndex);
    }

    return populated;
}

From source file:com.huewu.pla.lib.internal.PLAListView.java

/**
 * Find a position that can be selected (i.e., is not a separator).
 * /*w w  w. ja  v  a2 s .  c  o m*/
 * @param position The starting position to look at.
 * @param lookDown Whether to look down for other positions.
 * @return The next selectable position starting at position and then
 *         searching either up or down. Returns {@link #INVALID_POSITION} if
 *         nothing can be found.
 */
@Override
int lookForSelectablePositionPLA(int position, final boolean lookDown) {
    final ListAdapter adapter = mAdapter;
    if (adapter == null || isInTouchMode())
        return INVALID_POSITION;

    final int count = adapter.getCount();
    if (!mAreAllItemsSelectable) {
        if (lookDown) {
            position = Math.max(0, position);
            while (position < count && !adapter.isEnabled(position)) {
                position++;
            }
        } else {
            position = Math.min(position, count - 1);
            while (position >= 0 && !adapter.isEnabled(position)) {
                position--;
            }
        }

        if (position < 0 || position >= count)
            return INVALID_POSITION;
        return position;
    } else {
        if (position < 0 || position >= count)
            return INVALID_POSITION;
        return position;
    }
}

From source file:com.huewu.pla.lib.internal.PLA_ListView.java

@Override
public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
    boolean populated = super.dispatchPopulateAccessibilityEvent(event);

    // If the item count is less than 15 then subtract disabled items from
    // the count and
    // position. Otherwise ignore disabled items.
    if (!populated) {
        int itemCount = 0;
        int currentItemIndex = getSelectedItemPosition();

        ListAdapter adapter = getAdapter();
        if (adapter != null) {
            final int count = adapter.getCount();
            if (count < 15) {
                for (int i = 0; i < count; i++) {
                    if (adapter.isEnabled(i)) {
                        itemCount++;/*from   w ww.ja  v a2 s  . c  o  m*/
                    } else if (i <= currentItemIndex) {
                        currentItemIndex--;
                    }
                }
            } else {
                itemCount = count;
            }
        }

        event.setItemCount(itemCount);
        event.setCurrentItemIndex(currentItemIndex);
    }

    return populated;
}

From source file:com.huewu.pla.lib.internal.PLAListView.java

@Override
protected void onFocusChanged(final boolean gainFocus, final int direction, final Rect previouslyFocusedRect) {
    super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);

    if (DEBUG) {/*  w  ww  .  j ava  2s.  c  om*/
        Log.v("PLA_ListView", "onFocusChanged");
    }

    int closetChildIndex = -1;
    if (gainFocus && previouslyFocusedRect != null) {
        // previouslyFocusedRect.offset(mScrollX, mScrollY);
        previouslyFocusedRect.offset(getScrollX(), getScrollY());

        final ListAdapter adapter = mAdapter;
        // Don't cache the result of getChildCount or mFirstPosition here,
        // it could change in layoutChildren.
        if (adapter.getCount() < getChildCount() + mFirstPosition) {
            mLayoutMode = LAYOUT_NORMAL;
            layoutChildren();
        }

        // figure out which item should be selected based on previously
        // focused rect
        final Rect otherRect = mTempRect;
        int minDistance = Integer.MAX_VALUE;
        final int childCount = getChildCount();
        final int firstPosition = mFirstPosition;

        for (int i = 0; i < childCount; i++) {
            // only consider selectable views
            if (!adapter.isEnabled(firstPosition + i)) {
                continue;
            }

            final View other = getChildAt(i);
            other.getDrawingRect(otherRect);
            offsetDescendantRectToMyCoords(other, otherRect);
            final int distance = getDistance(previouslyFocusedRect, otherRect, direction);

            if (distance < minDistance) {
                minDistance = distance;
                closetChildIndex = i;
            }
        }
    }

    if (closetChildIndex >= 0) {
        setSelection(closetChildIndex + mFirstPosition);
    } else {
        requestLayout();
    }
}

From source file:com.dishes.views.stageredggridview.StaggeredGridView.java

int lookForSelectablePosition(int position, boolean lookDown) {
    final ListAdapter adapter = mAdapter;
    if (adapter == null || isInTouchMode()) {
        return INVALID_POSITION;
    }//from w w  w.j ava  2  s . c  om

    final int count = adapter.getCount();
    if (!mAreAllItemsSelectable) {
        if (lookDown) {
            position = Math.max(0, position);
            while (position < count && !adapter.isEnabled(position)) {
                position++;
            }
        } else {
            position = Math.min(position, count - 1);
            while (position >= 0 && !adapter.isEnabled(position)) {
                position--;
            }
        }

        if (position < 0 || position >= count) {
            return INVALID_POSITION;
        }
        return position;
    } else {
        if (position < 0 || position >= count) {
            return INVALID_POSITION;
        }
        return position;
    }
}