Example usage for android.widget ExpandableListView getFlatListPosition

List of usage examples for android.widget ExpandableListView getFlatListPosition

Introduction

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

Prototype

public int getFlatListPosition(long packedPosition) 

Source Link

Document

Converts a group and/or child position to a flat list position.

Usage

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);//  w ww.  ja v a2s . co  m
    return true;
}

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);/*from  w  w w  .j av  a 2s . c  o m*/
    }

    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:hcm.ssj.creator.dialogs.AddDialog.java

/**
 * ExpandableListView doesn't track selected items correctly, so it is done manually
 *///from  w  w w. ja  v a  2s . c  o  m
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

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

    Intent intent = getIntent();/*from  w w w.  j a va  2  s. c o m*/
    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:edu.uwp.alga.Data.DataLogFragment.java

/**
 * {@inheritDoc}/*from w ww  .  ja  v a2  s .  c om*/
 */
@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:com.robandjen.comicsapp.FullscreenActivity.java

void showCurrentComic(String url) {
    if (url == null || url.isEmpty()) {
        url = mComicList.get(mCurComic).getURL();
    }/*from   ww w.j a  va 2s  .  co m*/

    final WebView contentView = (WebView) findViewById(R.id.fullscreen_content);
    contentView.stopLoading();

    //Load about:blank to clear any extra data and have a well defined URL in history
    contentView.loadUrl("about:blank");
    contentView.loadUrl(url);
    contentView.clearHistory();
    ActionBar actionBar = getActionBar();
    if (actionBar != null) {
        actionBar.setTitle(mComicList.get(mCurComic).getName());
    }
    updateShare(url);

    ExpandableListView elv = (ExpandableListView) findViewById(R.id.comic_drawer);
    long packedPos = mAdapter.comicsPosToPackedPos(mCurComic);

    //Selection doesn't work if expandGroup isn't called or group already expanded. ?????
    elv.expandGroup(ExpandableListView.getPackedPositionGroup(packedPos));
    elv.setSelectedChild(ExpandableListView.getPackedPositionGroup(packedPos),
            ExpandableListView.getPackedPositionChild(packedPos), true);
    elv.setItemChecked(elv.getFlatListPosition(packedPos), true);
}