我的应用程序中有一个导航抽屉。导航抽屉有3个选项(按钮)。 单击这些选项,将使用Img和Textview打开网格视图。 但现在我想使用仅一个网格视图来获得具有3个不同数据源的3个选项。
我该怎么做?
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;
}
}
答案 0 :(得分:1)
您可以在同一个活动(布局)上执行此操作。
在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;
}