多个列表视图,只有一个滚动条

发布于 2024-09-13 21:20:18 字数 4254 浏览 12 评论 0原文

我需要将几个列表视图放入 LinearLayout 中。列表视图应该包装内容以使列表视图适应其项目。我只需要一个滚动条,用它可以滚动所有列表视图。我知道我不能使用滚动视图,因为列表视图有自己的滚动(但我不需要列表视图的滚动,因为我将它们放在包装内容中)。我想也许将列表视图放入 LinearLayout 中,滚动视图不会出现问题,但它不起作用,LinearLayout 固定在屏幕上,滚动视图没有出现,并且列表视图结束可滚动。如果没有滚动视图,如果我在换行内容中有列表,它们会正确显示,但我没有任何滚动,并且我看不到屏幕下方的 lisviews。真的,我已经有这个错误一个月了,我还没有找到任何解决方案,我很绝望:(

我的代码:

XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/detail_info_on_top" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:background="#FF003150"
    android:orientation="vertical">
<!--<ScrollView android:layout_width="fill_parent"-->
<!--    android:layout_height="fill_parent"-->
<!--    android:fillViewport="true">-->
    <LinearLayout 
            android:id="@+id/layout"
            android:scrollbars="vertical"
            android:scrollbarAlwaysDrawVerticalTrack="true" 
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
    </LinearLayout>
<!--</ScrollView>-->

JAVA

@Override
protected void onCreate(Bundle icicle) {
             super.onCreate(icicle);

             ...

             LinearLayout layout = (LinearLayout)findViewById(R.id.layout);
        ArrayList<SectionHolder> shs = SObjectDB.SOBJECTS.get(sobject).detail;

        for(SectionHolder sh : shs) {
        /** layout by each section */
            ListView lsvDetails = new ListView(this);
                lsvDetails.setPadding(0, 0, 0, 0);
                lsvDetails.setBackgroundColor(0xFFFFFFFF);
                lsvDetails.setCacheColorHint(Color.TRANSPARENT);
                lsvDetails.setFocusable(false);
                lsvDetails.setSelected(false);
                lsvDetails.setScrollContainer(false);
                lsvDetails.setVerticalScrollBarEnabled(false);
                LinearLayout ll = new LinearLayout(this);
                LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);           
                ll.setLayoutParams(llp);
                ll.setOrientation(LinearLayout.VERTICAL);
                ll.setFocusable(false); 

            /** setting section header */
            TextView sv = new TextView(this);
            sv.setText(sh.name);
            sv.setTextSize(15);
            sv.setTextColor(0xFFFFFFFF);
            sv.setBackgroundColor(0xFF656565);
            sv.setWidth(480);
            //sv.setTypeface(null, Typeface.BOLD);
            ll.addView(sv);

            ...

            LinkedHashMap<String,String> hm; 
            final ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
            hm = new LinkedHashMap<String, String>();
            for(FieldHolder fs : fhs) {
                hm = new LinkedHashMap<String, String>();

                     ...

                hm.put("data", v1);
                mylist.add(hm); 

            }
            ListView.LayoutParams lsvp = new ListView.LayoutParams(ListView.LayoutParams.FILL_PARENT, ListView.LayoutParams.WRAP_CONTENT);
            lsvDetails.setLayoutParams(lsvp);
            lsvDetails.setScrollbarFadingEnabled(false);
            lsvDetails.setScrollingCacheEnabled(false);
            lsvDetails.setSelector(R.drawable.multi_white_color);
            lsvDetails.setDrawSelectorOnTop(false);
            lsvDetails.setAdapter(new DetailAdapter(this, mylist, R.layout.grid_detail, new String[]{"label", "data"}, new int[]{R.id.lblName, R.id.lblData}));

            ll.addView(lsvDetails);
            layout.setVerticalScrollBarEnabled(true);
            layout.setScrollbarFadingEnabled(true);
            layout.setScrollBarStyle(50331648);
            layout.setScrollContainer(true);
            layout.setFocusable(true);
            layout.setFocusableInTouchMode(true);
            layout.addView(ll); 

        }
   }

你如何看到我以动态方式创建了 lisviews,并在布局中放置了一个节标题。如果你有疑问请询问我,我需要尽快找到解决方案

。 net/image.php?6dea1468bd.png

