package grimbo.android.demo.slidingmenu;
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.HorizontalScrollView;
/**
* A HorizontalScrollView (HSV) implementation that disallows touch events (so no scrolling can be done by the user).
*
* This HSV MUST contain a single ViewGroup as its only child, and this ViewGroup will be used to display the children Views
* passed in to the initViews() method.
*/
public class MyHorizontalScrollView extends HorizontalScrollView {
public MyHorizontalScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
public MyHorizontalScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public MyHorizontalScrollView(Context context) {
super(context);
init(context);
}
void init(Context context) {
// remove the fading as the HSV looks better without it
setHorizontalFadingEdgeEnabled(false);
setVerticalFadingEdgeEnabled(false);
}
/**
* @param children
* The child Views to add to parent.
* @param scrollToViewIdx
* The index of the View to scroll to after initialisation.
* @param sizeCallback
* A SizeCallback to interact with the HSV.
*/
public void initViews(View[] children, int scrollToViewIdx, SizeCallback sizeCallback) {
// A ViewGroup MUST be the only child of the HSV
ViewGroup parent = (ViewGroup) getChildAt(0);
// Add all the children, but add them invisible so that the layouts are calculated, but you can't see the Views
for (int i = 0; i < children.length; i++) {
children[i].setVisibility(View.INVISIBLE);
parent.addView(children[i]);
}
// Add a layout listener to this HSV
// This listener is responsible for arranging the child views.
OnGlobalLayoutListener listener = new MyOnGlobalLayoutListener(parent, children, scrollToViewIdx, sizeCallback);
getViewTreeObserver().addOnGlobalLayoutListener(listener);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
// Do not allow touch events.
return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// Do not allow touch events.
return false;
}
/**
* An OnGlobalLayoutListener impl that passes on the call to onGlobalLayout to a SizeCallback, before removing all the Views
* in the HSV and adding them again with calculated widths and heights.
*/
class MyOnGlobalLayoutListener implements OnGlobalLayoutListener {
ViewGroup parent;
View[] children;
int scrollToViewIdx;
int scrollToViewPos = 0;
SizeCallback sizeCallback;
/**
* @param parent
* The parent to which the child Views should be added.
* @param children
* The child Views to add to parent.
* @param scrollToViewIdx
* The index of the View to scroll to after initialisation.
* @param sizeCallback
* A SizeCallback to interact with the HSV.
*/
public MyOnGlobalLayoutListener(ViewGroup parent, View[] children, int scrollToViewIdx, SizeCallback sizeCallback) {
this.parent = parent;
this.children = children;
this.scrollToViewIdx = scrollToViewIdx;
this.sizeCallback = sizeCallback;
}
@Override
public void onGlobalLayout() {
// System.out.println("onGlobalLayout");
final HorizontalScrollView me = MyHorizontalScrollView.this;
// The listener will remove itself as a layout listener to the HSV
me.getViewTreeObserver().removeGlobalOnLayoutListener(this);
// Allow the SizeCallback to 'see' the Views before we remove them and re-add them.
// This lets the SizeCallback prepare View sizes, ahead of calls to SizeCallback.getViewSize().
sizeCallback.onGlobalLayout();
parent.removeViewsInLayout(0, children.length);
final int w = me.getMeasuredWidth();
final int h = me.getMeasuredHeight();
// System.out.println("w=" + w + ", h=" + h);
// Add each view in turn, and apply the width and height returned by the SizeCallback.
int[] dims = new int[2];
scrollToViewPos = 0;
for (int i = 0; i < children.length; i++) {
sizeCallback.getViewSize(i, w, h, dims);
// System.out.println("addView w=" + dims[0] + ", h=" + dims[1]);
children[i].setVisibility(View.VISIBLE);
parent.addView(children[i], dims[0], dims[1]);
if (i < scrollToViewIdx) {
scrollToViewPos += dims[0];
}
}
// For some reason we need to post this action, rather than call immediately.
// If we try immediately, it will not scroll.
new Handler().post(new Runnable() {
@Override
public void run() {
me.scrollBy(scrollToViewPos, 0);
}
});
}
}
/**
* Callback interface to interact with the HSV.
*/
public interface SizeCallback {
/**
* Used to allow clients to measure Views before re-adding them.
*/
public void onGlobalLayout();
/**
* Used by clients to specify the View dimensions.
*
* @param idx
* Index of the View.
* @param w
* Width of the parent View.
* @param h
* Height of the parent View.
* @param dims
* dims[0] should be set to View width. dims[1] should be set to View height.
*/
public void getViewSize(int idx, int w, int h, int[] dims);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Android程序研发源码Android 仿facebook布局效果.zip
共56个文件
xml:19个
class:17个
png:8个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 132 浏览量
2023-04-30
16:55:52
上传
评论
收藏 265KB ZIP 举报
温馨提示
Android程序研发源码Android 仿facebook布局效果.zip
资源推荐
资源详情
资源评论
收起资源包目录
Android程序研发源码Android 仿facebook布局效果.zip (56个子文件)
Android程序研发源码Android 仿facebook布局效果
SlidingMenuDemo
project.properties 361B
.classpath 356B
assets
src
grimbo
android
demo
slidingmenu
HorzScrollWithImageMenu.java 2KB
HorzScrollWithListMenu.java 5KB
ViewUtils.java 2KB
MyHorizontalScrollView.java 6KB
res
anim
push_right_out_80.xml 916B
push_left_in_80.xml 916B
push_left_in.xml 917B
push_right_out.xml 917B
push_left_out.xml 918B
push_right_in.xml 918B
drawable-mdpi
ic_launcher.png 3KB
drawable-ldpi
ic_launcher.png 2KB
values
strings.xml 119B
layout
horz_scroll_with_list_menu.xml 710B
push_left_push_right_animation.xml 4KB
horz_scroll_menu.xml 653B
list_item.xml 243B
horz_scroll_app.xml 1KB
test_slide_activity.xml 1KB
slide_animation_then_call_layout.xml 2KB
dummy_menu.xml 2KB
horz_scroll_with_image_menu.xml 1KB
activity_menu.xml 1KB
animation_stacked_frames.xml 1KB
drawable
fb_menu.png 35KB
drawable-hdpi
ic_launcher.png 4KB
bin
classes
grimbo
android
demo
slidingmenu
HorzScrollWithImageMenu.class 3KB
HorzScrollWithListMenu$ClickListenerForScrolling.class 1KB
R$string.class 437B
R$layout.class 890B
ViewUtils.class 3KB
R$id.class 878B
HorzScrollWithListMenu$SizeCallbackForMenu.class 1KB
R$attr.class 379B
MyHorizontalScrollView.class 3KB
MyHorizontalScrollView$SizeCallback.class 340B
R$drawable.class 477B
HorzScrollWithListMenu.class 4KB
MyHorizontalScrollView$MyOnGlobalLayoutListener$1.class 1KB
R.class 681B
R$anim.class 627B
ViewUtils$1.class 2KB
MyHorizontalScrollView$MyOnGlobalLayoutListener.class 3KB
resources.ap_ 55KB
res
drawable-mdpi
ic_launcher.png 2KB
drawable-ldpi
ic_launcher.png 2KB
drawable
fb_menu.png 34KB
drawable-hdpi
ic_launcher.png 4KB
SlidingMenuDemo.apk 65KB
classes.dex 13KB
proguard.cfg 1KB
.project 818B
AndroidManifest.xml 710B
gen
grimbo
android
demo
slidingmenu
R.java 3KB
共 56 条
- 1
资源评论
HappyGirl快乐女孩
- 粉丝: 1w+
- 资源: 4156
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功