首页 > 代码库 > android scrollview listview显示不全

android scrollview listview显示不全

原来处理方法是重写ListView

import android.content.Context;import android.util.AttributeSet;import android.view.MotionEvent;import android.widget.ListView;public class MyListView extends ListView {    public MyListView(Context context) {        super(context);        // TODO Auto-generated constructor stub    }    public MyListView(Context context, AttributeSet attrs) {          super(context, attrs);      }        public MyListView(Context context, AttributeSet attrs, int defStyle) {          super(context, attrs, defStyle);      }        @Override      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {          int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST);          super.onMeasure(widthMeasureSpec, expandSpec);      }        @Override      public boolean dispatchTouchEvent(MotionEvent ev) {          if(ev.getAction() == MotionEvent.ACTION_MOVE){                 return true;           }           return super.dispatchTouchEvent(ev);      }  }

试了这种方法还是显示不全 总是少二项内容

又使用了

public static void setListViewHeightBasedOnChildren(ListView listView) {        // 获取ListView对应的Adapter        ListAdapter listAdapter = listView.getAdapter();        if (listAdapter == null) {            return;        }        int totalHeight = 0;        for (int i = 0, len = listAdapter.getCount(); i < len; i++) { // listAdapter.getCount()返回数据项的数目            View listItem = listAdapter.getView(i, null, listView);            listItem.measure(0, 0); // 计算子项View 的宽高            totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度        }        ViewGroup.LayoutParams params = listView.getLayoutParams();        params.height = totalHeight                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));        // listView.getDividerHeight()获取子项间分隔符占用的高度        // params.height最后得到整个ListView完整显示需要的高度        listView.setLayoutParams(params);    }
View Code

还是一样效果 少二项内容

最后在网上找到有人重写LinearLayout显示列表

import android.content.Context;import android.util.AttributeSet;import android.util.Log;import android.util.TypedValue;import android.view.View;import android.widget.AdapterView.OnItemClickListener;import android.widget.BaseAdapter;import android.widget.LinearLayout;/*** *  * @author FreePC * */public class LinearLayoutForListView extends LinearLayout{    private BaseAdapter adapter;    private OnItemClickListener onItemClickListener;        /**     * 通过 Java代码  实例化     * @param context     */    public LinearLayoutForListView(Context context)    {        super(context);        //设置LinearLayoutForListView为垂直布局,否者默认为水平布局,容易疏忽导致子项显示不全        LinearLayoutForListView.this.setOrientation(LinearLayout.VERTICAL);    }        /**     * 此构造函数可以允许我们通过 XML的方式注册 控件     * @param context     * @param attrs     */    public LinearLayoutForListView(Context context, AttributeSet attrs)    {        super(context, attrs);        LinearLayoutForListView.this.setOrientation(LinearLayout.VERTICAL);    }            /**     * 设置适配器     *      * @param adpater     */    public void setAdapter(BaseAdapter adpater)    {        this.adapter = adpater;        bindLinearLayout();    }    /**     * 获取适配器Adapter     *      * @return adapter     */    public BaseAdapter getAdpater()    {        return adapter;    }            /**     * 绑定布局:将每个子项的视图view添加进此线性布局LinearLayout中     */    public void bindLinearLayout()    {        int count = adapter.getCount();        for (int i = 0; i < count; i++)        {            View v = adapter.getView(i, null, null);            if (i != count - 1)            {    //添加每项item之间的分割线                 v = addLine(v);            }            addView(v, i);        }        setItemClickListener();        Log.v("countTAG", "" + count);    }    /**     * 添加每项item之间的分割线     *      * @param view     * @return     */    public View addLine(View view)    {        //分割线view        View lineView = new View(view.getContext());        // 将数据从dip(即dp)转换到px,第一参数为数据原单位(此为DIP),第二参数为要转换的数据值        float fPx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,                (float) 0.5, view.getResources().getDisplayMetrics());        int iPx = Math.round(fPx);        LayoutParams layoutParams = new LayoutParams(                LinearLayout.LayoutParams.MATCH_PARENT, iPx);        lineView.setLayoutParams(layoutParams);        lineView.setBackgroundColor(view.getSolidColor());        LinearLayout ly = new LinearLayout(view.getContext());        ly.setOrientation(LinearLayout.VERTICAL);        ly.addView(view);        ly.addView(lineView);        return ly;    }        /**     * 设置点击子项事件监听对象     * @param onItemClickListener     */    public void setOnItemClickListener(OnItemClickListener onItemClickListener)    {        this.onItemClickListener = onItemClickListener;        setItemClickListener();    }        /**     * 获取点击子项事件监听对象     * @return     */    public OnItemClickListener getOnItemClickListener()    {        return onItemClickListen
View Code

这种方法完全可以搞定,在原来开发当中 上面二个方法都可以搞定不知道这次为什么不行

我怀疑是布局问题

 <ScrollView        android:id="@id/myscrollview"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="@color/v_common_line_color"        android:orientation="vertical"        android:scrollbars="none" >        <RelativeLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginBottom="8.0dip"            android:orientation="vertical" >            <FrameLayout                android:id="@id/frame_layout"                android:layout_width="match_parent"                android:layout_height="wrap_content" >                <ImageView                    android:layout_width="match_parent"                    android:layout_height="200.0dip"                    android:background="@drawable/home_cover2"                    android:scaleType="centerCrop" />                <RelativeLayout                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:layout_gravity="bottom|left"                    android:layout_marginBottom="8.0dip"                    android:layout_marginLeft="16.0dip" >                    <TextView                        android:id="@id/txt_city"                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:textColor="@color/white"                        android:textSize="@dimen/font18" />                    <TextView                        android:id="@id/txt_weather"                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:layout_below="@id/txt_city"                        android:layout_gravity="bottom|left"                        android:textColor="@color/white"                        android:textSize="@dimen/font18" />                </RelativeLayout>            </FrameLayout>            <LinearLayout                android:id="@id/linear_title"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_below="@id/frame_layout"                android:layout_marginTop="8.0dp"                android:background="@drawable/layout_background_corners"                android:orientation="vertical"                android:padding="8.0dip" >                <TextView                    android:id="@id/txtTitle"                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:gravity="center_vertical"                    android:textColor="@color/black2"                    android:textSize="@dimen/font16" />            </LinearLayout>            <ListView                android:id="@id/liv_trip"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_below="@id/linear_title"                android:layout_marginBottom="8.0dip"                android:layout_marginTop="8.0dip"                android:divider="@color/cccccc"                android:dividerHeight="0px"                android:fadingEdge="none" />        </RelativeLayout>    </ScrollView>
View Code

item布局是

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"         android:background="@color/v_common_line_color"></LinearLayout>
View Code

 

android scrollview listview显示不全