Example usage for android.widget ExpandableListView getPackedPositionForGroup

List of usage examples for android.widget ExpandableListView getPackedPositionForGroup

Introduction

In this page you can find the example usage for android.widget ExpandableListView getPackedPositionForGroup.

Prototype

public static long getPackedPositionForGroup(int groupPosition) 

Source Link

Document

Returns the packed position representation of a group's position.

Usage

From source file:com.ephemeraldreams.gallyshuttle.ui.MainActivity.java

/**
 * Handle navigation group clicks. Header 1, "Schedules", has no actions and cannot be clicked.
 *
 * @param groupPosition Group to be selected.
 *//* w  ww.j  a  v a 2 s.  co  m*/
private void setSelectedNavigationGroup(int groupPosition) {
    switch (groupPosition) {
    case HOME_NAVIGATION_INDEX:
        currentFragment = MainFragment.newInstance();
        break;
    case POLICIES_NAVIGATION_INDEX:
        currentFragment = PoliciesFragment.newInstance();
        break;
    case ABOUT_NAVIGATION_INDEX:
        currentFragment = AboutFragment.newInstance();
        break;
    default:
        currentFragment = null;
    }
    if (groupPosition != SCHEDULE_NAVIGATION_INDEX && currentFragment != null) {
        int position = leftExpandableListView
                .getFlatListPosition(ExpandableListView.getPackedPositionForGroup(groupPosition));
        leftExpandableListView.setItemChecked(position, true);

        fragmentManager.beginTransaction().replace(R.id.container, currentFragment).commit();
    }
}

From source file:co.nerdart.ourss.fragment.FeedsListFragment.java

private void moveItem(boolean fromIsGroup, boolean toIsGroup, boolean fromIsFeedWithoutGroup, long packedPosTo,
        int packedGroupPosTo, int flatPosFrom) {
    ContentValues values = new ContentValues();
    ContentResolver cr = getActivity().getContentResolver();

    if (fromIsGroup && toIsGroup) {
        values.put(FeedColumns.PRIORITY, packedGroupPosTo + 1);
        cr.update(FeedColumns.CONTENT_URI(mListView.getItemIdAtPosition(flatPosFrom)), values, null, null);
        cr.notifyChange(FeedColumns.GROUPS_CONTENT_URI, null);

    } else if (!fromIsGroup && toIsGroup) {
        values.put(FeedColumns.PRIORITY, packedGroupPosTo + 1);
        values.putNull(FeedColumns.GROUP_ID);
        cr.update(FeedColumns.CONTENT_URI(mListView.getItemIdAtPosition(flatPosFrom)), values, null, null);
        cr.notifyChange(FeedColumns.GROUPS_CONTENT_URI, null);

    } else if ((!fromIsGroup && !toIsGroup) || (fromIsFeedWithoutGroup && !toIsGroup)) {
        int groupPrio = ExpandableListView.getPackedPositionChild(packedPosTo) + 1;
        values.put(FeedColumns.PRIORITY, groupPrio);

        int flatGroupPosTo = mListView
                .getFlatListPosition(ExpandableListView.getPackedPositionForGroup(packedGroupPosTo));
        values.put(FeedColumns.GROUP_ID, mListView.getItemIdAtPosition(flatGroupPosTo));
        cr.update(FeedColumns.CONTENT_URI(mListView.getItemIdAtPosition(flatPosFrom)), values, null, null);
        cr.notifyChange(FeedColumns.GROUPS_CONTENT_URI, null);
    }//from w ww . j a va2s.co m
}

