Domanda

I am trying to implement a scenario in which user will selection one option among many in a group. See picture below:

enter image description here

In figure above, under group named Sauce, there are 3 options. User needs to check only 1 option among these. e.g. If user previously selected "Hot". After that he taps on "Mild" then check must disappear from "Hot" and appear on "Mild". I hope you got this point.

To achieve this approach, I need to have reference of either a group's childviews or individual checks, so that I can toggle my checkmarks. I really don't know what to do at this point... Please help me. Thanks..

childlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dip" >

    <TextView
        android:id="@+id/childname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:layout_alignParentLeft="true" 
        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
        android:textAppearance="?android:attr/textAppearanceMedium" />

  <ImageView
android:id="@+id/checkmark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/childname"
android:layout_alignParentRight="true"
android:layout_marginRight="22dp"
android:background="@android:color/transparent"
android:src="@drawable/checkmark" />
</RelativeLayout>

Child OnClick Listener

list.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

                    @Override
                    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {

                     //how to get reference to other children here to toggle checkmark of current as well as previously tapped child's checkmark?  

                        return false;
                    }
                });
È stato utile?

Soluzione

I used a hashmap to store references of all children when they were created in getChildView. In my custom adapter, I declared:

private HashMap<String, View> childviewReferences = new HashMap<String, View>();

public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {

    final String childStr = (String) getChild(groupPosition, childPosition);

       LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);//context.getLayoutInflater();

    convertView = inflator.inflate(R.layout.childlayout, null);

     TextView textview = (TextView) convertView.findViewById(R.id.childname);
                textview.setText(childStr);
    childviewReferences.put(Integer.toString(groupPosition) + Integer.toString(childPosition), convertView);

        return convertView;
    }

Child OnClick Listener

list.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

              @Override
              public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {

CreatePizzaAdapter adapter = (CreatePizzaAdapter) parent.getExpandableListAdapter();

        View conv = adapter.getChildviewReferences().get(Integer.toString(groupPosition) + Integer.toString(childPosition));

                    //now you can find any view and child and do anything
                        return true;
                    }
                });
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top