2

我在此布局中有多个线性布局。滚动视图在我完全滚动项目时工作,但不在列表视图中。我的列表视图有多个列表项目,它一次只显示一个列表项目谢谢帮助我... ..

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/taskscrollviewid"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:scrollbars="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal"
            android:weightSum="2" >

            <Button
                android:id="@+id/txtdate"
                android:layout_width="0dip"
                android:layout_height="30px"
                android:layout_weight="1"
                android:background="@drawable/taskbuttonselector" />

            <Button
                android:id="@+id/btnTaskTimeid"
                android:layout_width="0dip"
                android:layout_height="30px"
                android:layout_weight="1"
                android:background="@drawable/taskbuttonselector" />
        </LinearLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal" >

            <EditText
                android:id="@+id/txtItem"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/edittextselector"
                android:drawableEnd="@drawable/inbox"
                android:gravity="top|left"
                android:hint="Enter task"
                android:inputType="textMultiLine"
                android:maxLines="5"
                android:minLines="3"
                android:scrollbars="vertical"
                android:singleLine="false" />

            <ImageButton
                android:id="@+id/imagebuttonid"
                android:layout_width="31dp"
                android:layout_height="36dp"
                android:layout_gravity="right|bottom"
                android:background="@drawable/inbox" />
        </FrameLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal"
            android:weightSum="2" >

            <Button
                android:id="@+id/btnaddcategoryid"
                android:layout_width="0dip"
                android:layout_height="30px"
                android:layout_weight="1"
                android:background="@drawable/taskbuttonselector"
                android:text="AddCategory" />

            <Button
                android:id="@+id/btnaddseverityid"
                android:layout_width="0dip"
                android:layout_height="30px"
                android:layout_weight="1"
                android:background="@drawable/taskbuttonselector"
                android:text="AddSeverity" />
        </LinearLayout>

        <Button
            android:id="@+id/btnadddb"
            android:layout_width="wrap_content"
            android:layout_height="30px"
            android:layout_gravity="center|center_horizontal"
            android:layout_marginTop="10dp"
            android:background="@drawable/taskbuttonselector"
            android:text="Add This Task" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal"
            android:weightSum="2" >

            <Button
                android:id="@+id/fillbut"
                android:layout_width="0dip"
                android:layout_height="30px"
                android:layout_weight="1"
                android:background="@drawable/taskbuttonselector"
                android:text="Remain Task" />

            <Button
                android:id="@+id/remainbut"
                android:layout_width="0dip"
                android:layout_height="30px"
                android:layout_weight="1"
                android:background="@drawable/taskbuttonselector"
                android:text="Finish Task" />
        </LinearLayout>

        <ListView
            android:id="@+id/listid"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:clickable="true"
            android:orientation="vertical" >
        </ListView>
    </LinearLayout>

</ScrollView>
4

4 回答 4

3

NEVER put a list view inside a scroll view or vice versa! It says so right in the documentation:

You should never use a ScrollView with a ListView, because ListView takes care of its own vertical scrolling. Most importantly, doing this defeats all of the important optimizations in ListView for dealing with large lists, since it effectively forces the ListView to display its entire list of items to fill up the infinite container supplied by ScrollView.

于 2013-07-03T13:36:19.203 回答
1

ListView 本身是可滚动的。

您不应该将 ListView 放在 ScrollView 中,因为 ListView 类实现了自己的滚动,并且它只是不接收手势,因为它们都由父 ScrollView 处理。

http://developer.android.com/reference/android/widget/ScrollView.html

您要么删除滚动视图,要么将列表视图移到滚动视图之外。

你可以检查这个链接。谈话是关于列表视图的,他们建议不要将列表视图放在滚动视图中。

https://www.youtube.com/watch?v=wDBM6wVEO70

于 2013-07-03T13:37:11.240 回答
0

像这样创建类:

public class Utility {

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        // pre-condition
        return;
    }

    int totalHeight = 0;
    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}
}

在你想要的地方调用这个方法像这样滚动你的列表视图。

Utility.setListViewHeightBasedOnChildren(YourListView);