From source file:org.totschnig.myexpenses.fragment.CategoryList.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    aggregateTypes = PrefKey.DISTRIBUTION_AGGREGATE_TYPES.getBoolean(true);
    final ManageCategories ctx = (ManageCategories) getActivity();
    View v;/*  w w w  .j  av a  2  s.  com*/
    Bundle extras = ctx.getIntent().getExtras();
    mManager = getLoaderManager();
    if (ctx.helpVariant.equals(ManageCategories.HelpVariant.distribution)) {
        showChart = PrefKey.DISTRIBUTION_SHOW_CHART.getBoolean(true);
        mMainColors = new ArrayList<>();
        for (int col : ColorTemplate.PASTEL_COLORS)
            mMainColors.add(col);
        for (int col : ColorTemplate.JOYFUL_COLORS)
            mMainColors.add(col);
        for (int col : ColorTemplate.LIBERTY_COLORS)
            mMainColors.add(col);
        for (int col : ColorTemplate.VORDIPLOM_COLORS)
            mMainColors.add(col);
        for (int col : ColorTemplate.COLORFUL_COLORS)
            mMainColors.add(col);
        mMainColors.add(ColorTemplate.getHoloBlue());

        final long id = Utils.getFromExtra(extras, KEY_ACCOUNTID, 0);
        mAccount = Account.getInstanceFromDb(id);
        if (mAccount == null) {
            TextView tv = new TextView(ctx);
            //noinspection SetTextI18n
            tv.setText("Error loading distribution for account " + id);
            return tv;
        }
        Bundle b = savedInstanceState != null ? savedInstanceState : extras;

        mGrouping = (Grouping) b.getSerializable("grouping");
        if (mGrouping == null)
            mGrouping = Grouping.NONE;
        mGroupingYear = b.getInt("groupingYear");
        mGroupingSecond = b.getInt("groupingSecond");
        getActivity().supportInvalidateOptionsMenu();
        mManager.initLoader(SUM_CURSOR, null, this);
        mManager.initLoader(DATEINFO_CURSOR, null, this);
        v = inflater.inflate(R.layout.distribution_list, container, false);
        mChart = (PieChart) v.findViewById(R.id.chart1);
        mChart.setVisibility(showChart ? View.VISIBLE : View.GONE);
        mChart.setDescription("");

        //Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), "OpenSans-Regular.ttf");

        //mChart.setValueTypeface(tf);
        //mChart.setCenterTextTypeface(Typeface.createFromAsset(getActivity().getAssets(), "OpenSans-Light.ttf"));
        //mChart.setUsePercentValues(true);
        //mChart.setCenterText("Quarterly\nRevenue");
        TypedValue typedValue = new TypedValue();
        getActivity().getTheme().resolveAttribute(android.R.attr.textAppearanceMedium, typedValue, true);
        int[] textSizeAttr = new int[] { android.R.attr.textSize };
        int indexOfAttrTextSize = 0;
        TypedArray a = getActivity().obtainStyledAttributes(typedValue.data, textSizeAttr);
        int textSize = a.getDimensionPixelSize(indexOfAttrTextSize, -1);
        a.recycle();
        mChart.setCenterTextSizePixels(textSize);

        // radius of the center hole in percent of maximum radius
        //mChart.setHoleRadius(60f); 
        //mChart.setTransparentCircleRadius(0f);
        mChart.setDrawSliceText(false);
        mChart.setDrawHoleEnabled(true);
        mChart.setDrawCenterText(true);
        mChart.setRotationEnabled(false);
        mChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {

            @Override
            public void onValueSelected(Entry e, int dataSetIndex) {
                int index = e.getXIndex();
                long packedPosition = (lastExpandedPosition == -1)
                        ? ExpandableListView.getPackedPositionForGroup(index)
                        : ExpandableListView.getPackedPositionForChild(lastExpandedPosition, index);
                int flatPosition = mListView.getFlatListPosition(packedPosition);
                mListView.setItemChecked(flatPosition, true);
                mListView.smoothScrollToPosition(flatPosition);
                setCenterText(index);
            }

            @Override
            public void onNothingSelected() {
                mListView.setItemChecked(mListView.getCheckedItemPosition(), false);
            }
        });
    } else {
        v = inflater.inflate(R.layout.categories_list, container, false);
        if (savedInstanceState != null) {
            mFilter = savedInstanceState.getString("filter");
        }
    }
    incomeSumTv = (TextView) v.findViewById(R.id.sum_income);
    expenseSumTv = (TextView) v.findViewById(R.id.sum_expense);
    bottomLine = v.findViewById(R.id.BottomLine);
    updateColor();
    mListView = (ExpandableListView) v.findViewById(R.id.list);
    final View emptyView = v.findViewById(R.id.empty);
    mListView.setEmptyView(emptyView);
    mImportButton = emptyView.findViewById(R.id.importButton);
    mManager.initLoader(SORTABLE_CURSOR, null, this);
    String[] from;
    int[] to;
    if (mAccount != null) {
        from = new String[] { KEY_LABEL, KEY_SUM };
        to = new int[] { R.id.label, R.id.amount };
    } else {
        from = new String[] { KEY_LABEL };
        to = new int[] { R.id.label };
    }
    mAdapter = new MyExpandableListAdapter(ctx, null, R.layout.category_row, R.layout.category_row, from, to,
            from, to);
    mListView.setAdapter(mAdapter);
    if (ctx.helpVariant.equals(ManageCategories.HelpVariant.distribution)) {
        mListView.setOnGroupExpandListener(new OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
                if (showChart) {
                    if (lastExpandedPosition != -1 && groupPosition != lastExpandedPosition) {
                        mListView.collapseGroup(lastExpandedPosition);
                    }
                }
                lastExpandedPosition = groupPosition;
            }
        });
        mListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
            @Override
            public void onGroupCollapse(int groupPosition) {
                if (showChart) {
                    lastExpandedPosition = -1;
                    setData(mGroupCursor, mMainColors);
                    highlight(groupPosition);
                    long packedPosition = ExpandableListView.getPackedPositionForGroup(groupPosition);
                    int flatPosition = mListView.getFlatListPosition(packedPosition);
                    mListView.setItemChecked(flatPosition, true);
                }
            }
        });
        mListView.setOnChildClickListener(new OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition,
                    long id) {
                if (showChart) {
                    long packedPosition = ExpandableListView.getPackedPositionForChild(groupPosition,
                            childPosition);
                    highlight(childPosition);
                    int flatPosition = mListView.getFlatListPosition(packedPosition);
                    mListView.setItemChecked(flatPosition, true);
                    return true;
                }
                return false;
            }
        });
        //the following is relevant when not in touch mode
        mListView.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (showChart) {
                    long pos = mListView.getExpandableListPosition(position);
                    int type = ExpandableListView.getPackedPositionType(pos);
                    int group = ExpandableListView.getPackedPositionGroup(pos),
                            child = ExpandableListView.getPackedPositionChild(pos);
                    int highlightedPos;
                    if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
                        if (lastExpandedPosition != group) {
                            mListView.collapseGroup(lastExpandedPosition);
                        }
                        highlightedPos = lastExpandedPosition == -1 ? group : -1;
                    } else {
                        highlightedPos = child;
                    }
                    highlight(highlightedPos);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        registerForContextMenu(mListView);
    } else {
        registerForContextualActionBar(mListView);
    }
    return v;
}

