实现分页选项卡上的布局 [英] Implement Pagination on tab layout
本文介绍了实现分页选项卡上的布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道如何使用XML布局发展卡活动,也是我知道该怎么做分页Android作为被认为是在Android Market应用
但我没有得到如何合并该寻呼的概念与选项卡活动,以及如何创建能够从Java code tabactivity:
见code下面我用分页使用线性布局:
sample.java
公共类样品延伸活动{
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
//创建分页布局
PaginationLayout paginationLayout =新PaginationLayout(本);
paginationLayout.setLayoutParams(新的LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
//只为样本创建内容
TableLayout表=新TableLayout(本);
table.setLayoutParams(新的LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
table.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
的TableRow行=新的TableRow(本);
row.setLayoutParams(新的LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
table.addView(行);
的TableRow ROW2 =新的TableRow(本);
row2.setLayoutParams(新的LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
table.addView(ROW2);
的for(int i = 0; I< 50;我++){
Button按钮=新的按钮(这一点);
button.setText(按钮+ I);
如果(我%2 == 0){
row.addView(按钮);
} 其他 {
row2.addView(按钮);
}
}
//加入分页内容
paginationLayout.addView(表);
//设置分页布局
的setContentView(paginationLayout);
}
}
和$ C $下分页布局是:
公共类PaginationLayout扩展的LinearLayout {
私人诠释mPageActive = 0;
私人HorizontalScrollView mScroll;
私人的LinearLayout毫巴;
公共PaginationLayout(上下文的背景下){
超(上下文);
setOrientation(LinearLayout.VERTICAL);
//创建将控制手势,并将其应用在类
// 滚动
最后GestureDetector mGestureDetector =新GestureDetector(新MySimpleOnGestureListener());
mScroll =新HorizontalScrollView(上下文);
mScroll.setLayoutParams(新的LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
mScroll.setOnTouchListener(新View.OnTouchListener(){
公共布尔onTouch(视图V,MotionEvent事件){
如果(mGestureDetector.onTouchEvent(事件)){
返回true;
} 其他 {
返回false;
}
}
});
//创建previous和Next按钮
按钮BTN previous =新的按钮(的getContext());
BTN previous.setLayoutParams(新的LayoutParams(150,LayoutParams.FILL_PARENT));
BTN previous.setText(previous);
BTN previous.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
previous();
}
});
按钮btnMid =新的按钮(的getContext());
btnMid.setLayoutParams(新的LayoutParams(150,LayoutParams.FILL_PARENT));
btnMid.setText(中);
btnMid.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
下一个();
}
});
按钮btnNext =新的按钮(的getContext());
btnNext.setLayoutParams(新的LayoutParams(150,LayoutParams.FILL_PARENT));
btnNext.setText(下一步);
btnNext.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
下一个();
}
});
//酒吧,包括按钮
毫巴=新的LinearLayout(的getContext());
mBar.setLayoutParams(新的LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
mBar.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
mBar.setBackgroundColor(Color.GRAY);
mBar.addView(BTN previous);
mBar.addView(btnNext);
//mBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.android_1080p));
mBar.setVisibility(LinearLayout.INVISIBLE);
//添加栏和滚动超(的LinearLayout)
super.addView(毫巴);
super.addView(mScroll);
}
/ **
*所有的用户包括在滚动中加入
* /
@覆盖
公共无效addView(查看孩子){
mScroll.addView(子);
}
/ **
*控制是否最上面一栏应该出现与否
* /
@覆盖
保护无效onSizeChanged(INT为arg0,诠释ARG1,诠释ARG2,诠释ARG3){
super.onSizeChanged(为arg0,ARG1,ARG2,ARG3);
视图chield = mScroll.getChildAt(0);
如果(chield!= NULL){
如果(chield.getMeasuredWidth()>的getWidth()){
mBar.setVisibility(LinearLayout.VISIBLE);
} 其他 {
mBar.setVisibility(LinearLayout.INVISIBLE);
}
}
}
/ **
*做的效果返回页面
* /
公共无效previous(){
mPageActive =(mPageActive大于0)? mPageActive - 1:0;
mScroll.smoothScrollTo(mPageActive * mScroll.getWidth(),0);
}
/ **
*做的效果转发页面
* /
下一个公共无效(){
INT页宽= mScroll.getWidth();
INT下一页=(mPageActive + 1)*页宽;
如果(下一页 - mScroll.getScrollX()&其中; =页宽){
mScroll.smoothScrollTo(下一页,0);
mPageActive ++;
} 其他 {
mScroll.smoothScrollTo(mScroll.getScrollX(),0);
}
}
/ **
*控制的姿态,向前或向后一页私有类。
* /
私有类MySimpleOnGestureListener扩展SimpleOnGestureListener {
@覆盖
公共布尔onFling(MotionEvent E1,E2 MotionEvent,浮velocityX,浮velocityY){
如果(E1 = NULL和放大器;!&安培;!E2 = NULL){
如果(e1.getX() - e2.getX()大于0){
// 前锋...
下一个();
返回true;
}否则如果(e2.getX() - e1.getX()大于0){
// 背部...
previous();
返回true;
}
}
返回false;
}
}
}
解决方案
我发现的这个的是,也许有帮助;)
I know how to develop the tab activity using xml layout and also i know how to do paging in android as is seen in android market application
but i am not getting how to merge this paging concept with tab activity and how to create tabactivity from the java code :
see the code below i use for paging using linear layout :
sample.java
public class Sample extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// creates the Pagination Layout
PaginationLayout paginationLayout = new PaginationLayout(this);
paginationLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
// creates content only for sample
TableLayout table = new TableLayout(this);
table.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
table.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
TableRow row = new TableRow(this);
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
table.addView(row);
TableRow row2 = new TableRow(this);
row2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
table.addView(row2);
for(int i = 0; i< 50;i++){
Button button = new Button(this);
button.setText("Button " + i);
if (i%2==0) {
row.addView(button);
} else {
row2.addView(button);
}
}
// add the content in pagination
paginationLayout.addView(table);
// set pagination layout
setContentView(paginationLayout);
}
}
and the code for pagination layout is :
public class PaginationLayout extends LinearLayout {
private int mPageActive = 0;
private HorizontalScrollView mScroll;
private LinearLayout mBar;
public PaginationLayout(Context context) {
super(context);
setOrientation(LinearLayout.VERTICAL);
// creates the class that will control the gestures and apply it in the
// scroll
final GestureDetector mGestureDetector = new GestureDetector(new MySimpleOnGestureListener());
mScroll = new HorizontalScrollView(context);
mScroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
mScroll.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (mGestureDetector.onTouchEvent(event)) {
return true;
} else {
return false;
}
}
});
// creates Previous and Next buttons
Button btnPrevious = new Button(getContext());
btnPrevious.setLayoutParams(new LayoutParams(150, LayoutParams.FILL_PARENT));
btnPrevious.setText("Previous");
btnPrevious.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
previous();
}
});
Button btnMid = new Button(getContext());
btnMid.setLayoutParams(new LayoutParams(150, LayoutParams.FILL_PARENT));
btnMid.setText("Mid");
btnMid.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
next();
}
});
Button btnNext = new Button(getContext());
btnNext.setLayoutParams(new LayoutParams(150, LayoutParams.FILL_PARENT));
btnNext.setText("Next");
btnNext.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
next();
}
});
// bar that include the buttons
mBar = new LinearLayout(getContext());
mBar.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
mBar.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
mBar.setBackgroundColor(Color.GRAY);
mBar.addView(btnPrevious);
mBar.addView(btnNext);
//mBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.android_1080p));
mBar.setVisibility(LinearLayout.INVISIBLE);
// add bar and scroll in the super (LinearLayout)
super.addView(mBar);
super.addView(mScroll);
}
/**
* All that the user include is added in the scroll
*/
@Override
public void addView(View child) {
mScroll.addView(child);
}
/**
* Controls if the top bar should appear or not
*/
@Override
protected void onSizeChanged(int arg0, int arg1, int arg2, int arg3) {
super.onSizeChanged(arg0, arg1, arg2, arg3);
View chield = mScroll.getChildAt(0);
if (chield != null) {
if (chield.getMeasuredWidth() > getWidth()) {
mBar.setVisibility(LinearLayout.VISIBLE);
} else {
mBar.setVisibility(LinearLayout.INVISIBLE);
}
}
}
/**
* does the effect "back a page"
*/
public void previous() {
mPageActive = (mPageActive > 0) ? mPageActive - 1 : 0;
mScroll.smoothScrollTo(mPageActive * mScroll.getWidth(), 0);
}
/**
* does the effect "forward a page"
*/
public void next() {
int pageWidth = mScroll.getWidth();
int nextPage = (mPageActive + 1) * pageWidth;
if (nextPage - mScroll.getScrollX() <= pageWidth) {
mScroll.smoothScrollTo(nextPage, 0);
mPageActive++;
} else {
mScroll.smoothScrollTo(mScroll.getScrollX(), 0);
}
}
/**
* Private class that controls the gestures, forward or back a page.
*/
private class MySimpleOnGestureListener extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (e1 != null && e2 != null) {
if (e1.getX() - e2.getX() > 0) {
// forward...
next();
return true;
} else if (e2.getX() - e1.getX() > 0) {
// back...
previous();
return true;
}
}
return false;
}
}
}
解决方案
I've found this it's maybe helpful ;)
这篇关于实现分页选项卡上的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文