淺析:Pulltorefresh使用中碰到的問題

第一 在使用XScrollView佈局是,沒法在該佈局.xml文件,放置內容佈局控件,假如放置了會報錯java

<com.markmao.pulltorefresh.widget.XScrollView
        android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/page_top"
        android:fillViewport="true"
        android:scrollbars="none" >
 
</com.markmao.pulltorefresh.widget.XScrollView>

 

XScrollView,經過看下面的代碼你會發現該控件在初始化時已經去動態添加了一個子控件,假如你再去放置內容佈局確定會報錯,由於android針對ScrollView的默認設置是隻容許包含惟一子空間android

public class XScrollView extends ScrollView implements OnScrollListener {
private LinearLayout mLayout;
    private LinearLayout mContentLayout;
public XScrollView(Context context) {
        super(context);
        initWithContext(context);
    }
 
    public XScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initWithContext(context);
    }
 
    public XScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initWithContext(context);
    }
 
    private void initWithContext(Context context) {
        mLayout = (LinearLayout) View.inflate(context, R.layout.vw_xscrollview_layout, null);
        mContentLayout = (LinearLayout) mLayout.findViewById(R.id.content_layout);this.addView(mLayout);
}

 

R.layout.vw_xscrollview_layout 該佈局文件的內部,頭部與頂部的我們先不用管,就看中間的,ID值爲 content_layout,默認咱們的自定義佈局是放置嵌套在其中的函數

<?xml version="1.0" encoding="utf-8"?>
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <LinearLayout
        android:id="@+id/header_layout"
        android:layout_gravity="center_horizontal|top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />
 
    <LinearLayout
        android:id="@+id/content_layout"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />
 
    <LinearLayout
        android:id="@+id/footer_layout"
        android:layout_gravity="center_horizontal|bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:tag="ttttt"
        android:orientation="vertical" />
 
</LinearLayout>

 

public void setContentView(ViewGroup content) {
        if (mLayout == null)
            return;
        if (mContentLayout == null)
            mContentLayout = (LinearLayout) mLayout.findViewById(R.id.content_layout);
       
        if (mContentLayout.getChildCount() > 0)
            mContentLayout.removeAllViews();
        mContentLayout.addView(content);
    }
 
    public void setView(View content) {
        if (mLayout == null)
            return;
        if (mContentLayout == null)
            mContentLayout = (LinearLayout) mLayout.findViewById(R.id.content_layout);
        mContentLayout.addView(content);
}

 

外部引入 ,設置內容的函數有兩個,setContentView,setView佈局

View content = LayoutInflater.from(this).inflate(R.layout.vw_scroll_view_content, null);
scrollview.setContentView()content;

 

下面的佈局文件仍是用一個使用XScrollView的佈局文件,內容佈局也放置在該文件中,可是跟XScrollView就不是父子的關係,而是同級的,ID xcollview_content,就是內容佈局,接下來就看代碼的this

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/test_parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 
    <com.markmao.pulltorefresh.widget.XScrollView
        android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/page_top"
        android:fillViewport="true"
        android:scrollbars="none" >
    </com.markmao.pulltorefresh.widget.XScrollView>
 
    <LinearLayout
        android:id="@+id/xcollview_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/scroll_view"
        android:background="@color/transparent"
        android:orientation="vertical" >
 
        <ListView
            android:id="@+id/content_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:cacheColorHint="#00000000"
            android:scrollbars="none" />
    </LinearLayout>
 
</RelativeLayout>

 

代碼變更,XScrollView中新增函數 ,須要注意的一個空間它只容許有一個父控件,到此步就結束了第一個問題spa

public void setDView(View content) {
        if (mLayout == null)
            return;
        if (mContentLayout == null)
            mContentLayout = (LinearLayout) mLayout.findViewById(R.id.content_layout);
 
        ViewParent parent = this.getParent();
        if (parent instanceof RelativeLayout) {
            RelativeLayout r_parent = (RelativeLayout) parent;
            r_parent.removeView(content);
        }
        if (parent instanceof LinearLayout) {
            LinearLayout l_parent = (LinearLayout) parent;
            l_parent.removeView(content);
        }
        mContentLayout.addView(content);
}


以上!另外對APP進行全方位的檢測,我都會用這個:www.ineice.comcode

相關文章
相關標籤/搜索