供您参考

希望这会帮助你。

于 2013-07-03T13:39:00.990 回答
0
    //requestDisallowInterceptTouchEvent(true);
    // use above method to disable listview scroll and enabled scrollview scroll
    // to disable listview.requestDisallowInterceptTouchEvent(true);
    // to enabled scrollview scroll write as below
    // ex:  ((MainActivity) context).listview.requestDisallowInterceptTouchEvent(true);
    // i  have used above things on viewHolder.l1.setOnTouchListener(new View.OnTouchListener()



    =============================
            Main Activity
    =============================

            package com.example.scrollview_demo;

            import java.util.ArrayList;

            import com.example.scrollview_demo.Scroll_adapter.ViewHolder;

            import android.app.Activity;
            import android.os.Bundle;
            import android.view.LayoutInflater;
            import android.view.Menu;
            import android.view.MotionEvent;
            import android.view.View;
            import android.view.ViewGroup;
            import android.view.View.OnTouchListener;
            import android.widget.ArrayAdapter;
            import android.widget.LinearLayout;
            import android.widget.ListView;
            import android.widget.ScrollView;
            import android.widget.TextView;

            public class MainActivity extends Activity {
                public ListView listview;
                ArrayList<String>data;
                Scroll_adapter adapter;

                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main);
                     listview=(ListView)findViewById(R.id.listView1);
                     data=new ArrayList<String>();
                     data.add("1");
                     data.add("1");
                     data.add("1");
                     data.add("1");
                     data.add("1");
                     data.add("1");
                     data.add("1");
                     data.add("1");
                     data.add("1");
                     data.add("1");
                     data.add("1");

                     adapter=new Scroll_adapter(MainActivity.this,data);
                        listview.setAdapter(adapter);


                }


                @Override
                public boolean onCreateOptionsMenu(Menu menu) {
                    // Inflate the menu; this adds items to the action bar if it is present.
                    getMenuInflater().inflate(R.menu.main, menu);
                    return true;
                }


                public void setListfalse() {
                    listview.requestDisallowInterceptTouchEvent(false);
                }

            }

    Scroll Adapter 

         package com.example.scrollview_demo;

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class Scroll_adapter  extends ArrayAdapter<String>  
{
    private final Activity context;
    ArrayList<String>data;
    class ViewHolder 
    {
        public TextView tv_name;
        public ScrollView scroll;
        public LinearLayout l1;
    }


    public Scroll_adapter(Activity context, ArrayList<String> all_data) {
        super(context, R.layout.item_list, all_data);
        this.context = context;
        //data=all_data;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View rowView = convertView;
        ViewHolder viewHolder;
        if (rowView == null)
        {
            LayoutInflater inflater = context.getLayoutInflater();
            rowView = inflater.inflate(R.layout.item_list, null);

            viewHolder = new ViewHolder();
            viewHolder.scroll=(ScrollView)rowView.findViewById(R.id.scrollView1);
            viewHolder.l1=(LinearLayout)rowView.findViewById(R.id.layout);
            viewHolder.l1.setVerticalScrollBarEnabled(true);


            rowView.setTag(viewHolder);
        }
        else
        viewHolder = (ViewHolder) rowView.getTag();
        viewHolder.l1.removeAllViews();

        // create dynamic Linear layout and add  textview or whatever u need just add into dynamic created layout

        for(int i=0;i<10;i++)
        {
            LinearLayout LL = new LinearLayout(context);

            LL.setOrientation(LinearLayout.VERTICAL);
            LL.setPadding(0,5,0,0);        

            final TextView t1=new TextView(context);

            //t1.setText("hello");

            t1.setText(""+ (i+1));

            LL.addView(t1);

            viewHolder.l1.addView(LL);

        }
        viewHolder.l1.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub

                // stop listview scrolling and enable scrollview inside listview item

                ((MainActivity) context).listview.requestDisallowInterceptTouchEvent(true);

                return false;
            }
        });

                return rowView;

    }


}
于 2014-10-16T05:39:35.147 回答