首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ScrollView和GridView冲突

ScrollView和GridView冲突
EN

Stack Overflow用户
提问于 2015-08-22 14:58:40
回答 1查看 184关注 0票数 0

我使用了滚动视图,因为我的视图很长,我需要使用户能够向下滚动查看项目,如TextViews和ImageViews。然而,在底部,我需要一个GridView来显示无限的图像列表。

我需要能够滚动来查看文本视图。

我还需要能够到达GridView并滚动

然而,我意识到在滚动视图中不可能有GridView,因为它们都涉及滚动,而一些奇怪的bug使我无法滚动GridView。除了我的处境,还有什么别的办法吗?

代码示例:

代码语言:javascript
复制
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

<TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="30dp"
            android:text="Hello there"
            android:textColor="#CC000000"
            android:textSize="25sp" />
<TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="30dp"
            android:text="Partner Name: "
            android:textColor="#CC000000"
            android:textSize="25sp" />

<!-- Lot more TextViews -->

<!-- And then comes the GridView -->

 <GridView
            android:id="@+id/picturefeed"
            android:numColumns="3"
            android:layout_marginTop="20dp"
            android:layout_below="@+id/partner_details"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        </GridView>
</ScrollView>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-22 15:15:29

您需要一个带有RecyclerView的异构GridLayoutManager

您应该查看创建列表和卡片指南。

以下是一个示例:

代码语言:javascript
复制
@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_main_layout);

    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.your_recycler_view);

    final MyAdapter adapter = new MyAdapter();

    GridLayoutManager layoutManager = new GridLayoutManager(this, 3); // a row can be 3 spans wide
    layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            switch(adapter.getItemViewType(position)){
                case MyAdapter.TYPE_TEXT:
                    return 3; // it's going to take up a row
                case MyAdapter.TYPE_IMAGE:
                    return 1; // it's going to take up 1/3 of a row
                default:
                    return -1;
            }
        }
    });

    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(adapter);
}

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static final int TYPE_TEXT = 1;
    public static final int TYPE_IMAGE = 2;

    @Override
    public int getItemViewType (int position) {
        if (position < 2) { // the first two items of the RecyclerView will be the TextView ones
                            // you can (should?) use a more sophisticated method if you want to
            return TYPE_TEXT;
        }

        // the rest of the items are images
        return TYPE_IMAGE;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder;
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());

        switch (viewType) {
            case TYPE_TEXT:
                View v1 = inflater.inflate(R.layout.layout_vh_text, parent, false);
                viewHolder = new TextViewHolder(v1);
                break;
            case TYPE_IMAGE:
                View v2 = inflater.inflate(R.layout.layout_vh_image, parent, false);
                viewHolder = new ImageViewHolder(v2);
                break;
            default:
                throw new IllegalArgumentException("Unsupported view type: " + viewType);
        }

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        switch (viewHolder.getItemViewType()) {
            case TYPE_TEXT:
                TextViewHolder vh1 = (TextViewHolder) viewHolder;
                // do something with vh1
                break;
            case TYPE_IMAGE:
                ImageViewHolder vh2 = (ImageViewHolder) viewHolder;
                // do something with vh2
                break;
            default:
                throw new IllegalArgumentException("Unsupported view type: " + viewType);
        }
    }

    public class TextViewHolder extends RecyclerView.ViewHolder {
        // ...
    }

    public class ImageViewHolder extends RecyclerView.ViewHolder {
        // ...
    }

    // rest of the adapter's code, not explained here as it's not part of the question
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32157526

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档