星空网 > 软件开发 > 操作系统

Pulltorefresh使用中碰到的问题

第一 在使用XScrollView布局是,无法在该布局.

  <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的默认设置是只允许包含唯一子空间

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,默认我们的自定义布局是放置嵌套在其中的
<??><LinearLayout ="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,就是内容布局,接下来就看代码的

<??><RelativeLayout ="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中新增函数 ,需要注意的一个空间它只允许有一个父控件,到此步就结束了第一个问题

  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); }

 

 

 

Pulltorefresh使用中碰到的问题images/loading.gif' data-original="file:///C:/Users/Administrator.SYT-20150831OQW/AppData/Local/YNote/data/ml0492501@163.com/aa4e7a3c4a6d4e93a4b2cbb490f4107c/clipboard.png" data-media-type="image" data-inited="true" />

 




原标题:Pulltorefresh使用中碰到的问题

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

如何快速提升Shopee店铺的产品销量?:https://www.goluckyvip.com/news/3424.html
沙特官方公布电商物流投诉榜,69%与跨境电商有关:https://www.goluckyvip.com/news/3425.html
Shopee虾皮适合做铺货模式还是精细化运营?现在是精铺时代!:https://www.goluckyvip.com/news/3426.html
又建新物流中心!亚马逊拟在剑桥(加拿大安大略省)建新仓库,预计年底投入使用:https://www.goluckyvip.com/news/3427.html
A+ 物流计划英国路向将于 UTC 时间2020年8月25日上线:https://www.goluckyvip.com/news/3428.html
玩转这5个招式,Shopee站内引流绝对杠杠的:https://www.goluckyvip.com/news/3429.html
淘宝给商家发了个“大红包” :https://www.kjdsnews.com/a/1836561.html
厦门曾厝垵家庭旅馆哪家最好?:https://www.vstour.cn/a/365183.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流