如何在导航抽屉活动中实现自定义网格布局?

dfuffjeb  于 2021-06-27  发布在  Java
关注(0)|答案(0)|浏览(258)

我是android工作室的初学者。我知道如何创建自定义列表视图并在空白活动中实现
现在,我已经创建了一个自定义网格布局及其适配器类。
但当我尝试设置适配器时,它会出错。我需要当我点击主页它应该打开我的自定义网格布局
这是我的自定义网格布局
行.xml:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_row="3"
    android:layout_column="2"
    android:layout_height="match_parent">

    <pl.droidsonroids.gif.GifImageButton
        android:id="@+id/biceps"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:src="@drawable/biceps"
        android:layout_row="0"
        android:layout_marginBottom="30dp"
        android:layout_marginRight="15dp"

        />

    <LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/title"
            android:text="Main Title"
            android:textSize="30dp"
            android:textColor="#DF1E1E"
            android:textStyle="italic"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:padding="5dp">

        </TextView>

    </LinearLayout>

</GridLayout>

这是我的自定义适配器类

package com.example.gymapplication.adapters;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import com.example.gymapplication.R;

import pl.droidsonroids.gif.GifImageButton;

public class customGridAdapter extends ArrayAdapter<String> {

    public Activity context;
    public String titles[];
    public Integer images[];

    public customGridAdapter(Activity context, String titles[], Integer images[]){
        super(context, R.layout.row,titles);
        this.context = context;
        this.titles = titles;
        this.images = images;
    }

    public View getView(int position, View view, ViewGroup parent){
        LayoutInflater layoutInflater = context.getLayoutInflater();
        View rowVew = layoutInflater.inflate(R.layout.row,null,true);

        GifImageButton gib = (GifImageButton) rowVew.findViewById(R.id.biceps);
        TextView title = (TextView) rowVew.findViewById(R.id.title);

        title.setText(titles[position]);

        gib.setImageResource(images[position]);

        return rowVew;
    }

}

这是我的活动课:

package com.example.gymapplication.activities;

import android.app.ActionBar;
import android.os.Bundle;
import android.view.View;
import android.view.Menu;
import android.widget.GridView;
import android.widget.ListView;

import com.example.gymapplication.R;
import com.example.gymapplication.adapters.customGridAdapter;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.navigation.NavigationView;

import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

public class NavActivity extends AppCompatActivity {

    private AppBarConfiguration mAppBarConfiguration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_nav);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(R.id.nav_home, R.id.compound, R.id.isolate,R.id.myworkout).setDrawerLayout(drawer).build();

        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);

        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);

        NavigationUI.setupWithNavController(navigationView, navController);

//        String title[] = {"BICEPS", "TRICEPS", "BACK", "CHEST", "LEGS", "SHOULDER"};
//
//        Integer images[] = {R.drawable.biceps,R.drawable.triceps,R.drawable.back,R.drawable.chest,R.drawable.lgs,R.drawable.shouder};
//
//        GridView gridView= (GridView) findViewById(R.id.listview);
//
//        customGridAdapter myadapter = new customGridAdapter(this,title,images);
//        gridView.setAdapter(myadapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.nav, menu);
        return true;
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }
}

在评论部分,我试图实现它,但失败了
我该怎么做?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题