From source file:com.viktorrudometkin.burramys.fragment.EditFeedsListFragment.java

private void moveItem(boolean fromIsGroup, boolean toIsGroup, boolean fromIsFeedWithoutGroup, long packedPosTo,
        int packedGroupPosTo, int flatPosFrom) {
    ContentValues values = new ContentValues();
    ContentResolver cr = getActivity().getContentResolver();

    if (fromIsGroup && toIsGroup) {
        values.put(FeedColumns.PRIORITY, packedGroupPosTo + 1);
        cr.update(FeedColumns.CONTENT_URI(mListView.getItemIdAtPosition(flatPosFrom)), values, null, null);
    } else if (!fromIsGroup && toIsGroup) {
        values.put(FeedColumns.PRIORITY, packedGroupPosTo + 1);
        values.putNull(FeedColumns.GROUP_ID);
        cr.update(FeedColumns.CONTENT_URI(mListView.getItemIdAtPosition(flatPosFrom)), values, null, null);
    } else if ((!fromIsGroup && !toIsGroup) || (fromIsFeedWithoutGroup && !toIsGroup)) {
        int groupPrio = ExpandableListView.getPackedPositionChild(packedPosTo) + 1;
        values.put(FeedColumns.PRIORITY, groupPrio);

        int flatGroupPosTo = mListView
                .getFlatListPosition(ExpandableListView.getPackedPositionForGroup(packedGroupPosTo));
        values.put(FeedColumns.GROUP_ID, mListView.getItemIdAtPosition(flatGroupPosTo));
        cr.update(FeedColumns.CONTENT_URI(mListView.getItemIdAtPosition(flatPosFrom)), values, null, null);
    }//from   w w  w .  j  ava  2s . c o  m
}

