viewdragerhelper 里可以用listview吗
推出了ViewDragHelper这个类。
可以极大方便我们自定义viewgroup.
先看一个简单效果 一个layout里有2个图片 其中有一个可以滑动 一个不能滑
这个效果其实还蛮简单的(原谅我让臭脚不能动 让BABY动)
布局文件:
1
2
6
7 .example.administrator.viewdragertestapp.DragLayout
8 android:layout_width="match_parent"
9 android:layout_height="match_parent"
10 android:orientation="vertical">
11
12
18
19
25
26
27 <.example.administrator.viewdragertestapp.DragLayout>
28
29
然后我们看一下自定义的layout 如何实现2个子view 一个可以滑动 一个不能滑动的
1 .example.administrator.viewdragertestapp;
2
3 import android.content.Context;
4 import android.support.v4.widget.ViewDragHelper;
5 import android.util.AttributeSet;
6 import android.view.MotionEvent;
7 import android.view.View;
8 import android.widget.ImageView;
9 import android.widget.LinearLayout;
10 import android.widget.TextView;
11
12 /**
13 * Created by Administrator on 2015/8/12.
14 */
15 public class DragLayout extends LinearLayout {
16
17 private ViewDragHelper mDragger;
18
19 private ViewDragHelper.Callback callback;
20
21 private ImageView iv1;
22 private ImageView iv2;
23
24 @Override
25 protected void onFinishInflate() {
26 iv1 = (ImageView) this.findViewById(R.id.iv1);
27 iv2 = (ImageView) this.findViewById(R.id.iv2);
28 super.onFinishInflate();
29
30 }
31
32 public DragLayout(Context context) {
33 super(context);
34
35 }
36
37 public DragLayout(Context context, AttributeSet attrs) {
38 super(context, attrs);
39 callback = new DraggerCallBack();
40 //第二个参数就是滑动灵敏度的意思 可以随意设置
41 mDragger = ViewDragHelper.create(this, 1.0f, callback);
42 }
43
44 class DraggerCallBack extends ViewDragHelper.Callback {
45
46 //这个地方实际上函数返回值为true就代表可以滑动 为false 则不能滑动
47 @Override
48 public boolean tryCaptureView(View child, int pointerId) {
49 if (child == iv2) {
50 return false;
51 }
52 return true;
53 }
54
55 @Override
56 public int clampViewPositionHorizontal(View child, int left, int dx) {
57 return left;
58 }
59
60 @Override
61 public int clampViewPositionVertical(View child, , int dy) {
62 ;
63 }
64 }
65
66
67 @Override
68 public boolean onInterceptTouchEvent(MotionEvent ev) {
69 //决定是否拦截当前事件
70 return mDragger.shouldInterceptTouchEvent(ev);
71 }
72
73 @Override
74 public boolean onTouchEvent(MotionEvent event) {
75 //处理事件
76 mDragger.processTouchEvent(event);
77 return true;
78 }
79
80
81 }如何给gridview的注脚添加onclick事件
如何给gridview的注脚添加onclick事件
public class SinglePaneContainer extends FrameLayout implements Container { private ItemListView listView; public SinglePaneContainer(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onFinishInflate() { super.onFinishInflate(); listView = (ItemListView) getChildAt(0); } public boolean onBackPressed() { if (!listViewAttached()) { removeViewAt(0); addView(listView); return true; } return false; } @Override public void showItem(String item) { if (listViewAttached()) { removeViewAt(0); View.inflate(getContext(), R.layout.detail, this); } MyDetailView detailView = (MyDetailView) getChildAt(0); detailView.setItem(item); } private boolean listViewAttached() { return listView.getParent() != null; } }Android 如何判断一个View重绘或加载完成?
可以设置这个回调函数
//view重绘时回调
view.getViewTreeObserver().addOnDrawListener(new?OnDrawListener()?{
@Override
public?void?onDraw()?{
//?TODO?Auto-generated?method?stub
}
});//view加载完成时回调
view.getViewTreeObserver().addOnGlobalLayoutListener(new?OnGlobalLayoutListener()?{
@Override
public?void?onGlobalLayout()?{
//?TODO?Auto-generated?method?stub
}
});getMenuInflater().inflate(R.menu... menu字变红,什么问题?
在activity类中有一个getmenuinflater()的函数用来返回这个activity的menuinflater,并通过menuinflater对象来设置menu xml里的menu作为该activity的菜单。
setContentView,findViewById 和 inflate 的区别
通俗的说,inflate就相当于将一个xml中定义的布局找出来.
因为在一个Activity里如果直接用findViewById()的话,对应的是setConentView()的那个layout里的组件.
因此如果你的
Activity里如果用到别的layout,比如对话框上的layout,你还要设置对话框上的layout里的组件(像图片
ImageView,文字TextView)上的内容,你就必须用inflate()先将对话框上的layout找出来,然后再用这个layout对象去
找到它上面的组件,如:
View view = View.inflate(this, R.layout.dialog_layout, null);
TextView dialogTV = (TextView) view.findViewById();
dialogTV.setText("abcd");
如果组件是对话框上的组件,而你直接用this.findViewById(),那么返回值将是null
三种方式可以生成LayoutInflater :
LayoutInflater inflater = LayoutInflater.from(this);
LayoutInflater inflater = getLayoutInflater();
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
然后调用inflate方法将xml布局文件转成View
public View inflate (int resource, ViewGroup root, boolean attachToRoot)
在View类中,也有inflate方法
public static View inflate (Context context, int resource, ViewGroup root)
findViewById有两种形式
R.layout.xx是引用res/layout/xx.xml的布局文件(inflate 方法),R.id.xx是引用布局文件里面的组件,组件的id是xx(findViewById方法)。
所有的组件id都能用R.id.xx来查看,但是组件不在setContentView()里面的layout中就无法使用,Activity.findViewById()会出现空指针异常
a. activity中的findViewById(int id)
b. View 中的findViewById(int id)
不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化!而findViewById()是找具体xml下的具体
widget控件(如:Button,TextView等)。
Android View类中的生命周期是怎样的
android view有以下14个周期:
1、onFinishInflate() 当View中所有的子控件均被映射成xml后触发 。
2、onMeasure( int , int ) 确定所有子元素的大小 。
3、onLayout( boolean , int , int , int , int ) 当View分配所有的子元素的大小和位置时触发 。
4、onSizeChanged( int , int , int , int ) 当view的大小发生变化时触发 。
5、onDraw(Canvas) view渲染内容的细节。
6、onKeyDown( int , KeyEvent) 有按键按下后触发 。
7、onKeyUp( int , KeyEvent) 有按键按下后弹起时触发 。
8、onTrackballEvent(MotionEvent) 轨迹球事件 。
9、onTouchEvent(MotionEvent) 触屏事件 。
10、onFocusChanged( boolean , int , Rect) 当View获取或失去焦点时触发 。
11、onWindowFocusChanged( boolean ) 当窗口包含的view获取或失去焦点时触发 。
12、onAttachedToWindow() 当view被附着到一个窗口时触发 。
13、onDetachedFromWindow() 当view离开附着的窗口时触发,Android123提示该方法和 onAttachedToWindow() 是相反的。
14、onWindowVisibilityChanged( int ) 当窗口中包含的可见的view发生变化时触发。
Tudcloud是一家新开的主机商,提供VPS和独立服务器租用,数据中心在中国香港(VPS和独立服务器)和美国洛杉矶(独立服务器),商家VPS基于KVM架构,开设在香港机房,可以选择限制流量大带宽或者限制带宽不限流量套餐。目前提供8折优惠码,优惠后最低每月7.2美元起。虽然主机商网站为英文界面,但是支付方式仅支付宝和Stripe,可能是国人商家。下面列出部分VPS主机套餐配置信息。CPU:1cor...
zoecloud怎么样?zoecloud是一家国人商家,5月成立,暂时主要提供香港BGP KVM VPS,线路为AS41378,并有首发永久8折优惠:HKBGP20OFF。目前,解锁香港区 Netflix、Youtube Premium ,但不保证一直解锁,谢绝以不是原生 IP 理由退款。不保证中国大陆连接速度,建议移动中转使用,配合广州移动食用效果更佳。点击进入:zoecloud官方网站地址zo...
快云科技怎么样?快云科技是一家成立于2020年的新起国内主机商,资质齐全 持有IDC ICP ISP等正规商家。云服务器网(yuntue.com)小编之前已经介绍过很多快云科技的香港及美国云服务器了,这次再介绍一下新的优惠方案。目前,香港云沙田CN2云服务器低至29元/月起;美国超防弹性云/洛杉矶CUVIP低至33.6元/月起。快云科技的云主机架构采用KVM虚拟化技术,全盘SSD硬盘,RAID10...
onfinishinflate为你推荐
素数算法求100以内的素数的算法怎么用电脑发短信谁知道怎样能用电脑给手机发短信pat是什么格式怎么将自己做的PS图片保存为PAT格式?webservice框架什么是webservice,什么情况下使用,如何使用5e5e5e计算器里5.55556e-5是什么意思comexception5种常见的Exception!za是哪个国家的IE是哪个国家的缩写za是哪个国家的奥洛菲是哪个国家的特斯拉model3降价特斯拉model 3中国有补贴吗怎么查微信注册时间怎么知道微信上次登录时间
万网域名代理 wavecom kdata 163网 外国空间 shopex空间 光棍节日志 ssh帐号 tightvnc 大容量存储 促正网秒杀 40g硬盘 工作站服务器 nerds qq云端 ca187 512mb 空间登陆首页 海外空间 域名转入 更多