如何使gridview扩展?

时间:2019-02-11 18:01:51

标签: android firebase android-studio firebase-realtime-database

所以我有一个GRIDVIEW,其中使用FirebaseListAdapter从Firebase数据库获取的每个网格中都显示了用户的个人资料,在该网格中,我从数据库中填充了网格。

现在我想要一个展开折叠功能,其中每个网格都可以展开以显示另一个视图。

我研究了以下使用ArrayListAdapter而不是FirebaseListAdapter的存储库:

https://github.com/thewaychung/ExpandableGridView

但是当我尝试使用FirebaseListAdapter实现相同功能时,它会引发各种错误。

我的代码没有扩展功能:

public class MainActivity extends AppCompatActivity {
    GridView gv;
    FirebaseDatabase database;
    DatabaseReference db;
    FirebaseListAdapter adapter;
    Button t;
    TextInputLayout parentInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button t = findViewById(R.id.payBtn);
        final ExpandableGridView grid = findViewById(R.id.gridView);
        ExpandableGridView gv = findViewById(R.id.gridView);
        t.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this,nextActivity.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 id = v.findViewById(R.id.id);
                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());
                id.setText(" ID: "+mt.getId());
                count.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,nextactivity.class);
                        startActivity(i);
                    }
                });
            }
        };
        gv.setAdapter(adapter);
    }

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

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

当我添加有关github存储库的扩展功能时的代码:

public class MainActivity extends AppCompatActivity {
    GridView gv;
    FirebaseDatabase database;
    DatabaseReference db;
    FirebaseListAdapter adapter;
    FirebaseListAdapter adapter1;
    Button t;
    TextInputLayout parentInfo;
    @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);
        t.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this,nextactivity.class);
                startActivity(i);
            }
        });
        String[] countryData = {"China", "USA", "UK", "Russia", "France", "Germany", "Japan", "Korea", "Canada"};
        final String[] stateData = {"State A", "State B", "State C", "State D"};
        //SETUP FIREBASE
        db = FirebaseDatabase.getInstance().getReference();
        Log.i("TEST", "TEST");
        Query query = FirebaseDatabase.getInstance().getReference().child("users");
        FirebaseListOptions<martyrs> options = new FirebaseListOptions.Builder<users>()
                .setLayout(R.layout.martyrs)
                .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 id = v.findViewById(R.id.id);
                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());
                id.setText(" ID: "+mt.getId());
                count.setText(mt.getContribution().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,Nextactivity.class);
                        startActivity(i);
                    }
                });
            }
        };
        gv.setAdapter(adapter);
        //ArrayAdapter<String> countryAdapter = new ArrayAdapter<String>(getBaseContext(),R.layout.grid_item,R.id.grid_item,countryData);
        //gv.setAdapter(countryAdapter);
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                gv.expandGridViewAtView(view, new FirebaseListAdapter<String>(getBaseContext(),R.layout.users,R.id.name,stateData)); {
                });
            }
        });
        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();
        }*/
}

请帮助

0 个答案:

没有答案