※效果
※使用方法
package com.cd.slidetest;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ScrollView;
public class MainActivity extends Activity implements OnScrollChangedListener{
private GestureDetector mGestureDetector = null;
private HScrollView mItemRoom = null;
private ScrollView mVertical = null;
private HScrollView mTimeItem = null;
private LinearLayout mContain = null;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGestureDetector = new GestureDetector(mGestureListener);
makeItems();
setContentView(mContain);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void makeItems(){
mItemRoom = new HScrollView(this, mGestureDetector);
mItemRoom.setListener(this);
LinearLayout VLayout = new LinearLayout(this);
VLayout.setOrientation(LinearLayout.VERTICAL);
for(int i = 0;i < 20;i++){
LinearLayout HLayout = new LinearLayout(this);
for(int j = 0;j < 10;j++){
TextItem item = new TextItem(this);
item.setText(i*10+j + "vh");
item.setBackgroundColor(Color.LTGRAY);
LinearLayout.LayoutParams params = new LayoutParams(100,70);
HLayout.addView(item, params);
}
VLayout.addView(HLayout);
}
mItemRoom.addView(VLayout);
mVertical = new ScrollView(this);
LinearLayout hlayout = new LinearLayout(this);
LinearLayout vlayout = new LinearLayout(this);
vlayout.setOrientation(LinearLayout.VERTICAL);
for(int i = 0;i < 20;i++){
TextItem item = new TextItem(this);
item.setBackgroundColor(Color.BLUE);
item.setText(i + "v");
LinearLayout.LayoutParams params = new LayoutParams(100,70);
vlayout.addView(item, params);
}
hlayout.addView(vlayout);
hlayout.addView(mItemRoom);
mVertical.addView(hlayout);
LinearLayout tophlayout = new LinearLayout(this);
mTimeItem = new HScrollView(this, mGestureDetector);
mTimeItem.setListener(this);
LinearLayout layout = new LinearLayout(this);
for(int i = 0;i < 10;i++){
TextItem item = new TextItem(this);
item.setText(i + "h");
item.setBackgroundColor(Color.GREEN);
LinearLayout.LayoutParams params = new LayoutParams(100,70);
layout.addView(item,params);
}
mTimeItem.addView(layout);
TextItem item = new TextItem(this);
item.setBackgroundColor(Color.RED);
LinearLayout.LayoutParams params = new LayoutParams(100,70);
tophlayout.addView(item,params);
tophlayout.addView(mTimeItem);
mContain = new LinearLayout(this);
mContain.setOrientation(LinearLayout.VERTICAL);
mContain.addView(tophlayout);
mContain.addView(mVertical);
}
private SimpleOnGestureListener mGestureListener = new SimpleOnGestureListener(){
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
if(Math.abs(distanceX) > Math.abs(distanceY)) {
return true;
}
return false;
}
};
@Override
public void onScrollChanged(HScrollView scroll, int x, int y, int oldx,
int oldy) {
if(scroll == mItemRoom){
mTimeItem.scrollTo(x, y);
}else if(scroll == mTimeItem){
mItemRoom.scrollTo(x, y);
}
}
}
※下载地址
http://download.csdn.net/detail/u010785585/8062787