GridView 中的项目不可见
Items in GridView not visible
我正在使用 gridview 来显示一些 images.I 的代码,但我仍然没有得到任何图像
自定义适配器
public class CustomAdapter extends BaseAdapter {
private Context context;
private int[] images;
public CustomAdapter(Context context, int[] images) {
this.context = context;
this.images = images;
}
@Override
public int getCount() {
return images.length;
}
@Override
public Object getItem(int position) {
return images[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.custom_trips_frag_row, parent, false);
// holder.tvHeader = (TextView) convertView.findViewById(R.id.tv_trips_home_header);
// holder.tvDate = (TextView) convertView.findViewById(R.id.tv_trips_home_date);
holder.ivImage = (ImageView) convertView.findViewById(R.id.iv_trips_home_image);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//
holder.ivImage.setImageResource(images[position]);
return convertView;
}
private class ViewHolder {
private TextView tvHeader;
private TextView tvDate;
private ImageView ivImage;
}
}
主要Activity
public class MainActivity extends MasterActivity {
private int[] images={R.drawable.images, R.drawable.images_2, R.drawable.images_1, R.drawable.images_4, R.drawable.images, R.drawable.images_2};
private GridView gridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.trips_frag);
gridView = (GridView) findViewById(R.id.grid);
gridView.setAdapter(new CustomAdapter(this, images));
}
}
自定义行Xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="@+id/iv_trips_home_image"
android:src="@drawable/images"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<!--<TextView-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_below="@+id/iv_trips_home_image"-->
<!--android:layout_centerHorizontal="true"-->
<!--android:id="@+id/tv_trips_home_header" />-->
<!--<triplogger.indiehustlers.com.triplogger.UTIL.CustomTextView-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:id="@+id/tv_trips_home_header"-->
<!--style="@style/textView"-->
<!--android:text="Berlin"-->
<!--android:textColor="#000"-->
<!--android:textStyle="bold"-->
<!--android:textSize="@dimen/header_text"-->
<!--android:layout_below="@+id/iv_trips_home_image"-->
<!--android:layout_centerHorizontal="true" />-->
<!--<triplogger.indiehustlers.com.triplogger.UTIL.CustomTextView-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="14-Sept-2014"-->
<!--style="@style/textView"-->
<!--android:visibility="gone"-->
<!--android:textColor="#23170e17"-->
<!--android:id="@+id/tv_trips_home_date"-->
<!--android:layout_below="@+id/tv_trips_home_header"-->
<!--android:layout_centerHorizontal="true" />-->
</RelativeLayout>
硕士Activity
public class MasterActivity extends Activity {
protected Context activityContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityContext = getApplicationContext();
}
}
trips_frag
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_marginRight="@dimen/margin_right"
android:layout_marginLeft="@dimen/margin_left"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/tab" />
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/tab" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2"
android:id="@+id/grid"
android:verticalSpacing="5dp"
android:stretchMode="spacingWidthUniform"></GridView>
</LinearLayout>
</LinearLayout>
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="triplogger.indiehustlers.com.triplogger">
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/TripLoggerTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我做了很多研究,但仍然没有取得任何成功
我没弄明白哪里出错了?请帮帮我
从网格视图中删除 android:stretchMode="spacingWidthUniform"。
您已经在项目布局中修复图像视图 griditem 的高度宽度
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<GridView
android:id="@+id/griddata"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2"
android:stretchMode="spacingWidthUniform"
android:verticalSpacing="5dp" >
</GridView>
</LinearLayout>
GridItemLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/iv_trips_home_image"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<!-- <TextView -->
<!-- android:layout_width="wrap_content" -->
<!-- android:layout_height="wrap_content" -->
<!-- android:layout_below="@+id/iv_trips_home_image" -->
<!-- android:layout_centerHorizontal="true" -->
<!-- android:id="@+id/tv_trips_home_header" /> -->
<!-- <triplogger.indiehustlers.com.triplogger.UTIL.CustomTextView -->
<!-- android:layout_width="wrap_content" -->
<!-- android:layout_height="wrap_content" -->
<!-- android:id="@+id/tv_trips_home_header" -->
<!-- style="@style/textView" -->
<!-- android:text="Berlin" -->
<!-- android:textColor="#000" -->
<!-- android:textStyle="bold" -->
<!-- android:textSize="@dimen/header_text" -->
<!-- android:layout_below="@+id/iv_trips_home_image" -->
<!-- android:layout_centerHorizontal="true" /> -->
<!-- <triplogger.indiehustlers.com.triplogger.UTIL.CustomTextView -->
<!-- android:layout_width="wrap_content" -->
<!-- android:layout_height="wrap_content" -->
<!-- android:text="14-Sept-2014" -->
<!-- style="@style/textView" -->
<!-- android:visibility="gone" -->
<!-- android:textColor="#23170e17" -->
<!-- android:id="@+id/tv_trips_home_date" -->
<!-- android:layout_below="@+id/tv_trips_home_header" -->
<!-- android:layout_centerHorizontal="true" /> -->
</RelativeLayout>
从 gridview 中删除 strechMode 并将 android:gravity="center" 添加到 custom_trips_frag_row.xml imageview
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2"
android:id="@+id/grid"
android:verticalSpacing="5dp">
</GridView>
浪费了大约4个小时我发现我的代码有错误。我复制了this code of a GridView
adapter and found that the GridView
didn't show images (or text labels) inside. I tried many answers from Gridview height gets cut, How to have a GridView that adapts its height when items are added,得知我的内景(ImageView
s)无法测量它们的宽度和高度。
当然,我可以设置gridView.layoutParams.height = 300
,ImageView
s会显示,但这取决于ImageView
大小。所以,我发现 ImageView
创建错误。
而不是 view = LayoutInflater.from((context)).inflate(R.layout.image_layout, null)
我必须写:
view = LayoutInflater.from((context)).inflate(R.layout.image_layout, parent, false)
在这种情况下,新视图将附加到 GridView
并且可以测量它们的大小。
所以,这里有一个 getView
方法的例子:
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val holder: ViewHolder
val view: View
if (convertView == null) {
holder = ViewHolder()
view = LayoutInflater.from((context)).inflate(layoutRes, parent, false)
view.tag = holder
holder.imageView = view as ImageView
} else {
holder = convertView.tag as ViewHolder
view = convertView
}
// Show an image with Glide, for instance.
with(getItem(position)) {
showPhoto(holder.imageView, this)
}
return view
}
Copy-paste 来自旧手册是一种耻辱。
我正在使用 gridview 来显示一些 images.I 的代码,但我仍然没有得到任何图像
自定义适配器
public class CustomAdapter extends BaseAdapter {
private Context context;
private int[] images;
public CustomAdapter(Context context, int[] images) {
this.context = context;
this.images = images;
}
@Override
public int getCount() {
return images.length;
}
@Override
public Object getItem(int position) {
return images[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.custom_trips_frag_row, parent, false);
// holder.tvHeader = (TextView) convertView.findViewById(R.id.tv_trips_home_header);
// holder.tvDate = (TextView) convertView.findViewById(R.id.tv_trips_home_date);
holder.ivImage = (ImageView) convertView.findViewById(R.id.iv_trips_home_image);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//
holder.ivImage.setImageResource(images[position]);
return convertView;
}
private class ViewHolder {
private TextView tvHeader;
private TextView tvDate;
private ImageView ivImage;
}
}
主要Activity
public class MainActivity extends MasterActivity {
private int[] images={R.drawable.images, R.drawable.images_2, R.drawable.images_1, R.drawable.images_4, R.drawable.images, R.drawable.images_2};
private GridView gridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.trips_frag);
gridView = (GridView) findViewById(R.id.grid);
gridView.setAdapter(new CustomAdapter(this, images));
}
}
自定义行Xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="@+id/iv_trips_home_image"
android:src="@drawable/images"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<!--<TextView-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_below="@+id/iv_trips_home_image"-->
<!--android:layout_centerHorizontal="true"-->
<!--android:id="@+id/tv_trips_home_header" />-->
<!--<triplogger.indiehustlers.com.triplogger.UTIL.CustomTextView-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:id="@+id/tv_trips_home_header"-->
<!--style="@style/textView"-->
<!--android:text="Berlin"-->
<!--android:textColor="#000"-->
<!--android:textStyle="bold"-->
<!--android:textSize="@dimen/header_text"-->
<!--android:layout_below="@+id/iv_trips_home_image"-->
<!--android:layout_centerHorizontal="true" />-->
<!--<triplogger.indiehustlers.com.triplogger.UTIL.CustomTextView-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="14-Sept-2014"-->
<!--style="@style/textView"-->
<!--android:visibility="gone"-->
<!--android:textColor="#23170e17"-->
<!--android:id="@+id/tv_trips_home_date"-->
<!--android:layout_below="@+id/tv_trips_home_header"-->
<!--android:layout_centerHorizontal="true" />-->
</RelativeLayout>
硕士Activity
public class MasterActivity extends Activity {
protected Context activityContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityContext = getApplicationContext();
}
}
trips_frag
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_marginRight="@dimen/margin_right"
android:layout_marginLeft="@dimen/margin_left"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/tab" />
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/tab" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2"
android:id="@+id/grid"
android:verticalSpacing="5dp"
android:stretchMode="spacingWidthUniform"></GridView>
</LinearLayout>
</LinearLayout>
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="triplogger.indiehustlers.com.triplogger">
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/TripLoggerTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我做了很多研究,但仍然没有取得任何成功 我没弄明白哪里出错了?请帮帮我
从网格视图中删除 android:stretchMode="spacingWidthUniform"。 您已经在项目布局中修复图像视图 griditem 的高度宽度
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<GridView
android:id="@+id/griddata"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2"
android:stretchMode="spacingWidthUniform"
android:verticalSpacing="5dp" >
</GridView>
</LinearLayout>
GridItemLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/iv_trips_home_image"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<!-- <TextView -->
<!-- android:layout_width="wrap_content" -->
<!-- android:layout_height="wrap_content" -->
<!-- android:layout_below="@+id/iv_trips_home_image" -->
<!-- android:layout_centerHorizontal="true" -->
<!-- android:id="@+id/tv_trips_home_header" /> -->
<!-- <triplogger.indiehustlers.com.triplogger.UTIL.CustomTextView -->
<!-- android:layout_width="wrap_content" -->
<!-- android:layout_height="wrap_content" -->
<!-- android:id="@+id/tv_trips_home_header" -->
<!-- style="@style/textView" -->
<!-- android:text="Berlin" -->
<!-- android:textColor="#000" -->
<!-- android:textStyle="bold" -->
<!-- android:textSize="@dimen/header_text" -->
<!-- android:layout_below="@+id/iv_trips_home_image" -->
<!-- android:layout_centerHorizontal="true" /> -->
<!-- <triplogger.indiehustlers.com.triplogger.UTIL.CustomTextView -->
<!-- android:layout_width="wrap_content" -->
<!-- android:layout_height="wrap_content" -->
<!-- android:text="14-Sept-2014" -->
<!-- style="@style/textView" -->
<!-- android:visibility="gone" -->
<!-- android:textColor="#23170e17" -->
<!-- android:id="@+id/tv_trips_home_date" -->
<!-- android:layout_below="@+id/tv_trips_home_header" -->
<!-- android:layout_centerHorizontal="true" /> -->
</RelativeLayout>
从 gridview 中删除 strechMode 并将 android:gravity="center" 添加到 custom_trips_frag_row.xml imageview
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2"
android:id="@+id/grid"
android:verticalSpacing="5dp">
</GridView>
浪费了大约4个小时我发现我的代码有错误。我复制了this code of a GridView
adapter and found that the GridView
didn't show images (or text labels) inside. I tried many answers from Gridview height gets cut, How to have a GridView that adapts its height when items are added,得知我的内景(ImageView
s)无法测量它们的宽度和高度。
当然,我可以设置gridView.layoutParams.height = 300
,ImageView
s会显示,但这取决于ImageView
大小。所以,我发现 ImageView
创建错误。
而不是 view = LayoutInflater.from((context)).inflate(R.layout.image_layout, null)
我必须写:
view = LayoutInflater.from((context)).inflate(R.layout.image_layout, parent, false)
在这种情况下,新视图将附加到 GridView
并且可以测量它们的大小。
所以,这里有一个 getView
方法的例子:
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val holder: ViewHolder
val view: View
if (convertView == null) {
holder = ViewHolder()
view = LayoutInflater.from((context)).inflate(layoutRes, parent, false)
view.tag = holder
holder.imageView = view as ImageView
} else {
holder = convertView.tag as ViewHolder
view = convertView
}
// Show an image with Glide, for instance.
with(getItem(position)) {
showPhoto(holder.imageView, this)
}
return view
}
Copy-paste 来自旧手册是一种耻辱。