Example usage for android.widget ExpandableListView getPackedPositionForChild

List of usage examples for android.widget ExpandableListView getPackedPositionForChild

Introduction

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

Prototype

public static long getPackedPositionForChild(int groupPosition, int childPosition) 

Source Link

Document

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

Usage

From source file:de.grobox.liberario.PickNetworkProviderActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pick_network_provider);

    Intent intent = getIntent();/*from w  w w.  j av a  2s.com*/
    if (intent.getBooleanExtra("FirstRun", false)) {
        // hide cancel button on first run
        findViewById(R.id.cancelNetworkProviderButton).setVisibility(View.GONE);
        // prevent going back
        back = false;
        // show first time notice
        findViewById(R.id.firstRunTextView).setVisibility(View.VISIBLE);
    } else {
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

    expListView = (ExpandableListView) findViewById(R.id.expandableNetworkProviderListView);

    prepareListData();

    listAdapter = new NetworkProviderListAdapter(this, listRegion, listNetwork);
    expListView.setAdapter(listAdapter);

    selectItem();

    expListView.setOnChildClickListener(new OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition,
                long id) {
            int index = parent.getFlatListPosition(
                    ExpandableListView.getPackedPositionForChild(groupPosition, childPosition));
            parent.setItemChecked(index, true);
            selectedRegion = groupPosition;
            return false;
        }
    });

    // on OK click
    Button button = (Button) findViewById(R.id.pickNetworkProviderButton);
    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (expListView.getCheckedItemPosition() >= 0) {
                String region = listAdapter.getGroup(selectedRegion);
                String network = ((NetworkItem) expListView
                        .getItemAtPosition(expListView.getCheckedItemPosition())).id.name();

                SharedPreferences settings = getSharedPreferences(Preferences.PREFS, Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = settings.edit();

                editor.putString("NetworkRegion", region);
                editor.putString("NetworkId", network);

                editor.commit();

                Intent returnIntent = new Intent();
                setResult(RESULT_OK, returnIntent);
                finish();
            } else {
                Toast.makeText(getBaseContext(), getResources().getText(R.string.error_pick_network),
                        Toast.LENGTH_SHORT).show();
            }
        }
    });

    // on Cancel click
    Button button_cancel = (Button) findViewById(R.id.cancelNetworkProviderButton);
    button_cancel.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent returnIntent = new Intent();
            setResult(RESULT_CANCELED, returnIntent);
            finish();
        }
    });
}

From source file:com.chatwing.whitelabel.fragments.BaseExpandableListFragment.java

@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
    // Highlight selected chat box
    int flatPosition = parent
            .getFlatListPosition(ExpandableListView.getPackedPositionForChild(groupPosition, childPosition));
    parent.setItemChecked(flatPosition, true);

    Cursor c = mAdapter.getChild(groupPosition, childPosition);
    onChildClicked(c);/*www .  j  av a2  s .  c  om*/
    return true;
}

From source file:edu.uwp.alga.Data.DataLogFragment.java

/**
 * {@inheritDoc}/*from w ww .  j  a  v a 2 s  .co  m*/
 */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    rootView = inflater.inflate(R.layout.fragment_datalog, container, false);
    //Save data
    Context context = getActivity();

    getDataLogFile(context);
    if (DataLog.size() > 0) {
        populateLogAdapter();
        logAdapter = new LogAdapter(context, LogHeader, LogChild);
        dataList = (ExpandableListView) rootView.findViewById(R.id.data_log_list);
        dataList.setAdapter(logAdapter);
        for (int i = 0; i < LogHeader.size(); i++) {
            dataList.expandGroup(i);
        }
        dataList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                return true;
            }
        });

        dataList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition,
                    long id) {
                int index = parent.getFlatListPosition(
                        ExpandableListView.getPackedPositionForChild(groupPosition, childPosition));
                parent.setItemChecked(index, true);
                String data = (LogChild.get(LogHeader.get(groupPosition))).get(childPosition);
                String logLoc = data.substring(26, data.length());

                Intent intent = new Intent(getActivity(), SubmitActivity.class);
                intent.putExtra("LogFile", logLoc);
                startActivity(intent);

                return false;
            }
        });
    }

    return rootView;
}

From source file:de.grobox.liberario.activities.PickNetworkProviderActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pick_network_provider);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    if (toolbar != null) {
        setSupportActionBar(toolbar);// w  w  w  . j  a v  a  2  s .  com
    }

    Intent intent = getIntent();
    if (intent.getBooleanExtra("FirstRun", false)) {
        // prevent going back
        back = false;
        // show first time notice
        findViewById(R.id.firstRunTextView).setVisibility(View.VISIBLE);
    } else {
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null)
            actionBar.setDisplayHomeAsUpEnabled(true);
        findViewById(R.id.firstRunTextView).setVisibility(View.GONE);
    }

    expListView = (ExpandableListView) findViewById(R.id.expandableNetworkProviderListView);

    HashMap<String, List<TransportNetwork>> listNetwork = ((TransportrApplication) getApplicationContext())
            .getTransportNetworks(this).getHashMapByRegion();
    List<String> listRegion = new ArrayList<>(listNetwork.keySet());
    Collections.sort(listRegion);

    listAdapter = new NetworkProviderListAdapter(this, listRegion, listNetwork);
    expListView.setAdapter(listAdapter);

    selectItem();

    expListView.setOnChildClickListener(new OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition,
                long id) {
            int index = parent.getFlatListPosition(
                    ExpandableListView.getPackedPositionForChild(groupPosition, childPosition));
            parent.setItemChecked(index, true);
            //if(parent.getCheckedItemPosition() >= 0) {
            if (index >= 0) {
                //TransportNetwork network = ((TransportNetwork) parent.getItemAtPosition(parent.getCheckedItemPosition()));
                TransportNetwork network = ((TransportNetwork) parent.getItemAtPosition(index));

                Preferences.setNetworkId(v.getContext(), network.getId());

                Intent returnIntent = new Intent();
                setResult(RESULT_OK, returnIntent);
                close();
                return true;
            }
            return false;
        }
    });
}