From source file:org.totschnig.myexpenses.fragment.CategoryList.java

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor c) {
    if (getActivity() == null)
        return;// ww  w.  j  a  v a2  s  .c  om
    int id = loader.getId();
    ProtectedFragmentActivity ctx = (ProtectedFragmentActivity) getActivity();
    ActionBar actionBar = ctx.getSupportActionBar();
    switch (id) {
    case SUM_CURSOR:
        boolean[] seen = new boolean[2];
        c.moveToFirst();
        while (c.isAfterLast() == false) {
            int type = c.getInt(c.getColumnIndex(KEY_TYPE));
            updateSum(type > 0 ? "+ " : "- ", type > 0 ? incomeSumTv : expenseSumTv,
                    c.getLong(c.getColumnIndex(KEY_SUM)));
            c.moveToNext();
            seen[type] = true;
        }
        //if we have no income or expense, there is no row in the cursor
        if (!seen[1])
            updateSum("+ ", incomeSumTv, 0);
        if (!seen[0])
            updateSum("- ", expenseSumTv, 0);
        break;
    case DATEINFO_CURSOR:
        c.moveToFirst();
        actionBar.setSubtitle(mGrouping.getDisplayTitle(ctx, mGroupingYear, mGroupingSecond, c));
        thisYear = c.getInt(c.getColumnIndex(KEY_THIS_YEAR));
        thisMonth = c.getInt(c.getColumnIndex(KEY_THIS_MONTH));
        thisWeek = c.getInt(c.getColumnIndex(KEY_THIS_WEEK));
        thisDay = c.getInt(c.getColumnIndex(KEY_THIS_DAY));
        maxValue = c.getInt(c.getColumnIndex(KEY_MAX_VALUE));
        break;
    case SORTABLE_CURSOR:
        mGroupCursor = c;
        mAdapter.setGroupCursor(c);
        if (ctx.helpVariant.equals(ManageCategories.HelpVariant.distribution)) {
            if (c.getCount() > 0) {
                if (lastExpandedPosition == -1) {
                    mChart.setVisibility(showChart ? View.VISIBLE : View.GONE);
                    setData(c, mMainColors);
                    highlight(0);
                    if (showChart)
                        mListView.setItemChecked(
                                mListView.getFlatListPosition(ExpandableListView.getPackedPositionForGroup(0)),
                                true);
                }
            } else {
                mChart.setVisibility(View.GONE);
            }
        }
        if (mAccount != null) {
            actionBar.setTitle(mAccount.label);
        }
        invalidateCAB(); //only need to do this for group since children's cab does not depnd on cursor
        break;
    default:
        //check if group still exists
        if (mAdapter.getGroupId(id) != 0) {
            mAdapter.setChildrenCursor(id, c);
            if (ctx.helpVariant.equals(ManageCategories.HelpVariant.distribution)) {
                long packedPosition;
                if (c.getCount() > 0) {
                    mSubColors = getSubColors(mMainColors.get(id % mMainColors.size()));
                    setData(c, mSubColors);
                    highlight(0);
                    packedPosition = ExpandableListView.getPackedPositionForChild(id, 0);
                } else {
                    packedPosition = ExpandableListView.getPackedPositionForGroup(id);
                    if (!chartDisplaysSubs) {//check if a loader running concurrently has already switched to subs
                        highlight(id);
                    }
                }
                if (showChart && id == lastExpandedPosition) {
                    //if user has expanded a new group before loading is finished, getFlatListPosition
                    //would run in an NPE
                    mListView.setItemChecked(mListView.getFlatListPosition(packedPosition), true);
                }
            }
        }
    }
}