使用ListAdapter填补内滚动型布局的LinearLayout [英] Using a ListAdapter to fill a LinearLayout inside a ScrollView layout

查看:55
本文介绍了使用ListAdapter填补内滚动型布局的LinearLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在面临一个非常普遍的问题: 我奠定了一个活动,现在事实证明它应该这样滚动型中显示的几个项目。正常的方式来做到这一点是使用现有的 ListAdapter ,将它连接到的ListView BOOM 的我有我的产品清单。

I'm facing a very common problem: I layed out an activity and now it turns out it should display a few items within this ScrollView. The normal way to do that would be to use the existing ListAdapter, connect it to a ListView and BOOM I'd have my list of items.

您不应该把一个嵌套的的ListView 滚动型,因为它的螺丝向上滚动 - 即便Android的林特抱怨它

BUT You should not place a nested ListView in a ScrollView as it screws up the scrolling - even Android Lint complains about it.

因此​​,这里是我的问题:

So here's my question:

我如何连接 ListAdapter 的LinearLayout 或类似的东西?

How do I connect a ListAdapter to a LinearLayout or something similar?

我知道这个解决方案将无法扩展了很多的项目,但我的名单很短(小于10项),这样的观点回用是不是真的需要。性能明智我可以忍受直接将所有意见纳入了的LinearLayout

I know this solution won't scale for a lot of items but my lists is very short (< 10 items) so reusage of views is not really needed. Performance wise I can live with placing all views directly into the LinearLayout.

我想到了会是把我现有的活动布局在<$​​ C $ C>的ListView 的headerView第一种解决方案。但是,这种感觉就像滥用这个机制,所以我正在寻找一个更清洁的解决方案。

One solution I came up with would be to place my existing activity layout in the headerView section of the ListView. But this feels like abusing this mechanism so I'm looking for a cleaner solution.

想法?

更新:为了鼓舞了正确的方向我想补充一个样本布局,以显示我的问题:

UPDATE: In order to inspire the right direction I add a sample layout to show my problem:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/news_detail_layout"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="vertical"
              android:visibility="visible">


    <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#FFF"
            >

        <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"
                android:paddingLeft="@dimen/news_detail_layout_side_padding"
                android:paddingRight="@dimen/news_detail_layout_side_padding"
                android:paddingTop="@dimen/news_detail_layout_vertical_padding"
                android:paddingBottom="@dimen/news_detail_layout_vertical_padding"
                >

            <TextView
                    android:id="@+id/news_detail_date"
                    android:layout_height="wrap_content"
                    android:layout_width="fill_parent"
                    android:gravity="center_horizontal"
                    android:text="LALALA"
                    android:textSize="@dimen/news_detail_date_height"
                    android:textColor="@color/font_black"
                    />

            <Gallery
                    android:id="@+id/news_detail_image"
                    android:layout_height="wrap_content"
                    android:layout_width="fill_parent"
                    android:paddingTop="5dip"
                    android:paddingBottom="5dip"
                    />

            <TextView
                    android:id="@+id/news_detail_headline"
                    android:layout_height="wrap_content"
                    android:layout_width="fill_parent"
                    android:gravity="center_horizontal"
                    android:text="Some awesome headline"
                    android:textSize="@dimen/news_detail_headline_height"
                    android:textColor="@color/font_black"
                    android:paddingTop="@dimen/news_detail_headline_paddingTop"
                    android:paddingBottom="@dimen/news_detail_headline_paddingBottom"
                    />

            <TextView
                    android:id="@+id/news_detail_content"
                    android:layout_height="wrap_content"
                    android:layout_width="fill_parent"
                    android:text="Here comes a lot of text so the scrollview is really needed."
                    android:textSize="@dimen/news_detail_content_height"
                    android:textColor="@color/font_black"
                    />

            <!---
                HERE I NEED THE LIST OF ITEMS PROVIDED BY THE EXISTING ADAPTER. 
                They should be positioned at the end of the content, so making the scrollview smaller is not an option.
            ---->                        

        </LinearLayout>
    </ScrollView>
</LinearLayout>

更新2 我改变了标题,使其更容易理解(有一个downvote,卫生署!)。

UPDATE 2 I changed the headline to make it easier to understand (got a downvote, doh!).

推荐答案

您也许应该只是手动添加项目的LinearLayout

You probably should just manually add your items to LinearLayout:

LinearLayout layout = ... // Your linear layout.
ListAdapter adapter = ... // Your adapter.

final int adapterCount = adapter.getCount();

for (int i = 0; i < adapterCount; i++) {
  View item = adapter.getView(i, null, null);
  layout.addView(item);
}

修改:我拒绝了这种做法,当我需要显示200不平凡的列表项,这是非常缓慢的 - 需要约2秒钟,显示我的名单的Nexus 4,这是不可接受的。所以,我转过身来,Flo的页眉做法。它的工作原理非常快,因为列表视图在用户滚动,而不是在时间创建视图时创建的需求。

EDIT: I rejected this approach when I needed to display about 200 non-trivial list items, it is very slow - Nexus 4 needed about 2 seconds to display my "list", that was unacceptable. So I turned to Flo's approach with headers. It works much faster because list views are created on demand when user scrolls, not at the time the view is created.

简介:手动添加的意见,布局更容易code(从而可能运动部件少和错误),但遭受的性能问题,所以如果你有一个像50观点或更多的,我建议使用头的方法。

Resume: The manual addition of views to layout is easier to code (thus potentially less moving parts and bugs), but suffers from performance problems, so if you have like 50 views or more, I advise to use the header approach.

为例。基本活动(或片段)布局转变为这样的事情(不滚动型需要了):

Example. Basically the activity (or fragment) layout transforms to something like this (no ScrollView needed anymore):

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/my_top_layout"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"/>

然后在 onCreateView()(我将使用同一个片段一个例子),你需要添加一个标题视图,然后设置一个适配器(我假定头资源ID为 header_layout ):

Then in onCreateView() (I'll use an example with a fragment) you need to add a header view and then set an adapter (I assume the header resource ID is header_layout):

ListView listView = (ListView) inflater.inflate(R.layout.my_top_layout, container, false);
View header = inflater.inflate(R.layout.header_layout, null);
// Initialize your header here.
listView.addHeaderView(header, null, false);

BaseAdapter adapter = // ... Initialize your adapter.
listView.setAdapter(adapter);

// Just as a bonus - if you want to do something with your list items:
view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    // You can just use listView instead of parent casted to ListView.
    if (position >= ((ListView) parent).getHeaderViewsCount()) {
      // Note the usage of getItemAtPosition() instead of adapter's getItem() because
      // the latter does not take into account the header (which has position 0).
      Object obj = parent.getItemAtPosition(position);
      // Do something with your object.
    }
  }
});

这篇关于使用ListAdapter填补内滚动型布局的LinearLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