我认为这是我的适配器问题所在,它适用于普通的GridView,我假设我只能切换双向gridview代替GridView。但现在一个地方没有显示任何东西,而另一个地方的图像没有垂直填充视图,并且列数多于我设置的数量。
这是适配器代码:
public final class CoverGridViewAdapter extends BaseAdapter {
private final Context context;
private final List<String> urls = new ArrayList<String>();
private final ArrayList<HashMap<String,String>> arrayList;
public CoverGridViewAdapter(Context context, ArrayList<HashMap<String, String>> arrayList) {
this.context = context;
this.arrayList = arrayList;
}
@Override public View getView(int position, View convertView, ViewGroup parent) {
// Get the image URL for the current position.
//String url = getItem(position);
final HashMap<String,String> game = getItem(position);
CoverImageView view = (CoverImageView) convertView;
if (view == null) {
view = new CoverImageView(context);
view.setScaleType(CENTER_CROP);
view.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(context, GameActivity.class);
myIntent.putExtra("key", game); //Optional parameters
context.startActivity(myIntent);
}
});
}
// Trigger the download of the URL asynchronously into the image view.
Picasso.with(context) //
.load(game.get(GameDiscoveryFragment.TAG_SUPER_IMAGE)) //
.placeholder(R.drawable.ic_launcher) //TODO: placeholder image
.error(R.drawable.ic_launcher) //TODO: error image
.fit() //
.into(view);
return view;
}
@Override public int getCount() {
return arrayList.size();
}
@Override public HashMap<String,String> getItem(int position) {
//return urls.get(position);
return arrayList.get(position);
}
@Override public long getItemId(int position) {
return position;
}
指向GitHub项目的链接https://github.com/jess-anders/two-way-gridview
编辑:更多信息。我玩了一点点,虽然没有图像以纵向显示,但是当我旋转设备时它们会出现。所以也许xml也会有所帮助。
<com.ryanjohnstone.gamediscovery.app.com.jess.ui.TwoWayGridView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#E8E8E8"
android:id="@+id/similar_games_grid_view"
android:layout_width="fill_parent"
android:layout_height="115dp"
app:cacheColorHint="#E8E8E8"
app:rowHeight="80dp"
app:numColumns="@integer/similar_column_count"
app:numRows="1"
app:verticalSpacing="1dp"
app:horizontalSpacing="1dp"
app:stretchMode="spacingWidthUniform"
app:scrollDirectionPortrait="horizontal"
app:scrollDirectionLandscape="horizontal"
app:gravity="center"
android:layout_gravity="center_horizontal|bottom" />
编辑2:没有以纵向显示的原因是我需要设置columnWidth属性。当我设置了列数时,我不明白为什么这是必要的。在GridView上没有必要。在xml中是否有办法将值设置为屏幕宽度除以列数?
编辑3:设置app:stretchMode到columnWidth修复了纵向模式下的问题,我现在看到图像并填充行和列。我仍然在他们没有填充的景观中遇到问题。这是当前的xml:
<com.ryanjohnstone.gamediscovery.app.com.jess.ui.TwoWayGridView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#E8E8E8"
android:id="@+id/grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:cacheColorHint="#E8E8E8"
app:numColumns= "@integer/column_count"
app:numRows="auto_fit"
app:verticalSpacing="1dp"
app:horizontalSpacing="1dp"
app:stretchMode="columnWidth"
app:scrollDirectionPortrait="vertical"
app:scrollDirectionLandscape="horizontal"
app:gravity="center"/>
编辑4:为了澄清,它在垂直滚动的肖像中按预期工作。水平滚动的肖像不是我想要的。它显示所有图像,而不是我放在numColumns中的数字。
编辑5:我将我的xml复制并粘贴到git hub项目的示例应用程序中,它按预期工作,所以我又回来相信我的问题在于我的适配器。
答案 0 :(得分:0)
我用
解决了这个问题xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#E8E8E8"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:cacheColorHint="#E8E8E8"
app:numColumns="auto_fit"
app:numRows="1"
app:stretchMode="columnWidth"
app:scrollDirectionPortrait="vertical"
app:scrollDirectionLandscape="horizontal"
app:gravity="center"