我在Android应用程序中工作,我正在使用带有8个图像的GridView。我想添加GridView作为ListView的页脚。添加后,添加的页脚即GridView 仅显示ListView中的前两个图像,其余图像未显示在ListView中。我想只显示了GridView的第一行。
请查看我的xml和代码
<GridView
android:layout_below="@+id/homescreen_top_RelativeLayout"
android:id="@+id/homescreen_gridview"
android:layout_margin="10dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="@dimen/gridViewcolumnWidth"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:layout_above="@+id/homescreen_logout"
android:stretchMode="columnWidth" />
public class ImageAdaptor extends BaseAdapter {
private Context mContext;
// Constructor
public ImageAdaptor(Context context) {
mContext = context;
}
public int getCount() {
return mImages.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
// get layout from mobile.xml
gridView = inflater.inflate(R.layout.subrow_gridview, null);
// set image based on selected text
ImageView imageView = (ImageView) gridView
.findViewById(R.id.grid_item_image);
TextView textView = (TextView) gridView
.findViewById(R.id.grid_item_TextView);
imageView.setImageResource(mImages[position]);
gridView.setTag(position);
textView.setText(mImagesValues[position]);
} else {
gridView = (View) convertView;
}
return gridView;
}
// Keep all Images in array
public Integer[] mImages = { R.drawable.care, R.drawable.bus,
R.drawable.monthly_summary,R.drawable.care,R.drawable.care,R.drawable.care,R.drawable.care,R.drawable.care };
// Keep all Images in array
public String[] mImagesValues = { "Service " + "\n" + "Entry",
"Bus" + "\n" + "Attendance", "Monthly" + "\n" + "Summary","Monthly" + "\n" + "Summary","Monthly" + "\n" + "Summary","Monthly" + "\n" + "Summary","Monthly" + "\n" + "Summary" };
}
答案 0 :(得分:0)
希望这是gridview的适配器,修改你的getView(),如下所示。
按如下方式添加ViewHolder类。
static class ViewHolder{
ImageView imageView;
TextView textView;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ViewHolder viewHolder;
if (convertView == null) {
// get layout from mobile.xml
convertView= inflater.inflate(R.layout.subrow_gridview, null);
// set image based on selected text
viewHolder.imageView = (ImageView) gridView
.findViewById(R.id.grid_item_image);
viewHolder.textView = (TextView) gridView
.findViewById(R.id.grid_item_TextView);
convertView.setTag(viewHolder);
} else {
viewHolder=(ViewHolder)convertView.getTag();
}
viewHolder.imageView.setImageResource(mImages[position]);
viewHolder.textView.setText(mImagesValues[position]);
return convertView;;
}
这是完全未经测试的代码。