Ad

Can't Get The Expand Functionality Of The GridView

I have a page where the profiles of the users are displayed in a GridView. Each grid contains profiles of the users(name,id,age etc) along with a button which takes us to next screen.

I implemented the Grid Expand functionality such that when the user clicks the grid , the grid expands to show more information. I referenced the following github code for the expand functionality: https://github.com/thewaychung/ExpandableGridView

But when I implemented this in my project , I get the expanded view overlapped on the existing grid view instead of showing up when the grid item is clicked.

Here's my code:

public class MainActivity extends AppCompatActivity {
    GridView gv;
    FirebaseDatabase database;
    DatabaseReference db;
    FirebaseListAdapter adapter;
    FirebaseListAdapter childAdapter;
    Button t;
    GridView cv;
    TextInputLayout parentInfo;
    TextInputLayout childInfo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       final ExpandableGridView gv = findViewById(R.id.gridView);
       final GridView cv = findViewById(R.id.childView);
        t.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this,Next.class);
                startActivity(i);
            }
        });
        //SETUP FIREBASE
        db = FirebaseDatabase.getInstance().getReference();
        Log.i("TEST", "TEST");
        Query query = FirebaseDatabase.getInstance().getReference().child("users");
        FirebaseListOptions<users> options = new FirebaseListOptions.Builder<users>()
                .setLayout(R.layout.users)
                .setLifecycleOwner(MainActivity.this)
                .setQuery(query, users.class)
                .build();
        //ADAPTER
        adapter = new FirebaseListAdapter(options) {
            @Override
            protected void populateView(@NonNull View v, @NonNull Object model, int position) {
                TextView name = v.findViewById(R.id.name);
                TextView age = v.findViewById(R.id.age);
                TextView mid = v.findViewById(R.id.mid);
                ImageView image = v.findViewById(R.id.userImage);
                image.setScaleType(ImageView.ScaleType.FIT_XY);
                TextView cont = v.findViewById(R.id.cont);
                users mt = (users) model;
                name.setText(" Name: "+ mt.getName());
                age.setText(" Age: "+ mt.getAge().toString());
                mid.setText(" ID: "+ mt.getMid());
                cont.setText(mt.getCount().toString());
                Picasso.with(MainActivity.this).load(mt.getImage()).into(image);
                Button d = v.findViewById(R.id.d);
                parentInfo = v.findViewById(R.id.parentInfo);
                parentInfo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity.this,"This is the parent information",Toast.LENGTH_SHORT).show();
                    }
                });
                d.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent i = new Intent(MainActivity.this,Next.class);
                        startActivity(i);
                    }
                });
            }
        };
        gv.setAdapter(adapter);

        childAdapter = new FirebaseListAdapter(options) {
            @Override
            protected void populateView(@NonNull View v, @NonNull Object model, int position) {
                TextView name = v.findViewById(R.id.name);
                TextView age = v.findViewById(R.id.age);
                TextView mid = v.findViewById(R.id.mid);
                ImageView image = v.findViewById(R.id.userImage);
                image.setScaleType(ImageView.ScaleType.FIT_XY);
                TextView cont = v.findViewById(R.id.cont);
                users mt = (users) model;
                name.setText(" Name: "+mt.getName());
                age.setText(" Age: "+mt.getAge().toString());
                mid.setText(" ID: "+mt.getMid());
                cont.setText(mt.getCount().toString());
                Picasso.with(MainActivity.this).load(mt.getImage()).into(image);
                Button d = v.findViewById(R.id.d);
                childInfo = v.findViewById(R.id.parentInfo);
                childInfo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity.this,"This is the child information",Toast.LENGTH_SHORT).show();
                    }
                });
            }
        };
        cv.setAdapter(childAdapter);

        final ExpandableGridView parentGridView = (ExpandableGridView)findViewById(R.id.gridView);
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //expand the grid view
                gv.expandGridViewAtView(view,childAdapter);
            }
        });
        //click event listener of sub gridview
        gv.setOnExpandItemClickListener(new ExpandableGridView.OnExpandItemClickListener() {
            @Override
            public void onItemClick(int position, Object clickPositionData) {
                Toast.makeText(getBaseContext(),clickPositionData.toString()+"clicked",Toast.LENGTH_LONG).show();
            }
        });
gv.setOnExpandItemClickListener(new ExpandableGridView.OnExpandItemClickListener() {
            @Override
            public void onItemClick(int position, Object clickPositionData) {
                Toast.makeText(getBaseContext(),clickPositionData.toString()+"clicked",Toast.LENGTH_LONG).show();
            }
        });


    }

    @Override
    protected void onStart() {
        super.onStart();
        adapter.startListening();
    }

    @Override
    protected void onStop() {
        super.onStop();
        adapter.stopListening();
    }
}

