gpt4 book ai didi

android - 为每个 NavigationItem 使用一个 GridView

转载 作者:行者123 更新时间:2023-11-30 01:39:44 26 4
gpt4 key购买 nike

我的应用程序中有一个抽屉导航。抽屉导航有 3 个选项(按钮)。通过单击这些选项, GridView 将打开带有 Img 和 Textview 的选项。但现在我想对具有 3 个不同数据源的 3 个选项使用仅一个 GridView

我该怎么做?

Child_GridView.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/grid_item_bg"
android:orientation="vertical"
>

<ImageView
android:id="@+id/ppr_img"
android:layout_width="80.0dip"
android:layout_height="80.0dip"
android:layout_gravity="center"
android:layout_margin="10dp"
android:contentDescription="@string/app_name"
android:src="@drawable/no_image" />

<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="@color/light_grey" />


<TextView
android:id="@+id/ppr_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:singleLine="true"
android:textColor="@android:color/black"
android:text="@string/app_name"
android:textSize="15dp" />

</LinearLayout>

GridView_adapter.java

public class CustomGridview extends BaseAdapter {

private Context mContext;
private final String[] pprName;
private final int[] pprImg;

public CustomGridview(Context c, String[] channelname, int[] Imageid) {
mContext = c;
this.pprImg = Imageid;
this.pprName = channelname;

}

@Override
public int getCount() {
// TODO Auto-generated method stub
return pprName.length;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

View grid;

LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

if (convertView == null) {

grid = new View(mContext);

grid = inflater.inflate(R.layout.child_grid, null);
TextView textView = (TextView) grid.findViewById(R.id.ppr_name);
ImageView imageView = (ImageView) grid.findViewById(R.id.ppr_img);
textView.setText(pprName[position]);
imageView.setImageResource(pprImg[position]);
} else {
grid = (View) convertView;
}

return grid;

}

}

最佳答案

您可以在同一个 Activity (布局)上执行此操作。

在 java 上初始化变量(用于更改不同 NavIten 选择的数据的项目)。

为您的 GridView 设置默认值(用于默认选择)。

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();

if (id == R.id.option1) {
// set data on your GridView item

} else if (id == R.id.option2) {
// set data on your GridView item
}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}

关于android - 为每个 NavigationItem 使用一个 GridView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34648925/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com