I need to put several listviews inner a LinearLayout. The Listviews should be wrap content to adapt the listview to their items. I need have one only scroll with which I can scroll all the listviews. I know that i can't use a scrollview because the listviews have their own scroll (but i don't need the scrolls of the listviews because i have them in wrap-content). I thought maybe puting the listviews inside the LinearLayout I will hadn't problems with the scrollview, but it didn't work, the LinearLayout was fixed to the screen, the scrollview didn't appear and the listviews ended scrollable. Without the scrollview if i have the list in wrap-content, their shown properly but i haven't any scroll and i can't see the lisviews below the screen. Really i have had this bug for one month and i haven't found any solution, i am desperate :(

My code:

XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/detail_info_on_top" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:background="#FF003150"
    android:orientation="vertical">
<!--<ScrollView android:layout_width="fill_parent"-->
<!--    android:layout_height="fill_parent"-->
<!--    android:fillViewport="true">-->
    <LinearLayout 
            android:id="@+id/layout"
            android:scrollbars="vertical"
            android:scrollbarAlwaysDrawVerticalTrack="true" 
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
    </LinearLayout>
<!--</ScrollView>-->

JAVA

@Override
protected void onCreate(Bundle icicle) {
             super.onCreate(icicle);

             ...

             LinearLayout layout = (LinearLayout)findViewById(R.id.layout);
        ArrayList<SectionHolder> shs = SObjectDB.SOBJECTS.get(sobject).detail;

        for(SectionHolder sh : shs) {
        /** layout by each section */
            ListView lsvDetails = new ListView(this);
                lsvDetails.setPadding(0, 0, 0, 0);
                lsvDetails.setBackgroundColor(0xFFFFFFFF);
                lsvDetails.setCacheColorHint(Color.TRANSPARENT);
                lsvDetails.setFocusable(false);
                lsvDetails.setSelected(false);
                lsvDetails.setScrollContainer(false);
                lsvDetails.setVerticalScrollBarEnabled(false);
                LinearLayout ll = new LinearLayout(this);
                LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);           
                ll.setLayoutParams(llp);
                ll.setOrientation(LinearLayout.VERTICAL);
                ll.setFocusable(false); 

            /** setting section header */
            TextView sv = new TextView(this);
            sv.setText(sh.name);
            sv.setTextSize(15);
            sv.setTextColor(0xFFFFFFFF);
            sv.setBackgroundColor(0xFF656565);
            sv.setWidth(480);
            //sv.setTypeface(null, Typeface.BOLD);
            ll.addView(sv);

            ...

            LinkedHashMap<String,String> hm; 
            final ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
            hm = new LinkedHashMap<String, String>();
            for(FieldHolder fs : fhs) {
                hm = new LinkedHashMap<String, String>();

                     ...

                hm.put("data", v1);
                mylist.add(hm); 

            }
            ListView.LayoutParams lsvp = new ListView.LayoutParams(ListView.LayoutParams.FILL_PARENT, ListView.LayoutParams.WRAP_CONTENT);
            lsvDetails.setLayoutParams(lsvp);
            lsvDetails.setScrollbarFadingEnabled(false);
            lsvDetails.setScrollingCacheEnabled(false);
            lsvDetails.setSelector(R.drawable.multi_white_color);
            lsvDetails.setDrawSelectorOnTop(false);
            lsvDetails.setAdapter(new DetailAdapter(this, mylist, R.layout.grid_detail, new String[]{"label", "data"}, new int[]{R.id.lblName, R.id.lblData}));

            ll.addView(lsvDetails);
            layout.setVerticalScrollBarEnabled(true);
            layout.setScrollbarFadingEnabled(true);
            layout.setScrollBarStyle(50331648);
            layout.setScrollContainer(true);
            layout.setFocusable(true);
            layout.setFocusableInTouchMode(true);
            layout.addView(ll); 

        }
   }

How you can see i create the lisviews dinamically and i put indside the layout with a secttion header. if you have some doubt ask me I need find a solution soon.

http://www.freeimagehosting.net/image.php?6dea1468bd.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

︶葆Ⅱㄣ 2024-09-20 21:20:18

我只需要一个滚动条,用它可以滚动所有列表视图。

那是不可能的,抱歉。

我有多个列表视图,具体取决于数据库,然后我将它们放入线性布局中

不要这样做。创建一个 ListView。将数据库结果合并到一个适配器中,并将该适配器放入 ListView 中。也许我的 MergeAdapter 可以提供帮助。

I need have one only scroll with which I can scroll all the listviews.

That is not possible, sorry.

i have multiple listviews depending of the Data Base and then i put them inside a linearLayout

Don't do that. Create one ListView. Combine your database results into a single adapter and put that adapter in the ListView. Perhaps my MergeAdapter can help.

居里长安 2024-09-20 21:20:18

我试图实现你的目标但失败了,我没有太多时间可以浪费,但这是我的尝试代码:

