package com.oppo.examples; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.util.DisplayMetrics; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.TextView; import com.skateboard.viewgroup.MyViewPager; import com.skateboard.viewgroup.SbRelativeLayout; import com.skateboard.viewgroup.SbRelativeLayout.TouchListener; /*** * * 模仿猿题库的情景题UI * 猿题库的UI之一。双层view.重写RelativeLayout对截屏事件的处理,用子控件去滑动父控件视图。重写ViewPager事件的传递 * 。动态计算布局大小。要的可以找我。 * * 卓佳群(Droidjia)221803017 * * QQ:1083573260 * 欢迎您加入该群 * * @author Lyy * */ public class DraftTest extends Activity { int screenWidth; int screenHeight; Button b; private ListView listView; private MyAdapter myAdapter; private List<String> data; SbRelativeLayout liner; private LinearLayout lastViewLiner; int x, y; int defaultHeight; private MyViewPager viewPager; private MyViewpagerAdapter myViewpagerAdapter; View view; MyView myView; List<View> viewData; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); DisplayMetrics dm = getResources().getDisplayMetrics(); screenWidth = dm.widthPixels; screenHeight = dm.heightPixels; listView = (ListView) findViewById(R.id.listView); dataInit(); lastViewLiner = (LinearLayout) findViewById(R.id.lastViewLiner); liner = (SbRelativeLayout) findViewById(R.id.liner); // 初始化上层view在屏幕中的位置 RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( screenWidth, RelativeLayout.LayoutParams.FILL_PARENT); lp.topMargin = 300; liner.setLayoutParams(lp); // 异步设置低层view的大小 new Handler().postDelayed(new Runnable() { @Override public void run() { // TODO Auto-generated method stub int[] location = new int[2]; liner.getLocationOnScreen(location); x = location[0]; y = location[1]; defaultHeight = y - 40; RelativeLayout.LayoutParams PP = new RelativeLayout.LayoutParams( screenWidth, defaultHeight); lastViewLiner.setLayoutParams(PP); } }, 500); // 监听动态改变视图信息 liner.setTouchListener(new TouchListener() { @Override public void backTouchState(int left, int top, int right, int bottom) { // TODO Auto-generated method stub RelativeLayout.LayoutParams PP = new RelativeLayout.LayoutParams( screenWidth, top); lastViewLiner.setLayoutParams(PP); liner = (SbRelativeLayout) findViewById(R.id.liner); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( screenWidth, RelativeLayout.LayoutParams.FILL_PARENT); lp.topMargin = top; liner.setLayoutParams(lp); } }); viewPager = (MyViewPager) findViewById(R.id.viewPager); myViewpagerAdapter = new MyViewpagerAdapter(); viewData = new ArrayList<View>(); viewPager.setOffscreenPageLimit(0); int img[] = { R.drawable.img0, R.drawable.img1 }; String[] content = { "我爱我家,我爱我媳妇。哈哈哈。。。。", "我们都是一个家名字叫中国,我爱我老婆" }; for (int i = 0; i < 2; i++) { view = getLayoutInflater().inflate(R.layout.view, null); myView = new MyView(view); myView.setData(content[i], img[i]); viewData.add(view); } myViewpagerAdapter.setViewadata(viewData); viewPager.setAdapter(myViewpagerAdapter); } public void dataInit() { data = new ArrayList<String>(); for (int i = 0; i < 6; i++) { data.add(i + 1 + "道题"); } myAdapter = new MyAdapter(data); listView.setAdapter(myAdapter); } public class MyAdapter extends BaseAdapter { private List<String> data; public MyAdapter(List<String> data) { this.data = data; } @Override public int getCount() { // TODO Auto-generated method stub return data.size(); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return data.get(arg0); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub convertView = getLayoutInflater().inflate(R.layout.item, null); TextView tv = (TextView) convertView.findViewById(R.id.textview); tv.setText(data.get(position)); return convertView; } } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#fff" android:orientation="vertical" > <LinearLayout android:id="@+id/lastViewLiner" android:layout_width="fill_parent" android:layout_height="wrap_content" > <ListView android:id="@+id/listView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:cacheColorHint="#00000000" android:listSelector="#00000000" > </ListView> </LinearLayout> <com.skateboard.viewgroup.SbRelativeLayout android:id="@+id/liner" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/btn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="MD拖我呀" android:textColor="#fff" android:background="@drawable/xk_back_press_btn" /> <com.skateboard.viewgroup.MyViewPager android:id="@+id/viewPager" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_below="@+id/btn" /> </com.skateboard.viewgroup.SbRelativeLayout> </RelativeLayout>
//==================
package com.skateboard.viewgroup; import android.content.Context; import android.util.AttributeSet; import android.util.DisplayMetrics; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import com.oppo.examples.R; /** * * com.skateboard.viewgroup.SbRelativeLayout * * 在自定义View中,onLayout配合onMeasure方法一起使用, * 可以实现自定义View的复杂布局。自定义View首先调用onMeasure进行测量, * 然后调用onLayout方法,动态获取子View和子View的测量大小,然后进行layout布局 * * @author Lyy * * 时间2015,7月20日 * */ public class SbRelativeLayout extends RelativeLayout { private int screenWidth; private int screenHeight; // 滑板 private Button skateboard; // 滑屏锁 private boolean mScrolling; // 滑动监听 public TouchListener touchListener; public SbRelativeLayout(Context context) { super(context); // TODO Auto-generated constructor stub init(); } public SbRelativeLayout(Context context, AttributeSet attrs) { super(context, attrs); init(); // TODO Auto-generated constructor stub } public TouchListener getTouchListener() { return touchListener; } public void setTouchListener(TouchListener touchListener) { this.touchListener = touchListener; } public void init() { DisplayMetrics dm = getResources().getDisplayMetrics(); screenWidth = dm.widthPixels; screenHeight = dm.heightPixels; } /*** * * 视图的初始化 * */ public void viewInit() { skateboard = (Button) findViewById(R.id.btn); skateboard.setOnTouchListener(onTouchListener); this.setFocusable(true); // this.setClickable(true); } /** * 返回为ture ,被父控件拦截,触屏事件不会传递到子控件去处理,直接调用父容器的OnTouch方法 * * 返回为假,让子控件去处理滑事件,子控件OnTouch返回为true, 不会再传递给父容器,为false 父控件处理滑屏事件 * */ @Override public boolean onInterceptTouchEvent(MotionEvent ev) { // TODO Auto-generated method stub switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: // 拦截父控件滑屏 mScrolling = false; break; case MotionEvent.ACTION_MOVE: mScrolling = false; break; case MotionEvent.ACTION_UP: mScrolling = false; break; } return mScrolling; } /*** * * onLayout()就是设置每个子View在屏幕那个位置来显示出子View * */ @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { // TODO Auto-generated method stub super.onLayout(changed, l, t, r, b); viewInit(); } private OnTouchListener onTouchListener = new OnTouchListener() { int lastX, lastY; @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: lastX = (int) event.getRawX(); lastY = (int) event.getRawY(); break; /** * layout(l,t,r,b) l Left position, relative to parent t Top * position, relative to parent r Right position, relative to parent * b Bottom position, relative to parent * */ case MotionEvent.ACTION_MOVE: int dx = (int) event.getRawX() - lastX; int dy = (int) event.getRawY() - lastY; int left = SbRelativeLayout.this.getLeft() + dx; int top = SbRelativeLayout.this.getTop() + dy; int right = SbRelativeLayout.this.getRight() + dx; int bottom = SbRelativeLayout.this.getBottom() + dy; if (left < 0) { left = 0; right = left + SbRelativeLayout.this.getWidth(); } if (right > screenWidth) { right = screenWidth; left = right - SbRelativeLayout.this.getWidth(); } if (top < 0) { top = 0; bottom = top + SbRelativeLayout.this.getHeight(); } if (top > screenHeight - 80) { break; } else { touchListener.backTouchState(left, top, right, bottom); } // if (bottom > screenHeight) { // bottom = screenHeight; // top = bottom - Rlienr.this.getHeight(); // } // 改变视图的位置 SbRelativeLayout.this.layout(left, top, right, screenHeight); // // Log.e("------left---------" + left + "--top-" + top // + "----right--" + right + "---bottom-" + bottom, // "-------"); lastX = (int) event.getRawX(); lastY = (int) event.getRawY(); break; case MotionEvent.ACTION_UP: break; } return false; } }; /*** * Touch监听接口 * * @author Lyy * */ public interface TouchListener { public void backTouchState(int left, int top, int right, int bottom); } } //============================= package com.skateboard.viewgroup; import android.content.Context; import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.view.MotionEvent; /*** * * 我的viewpager * * @author Lyy * com.skateboard.viewgroup.MyViewPager */ public class MyViewPager extends ViewPager { // 控制滑动的boolean锁 public boolean scrollable = false; public MyViewPager(Context context) { super(context); } public MyViewPager(Context context, AttributeSet attrs) { super(context, attrs); } public void setScrollable(boolean enable) { this.scrollable = enable; } float mDownX, mDownY; @Override public boolean dispatchTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mDownX = event.getX(); mDownY = event.getY(); getParent().requestDisallowInterceptTouchEvent(true); break; case MotionEvent.ACTION_MOVE: if (Math.abs(event.getX() - mDownX) > Math.abs(event.getY() - mDownY)) { getParent().requestDisallowInterceptTouchEvent(true); } else { getParent().requestDisallowInterceptTouchEvent(false); } break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: getParent().requestDisallowInterceptTouchEvent(false); break; } return super.dispatchTouchEvent(event); } }
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