Basically the childView is overlapping the parentView instead of showing up when the parent item is clicked i.e. the grid item.

This is my updated code:

public class MainActivity extends AppCompatActivity {
    GridView gv;
    FirebaseDatabase database;
    DatabaseReference db;
    FirebaseListAdapter adapter;
    FirebaseListAdapter childAdapter;
    Button t;
    GridView cv;
    TextInputLayout parentInfo;
    TextView child;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button t = findViewById(R.id.payBtn);
       final ExpandableGridView gv = findViewById(R.id.gridView);
       //final GridView cv = findViewById(R.id.childView);
        t.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this,Next.class);
                startActivity(i);
            }
        });
        //SETUP FIREBASE
        db = FirebaseDatabase.getInstance().getReference();
        Log.i("TEST", "TEST");
        Query query = FirebaseDatabase.getInstance().getReference().child("users");
        FirebaseListOptions<users> options = new FirebaseListOptions.Builder<users>()
                .setLayout(R.layout.users)
                .setLifecycleOwner(MainActivity.this)
                .setQuery(query, users.class)
                .build();
        //ADAPTER
        adapter = new FirebaseListAdapter(options) {
            @Override
            protected void populateView(@NonNull View v, @NonNull Object model, int position) {
                TextView name = v.findViewById(R.id.name);
                TextView age = v.findViewById(R.id.age);
                TextView mid = v.findViewById(R.id.mid);
                ImageView image = v.findViewById(R.id.userImage);
                image.setScaleType(ImageView.ScaleType.FIT_XY);
                TextView cont = v.findViewById(R.id.cont);
                users mt = (users) model;
                name.setText(" Name: "+ mt.getName());
                age.setText(" Age: "+ mt.getAge().toString());
                mid.setText(" ID: "+ mt.getMid());
                cont.setText(mt.getCount().toString());
                Picasso.with(MainActivity.this).load(mt.getImage()).into(image);
                Button d = v.findViewById(R.id.d);
                parentInfo = v.findViewById(R.id.parentInfo);
                parentInfo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity.this,"This is the parent information",Toast.LENGTH_SHORT).show();
                    }
                });
                d.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent i = new Intent(MainActivity.this,Payment.class);
                        startActivity(i);
                    }
                });
            }
        };
        gv.setAdapter(adapter);

        childAdapter = new FirebaseListAdapter(options) {
            @Override
            protected void populateView(@NonNull View v, @NonNull Object model, int position) {
                TextView field = v.findViewById(R.id.field);
                users mt = (users) model;
                field.setText(" Field: "+ mt.getField());
                child = v.findViewById(R.id.field);
                child.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity.this,"This is the child information",Toast.LENGTH_SHORT).show();
                    }
                });
            }
        };
  
        //final ExpandableGridView parentGridView = (ExpandableGridView)findViewById(R.id.gridView);
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //expand the grid view
                gv.expandGridViewAtView(view,childAdapter);
            }
        });
        //click event listener of sub gridview
        gv.setOnExpandItemClickListener(new ExpandableGridView.OnExpandItemClickListener() {
            @Override
            public void onItemClick(int position, Object clickPositionData) {
                Toast.makeText(getBaseContext(),clickPositionData.toString()+"clicked",Toast.LENGTH_LONG).show();
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        adapter.startListening();
    }

    @Override
    protected void onStop() {
        super.onStop();
        adapter.stopListening();
    }
}

Ad

Answer

You are getting the wrong idea here. You just need 1 expandable gridview and 2 adapters: 1 for parent and 1 for child like in the link you share

ExpandableGridView countryGridView = (ExpandableGridView)findViewById(R.id.country_grid);
 ArrayAdapter<String> countryAdapter = new ArrayAdapter<String>(getBaseContext(),R.layout.grid_item, R.id.grid_item, countryData);

 // 1.Set adapter for the grid view
 countryGridView.setAdapter(countryAdapter);

 // 2.Add click event listener to the grid view, expand grid view when item is clicked
 countryGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
     @Override
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
         // expand the grid view
         countryGridView.expandGridViewAtView(view, new ArrayAdapter<String>(getBaseContext(),
                 R.layout.grid_item, R.id.grid_item, stateData));
     }
 });

The way you are doing is you include 2 gridviews so you just need to remove the normal gridview. Remove R.id.childView from your code.

Ad
source: stackoverflow.com
Ad