From source file:de.grobox.liberario.activities.PickNetworkProviderActivity.java

private void selectItem() {
    // get current network from settings
    TransportNetwork tn = Preferences.getTransportNetwork(this);

    // return if no network is set
    if (tn == null || tn.getIdString() == null || tn.getRegion() == null) {
        Log.d(getClass().getSimpleName(), "No NetworkId in Settings.");
        return;/*from w  w w  .j a v a 2  s .  c o  m*/
    }

    // we have a network, so pre-select it in the list
    int region = listAdapter.getGroupPos(tn.getRegion());
    int network = listAdapter.getChildPos(tn.getRegion(), tn.getId());
    if (network >= 0) {
        expListView.expandGroup(region);
        int index = expListView
                .getFlatListPosition(ExpandableListView.getPackedPositionForChild(region, network));
        expListView.setItemChecked(index, true);
    }
}

From source file:hcm.ssj.creator.dialogs.AddDialog.java

/**
 * ExpandableListView doesn't track selected items correctly, so it is done manually
 *//*from   ww w  .  j  a  va  2s .  c om*/
private void setListListeners() {
    listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition,
                long id) {
            int index = parent.getFlatListPosition(
                    ExpandableListView.getPackedPositionForChild(groupPosition, childPosition));
            parent.setItemChecked(index, !parent.isItemChecked(index));
            itemState[groupPosition][childPosition] = !itemState[groupPosition][childPosition];
            return true;
        }
    });
    listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
            //get current position
            int currentPosition = 1;
            for (int i = 0; i < groupPosition; i++, currentPosition++) {
                if (listView.isGroupExpanded(i)) {
                    currentPosition += listView.getExpandableListAdapter().getChildrenCount(i);
                }
            }
            //shift values as needed
            int children = listView.getExpandableListAdapter().getChildrenCount(groupPosition);
            if (listView.isGroupExpanded(groupPosition)) {
                //currently closing
                for (int i = currentPosition; i < allItems; i++) {
                    listView.setItemChecked(i, listView.isItemChecked(i + children));
                }
            } else {
                //currently expanding
                for (int i = allItems + 1; i > currentPosition; i--) {
                    listView.setItemChecked(i, listView.isItemChecked(i - children));
                }
                //set values for expanded group from memory
                for (int i = currentPosition, j = 0; j < children; i++, j++) {
                    listView.setItemChecked(i, itemState[groupPosition][j]);
                }
            }
            return false;
        }
    });
}

From source file:de.grobox.liberario.PickNetworkProviderActivity.java

private void selectItem() {
    // get current network from settings
    SharedPreferences settings = getSharedPreferences(Preferences.PREFS, Context.MODE_PRIVATE);
    String network_string = settings.getString("NetworkId", null);
    String region_string = settings.getString("NetworkRegion", null);

    // return if no network is set
    if (network_string == null || region_string == null) {
        Log.d(getClass().getSimpleName(), "No NetworkId in Settings.");
        return;//from   w  ww  . j a  v a 2  s .c om
    }

    // construct NetworkId object from network string
    NetworkId network_id;
    try {
        network_id = NetworkId.valueOf(network_string);
    } catch (IllegalArgumentException e) {
        Log.d(getClass().getSimpleName(), "Invalid NetworkId in Settings.");
        return;
    }

    // we have a network, so pre-select it in the list
    int region = listAdapter.getGroupPos(region_string);
    int network = listAdapter.getChildPos(region_string, network_id);
    if (network >= 0) {
        expListView.expandGroup(region);
        selectedRegion = region;
        int index = expListView
                .getFlatListPosition(ExpandableListView.getPackedPositionForChild(region, network));
        expListView.setItemChecked(index, true);
    }
}

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

/**
 * Handle navigation child clicks. Group 0's children are the only clickable children.
 *
 * @param groupPosition Group to find child to be selected.
 * @param childPosition Child to select.
 *//*  w w  w .  j a va 2s  .  com*/
private void setSelectedNavigationChild(int groupPosition, int childPosition) {
    if (groupPosition == SCHEDULE_NAVIGATION_INDEX) {
        int position = leftExpandableListView.getFlatListPosition(
                ExpandableListView.getPackedPositionForChild(groupPosition, childPosition));
        leftExpandableListView.setItemChecked(position, false);
        ScheduleActivity.launchActivity(this, childPosition);
    }
}

From source file:com.mycodehurts.rapidmath.app.NavigationDrawerFragment.java

private void selectChildItem(int i, int i2) {
    mCurrentSelectedChild = i2;// www  .  j a v a 2s.com
    Log.i("JS", i + " " + i2);
    if (mDrawerListView != null) {
        int index = mDrawerListView.getFlatListPosition(ExpandableListView.getPackedPositionForChild(i, i2));
        mDrawerListView.setItemChecked(index, true);

    }
    if (mDrawerLayout != null) {
        mDrawerLayout.closeDrawer(mFragmentContainerView);
    }
    if (mCallbacks != null) {
        mCallbacks.onNavigationDrawerChildItemSelected(i, i2);
    }
}

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;//from   ww  w.ja v  a 2s  .  co  m
    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;
}