package your.packag;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class TestScrollingActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final ListView lv1 = (ListView) findViewById(R.id.listView1);
        final ListView lv2 = (ListView) findViewById(R.id.listView2);
        final ListView lv3 = (ListView) findViewById(R.id.listView3);
        final ListView lv4 = (ListView) findViewById(R.id.listView4);
        final ListView lv5 = (ListView) findViewById(R.id.listView5);
        BaseAdapter ba = new BaseAdapter() {

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                TextView tv = new TextView(parent.getContext());
                tv.setText(String.valueOf(position));
                return tv;
            }

            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return position;
            }

            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return position;
            }

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return 100;
            }
        };
        lv1.setAdapter(ba);
        lv2.setAdapter(ba);
        lv3.setAdapter(ba);
        lv4.setAdapter(ba);
        lv5.setAdapter(ba);
        OnScrollListener onScrollListener = new OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == SCROLL_STATE_IDLE) {
                    int top = view.getHeight()/view.getAdapter().getCount()*view.getFirstVisiblePosition();
                    if (lv1 != view) {
                        lv1.scrollTo(0, top);
                    }
                    if (lv2 != view) {
                        lv2.scrollTo(0, top);
                    }
                    if (lv3 != view) {
                        lv3.scrollTo(0, top);
                    }
                    if (lv4 != view) {
                        lv4.scrollTo(0, top);
                    }
                    if (lv5 != view) {
                        lv5.scrollTo(0, top);
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
            }
        };
        lv1.setOnScrollListener(onScrollListener);
        lv2.setOnScrollListener(onScrollListener);
        lv3.setOnScrollListener(onScrollListener);
        lv4.setOnScrollListener(onScrollListener);
        lv5.setOnScrollListener(onScrollListener);
        }
}

<ListView
    android:id="@+id/listView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1" >

    <!-- Preview: listitem=@android:layout/simple_list_item_1 -->
</ListView>

<ListView
    android:id="@+id/listView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1" >
</ListView>

<ListView
    android:id="@+id/listView3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1" >
</ListView>

<ListView
    android:id="@+id/listView4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1" >
</ListView>

<ListView
    android:id="@+id/listView5"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1" >
</ListView>

滚动以一种奇怪的方式失败,请务必尝试一下,如果您修复了它,请告诉我。

PS 这已经非常接近让它发挥作用了。

I was trying to achieve your goal but failed and i don't have a lot of time to waste but here is my attempt code:

package your.packag;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class TestScrollingActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final ListView lv1 = (ListView) findViewById(R.id.listView1);
        final ListView lv2 = (ListView) findViewById(R.id.listView2);
        final ListView lv3 = (ListView) findViewById(R.id.listView3);
        final ListView lv4 = (ListView) findViewById(R.id.listView4);
        final ListView lv5 = (ListView) findViewById(R.id.listView5);
        BaseAdapter ba = new BaseAdapter() {

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                TextView tv = new TextView(parent.getContext());
                tv.setText(String.valueOf(position));
                return tv;
            }

            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return position;
            }

            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return position;
            }

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return 100;
            }
        };
        lv1.setAdapter(ba);
        lv2.setAdapter(ba);
        lv3.setAdapter(ba);
        lv4.setAdapter(ba);
        lv5.setAdapter(ba);
        OnScrollListener onScrollListener = new OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == SCROLL_STATE_IDLE) {
                    int top = view.getHeight()/view.getAdapter().getCount()*view.getFirstVisiblePosition();
                    if (lv1 != view) {
                        lv1.scrollTo(0, top);
                    }
                    if (lv2 != view) {
                        lv2.scrollTo(0, top);
                    }
                    if (lv3 != view) {
                        lv3.scrollTo(0, top);
                    }
                    if (lv4 != view) {
                        lv4.scrollTo(0, top);
                    }
                    if (lv5 != view) {
                        lv5.scrollTo(0, top);
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
            }
        };
        lv1.setOnScrollListener(onScrollListener);
        lv2.setOnScrollListener(onScrollListener);
        lv3.setOnScrollListener(onScrollListener);
        lv4.setOnScrollListener(onScrollListener);
        lv5.setOnScrollListener(onScrollListener);
        }
}

<ListView
    android:id="@+id/listView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1" >

    <!-- Preview: listitem=@android:layout/simple_list_item_1 -->
</ListView>

<ListView
    android:id="@+id/listView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1" >
</ListView>

<ListView
    android:id="@+id/listView3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1" >
</ListView>

<ListView
    android:id="@+id/listView4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1" >
</ListView>

<ListView
    android:id="@+id/listView5"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1" >
</ListView>

Scrolling fails in a strange way be sure to try it and let me know if you fixed it.

P.S. this comes very close to making it work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文