前段时间碰到个如下的情况
uiviewcontroller *a uiviewcontroller *b uibutton *c;
在a中 [a addsubview:b]; 然后 [b addsubview:c];
结果没想到c中的按键动作不能响应了。。
结果发现 在一个controller中添加另外一个controller 一定要主动添加父子关系,即
[a setchildviewController:b];
否则按键消息传递不上去。
ListView
OnScrollListener分析
ListView.setOnScrollListener(new OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { switch (scrollState) { case OnScrollListener.SCROLL_STATE_IDLE: Log.v("已经停止:SCROLL_STATE_IDLE"); break; case OnScrollListener.SCROLL_STATE_FLING: Log.v("开始滚动:SCROLL_STATE_FLING"); break; case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL: Log.v("正在滚动:SCROLL_STATE_TOUCH_SCROLL"); break; } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { } });
// 监听listview滚到最底部 mIndexList.setOnScrollListener(new OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { switch (scrollState) { // 当不滚动时 case OnScrollListener.SCROLL_STATE_IDLE: // 判断滚动到底部 if (view.getLastVisiblePosition() == (view.getCount() - 1)) { isLastisNext++; } break; } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { } });
ScrollView 需要覆盖一个新的类LazyScrollView
package com.dodowaterfall; import android.content.Context; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.widget.ScrollView; public class LazyScrollView extends ScrollView { @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt); onScrollListener.onAutoScroll(l, t, oldl, oldt); } private static final String tag = "LazyScrollView"; private Handler handler; private View view; public LazyScrollView(Context context) { super(context); } public LazyScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public LazyScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } // 这个获得总的高度 public int computeVerticalScrollRange() { return super.computeHorizontalScrollRange(); } public int computeVerticalScrollOffset() { return super.computeVerticalScrollOffset(); } private void init() { this.setOnTouchListener(onTouchListener); handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case 1: if (view.getMeasuredHeight() - 60 <= getScrollY() + getHeight()) { if (onScrollListener != null) { onScrollListener.onBottom(); } } else if (getScrollY() == 0) { if (onScrollListener != null) { onScrollListener.onTop(); } } else { if (onScrollListener != null) { onScrollListener.onScroll(); } } break; default: break; } } }; } OnTouchListener onTouchListener = new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_UP: if (view != null && onScrollListener != null) { handler.sendMessageDelayed(handler.obtainMessage(1), 200); } break; default: break; } return false; } }; /** * 获得参考的View,主要是为了获得它的MeasuredHeight,然后和滚动条的ScrollY+getHeight作比较。 */ public void getView() { this.view = getChildAt(0); if (view != null) { init(); } } /** * 定义接口 * * @author admin */ public interface OnScrollListener { void onBottom(); void onTop(); void onScroll(); void onAutoScroll(int l, int t, int oldl, int oldt); } private OnScrollListener onScrollListener; public void setOnScrollListener(OnScrollListener onScrollListener) { this.onScrollListener = onScrollListener; } }
waterfall_scroll.setOnScrollListener(new LazyScrollView.OnScrollListener() { @Override public void onTop() { // 滚动到最顶端 Log.d("LazyScroll", "Scroll to top"); } @Override public void onScroll() { if (menu_block.getVisibility() == View.VISIBLE) { menu_block.setVisibility(View.GONE); } } @Override public void onBottom() { // 滚动到最低端 // AddItemToContainer(++current_page, page_count); } @Override public void onAutoScroll(int l, int t, int oldl, int oldt) { Log.d("MainActivity", String.format("%d %d %d %d", l, t, oldl, oldt)); // Log.d("MainActivity", "range:" + range); // Log.d("MainActivity", "range-t:" + (range - t)); int scrollHeight = t; scroll_height = waterfall_scroll.getMeasuredHeight(); Log.d("MainActivity", "scroll_height:" + scroll_height); int longHeight = waterfall_scroll.getChildAt(0).getMeasuredHeight(); Log.d("MainActivity", "height:" + longHeight); // 当滚到底部时 马上加载 if (scrollHeight + scroll_height >= longHeight - 40) { if (!isFetchingNow && !noMoreProducts) { new FetchAsyncTask(false).execute(); bottom_bar.setVisibility(View.VISIBLE); } } } }
RelativeLayout.LayoutParams lp=(RelativeLayout.LayoutParams) new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.topMargin=-5;
lp.leftMargin=10;
lp.width=36;
lp.height=36;
RelativeLayout srcimg= (RelativeLayout) findViewById(R.id.relativeLayout1);//里面包含了一个图片
自适应显示
ImageView delimg=null;
delimg.setBackgroundResource(R.drawable.close);
// srcimg.addview(delimg,lp);
srcimg.addView(delimg, lp);
<RelativeLayout android:layout_width="wrap_content"
android:id="@+id/relativeLayout1" android:layout_height="wrap_content">
<ImageView android:layout_width="wrap_content"
android:id="@+id/imageView1" android:layout_height="wrap_content"
android:src="/blog_article/@drawable/icon/index.html"></ImageView>
</RelativeLayout>