无法获得GridView的扩展功能

时间:2019-02-12 06:14:31

标签: android firebase android-layout firebase-realtime-database android-gridview

我有一个页面,其中用户的个人资料显示在GridView中。每个网格都包含用户的个人资料(名称,ID,年龄等)以及一个将我们带到下一个屏幕的按钮。

我实现了网格扩展功能,以便当用户单击网格时,网格将扩展以显示更多信息。 我为扩展功能引用了以下github代码: https://github.com/thewaychung/ExpandableGridView

但是当我在项目中实现此功能时,我得到的扩展视图重叠在现有网格视图上,而不是在单击网格项时显示。

这是我的代码:

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();
    }
}

基本上,childView与parentView重叠,而不是在单击父项(即网格项)时显示。

这是我更新的代码:

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();
    }
}

1 个答案:

答案 0 :(得分:1)

您在这里弄错了主意。您只需要1个可扩展的gridview和2个适配器:1个用于父级,1个用于子级,就像您共享的链接一样

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));
     }
 });

您的操作方式是包含2个网格视图,因此只需要删除普通的网格视图即可。从您的代码中删除R.id.childView