溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時間:2020-08-06 07:10:00 來源:網(wǎng)絡(luò) 閱讀:365 作者:鏡中小白 欄目:移動開發(fā)

第一在使用XScrollView布局是,無法在該布局.xml文件,放置內(nèi)容布局控件,假如放置了會報錯

<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,通過看下面的代碼你會發(fā)現(xiàn)該控件在初始化時已經(jīng)去動態(tài)添加了一個子控件,假如你再去放置內(nèi)容布局肯定會報錯,因為android針對ScrollView的默認(rèn)設(shè)置是只允許包含唯一子空間

public class XScrollView extends ScrollViewimplements 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 該布局文件的內(nèi)部,頭部與頂部的咱們先不用管,就看中間的,ID值為content_layout,默認(rèn)我們的自定義布局是放置嵌套在其中的

<?xml version="1.0"encoding="utf-8"?>
 
<LinearLayoutxmlns: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(ViewGroupcontent) {
       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);
}

 

外部引入 ,設(shè)置內(nèi)容的函數(shù)有兩個,setContentView,setView

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

 

下面的布局文件還是用一個使用XScrollView的布局文件,內(nèi)容布局也放置在該文件中,但是跟XScrollView就不是父子的關(guān)系,而是同級的,ID xcollview_content,就是內(nèi)容布局,接下來就看代碼的

<?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中新增函數(shù) ,需要注意的一個空間它只允許有一個父控件,到此步就結(jié)束了第一個問題

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.com。

 


向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI