【问题标题】:Having trouble implementing two-way-gridview在实现双向网格视图时遇到问题
【发布时间】:2014-11-05 21:31:12
【问题描述】:

我认为问题出在我的适配器上,它在普通的 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 项目的示例应用程序中,它按预期工作,所以我重新相信我的问题在于我的适配器。

【问题讨论】:

  • 也许你链接到你使用的两种方式的gridview源(我猜是github项目?)
  • 哦,是的,对不起。使用项目链接编辑问题。

标签: android


【解决方案1】:

我解决了这个问题

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" 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多