我想创建一个包含多个视图的布局。目前我有 3 个选项卡,其中一个我希望能够滚动包含两个文本视图和一个图像的布局的特定部分。我试过使用 ViewPager , HorizontalScrollView 但我无法滚动我的这部分布局。这是我的布局设计
[部分图片][滚动查看]
[表格1]
[表2]
[tab1][tab2][tab3]
我为手势检测运行的代码
public class RegionView extends HorizontalScrollView implements View.OnTouchListener{
private static final int SWIPE_MIN_DISTANCE = 5;
private static final int SWIPE_THRESHOLD_VELOCITY = 300;
private ArrayList<ArticleList> mItems= new ArrayList<ArticleList>();
private GestureDetector mGestureDetector;
private int mActiveFeature = 0;
public RegionView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setItems();
// TODO Auto-generated constructor stub
}
public RegionView(Context context) {
super(context);
internalWrapper = (HorizontalScrollView) findViewById(R.id.horizontalScrollView1);
internalLayout = (LinearLayout) findViewById(R.id.regionll1);
featureLayout = (LinearLayout) View.inflate(myContext,R.layout.regionview1,null);
setItems();
}
public void setItems(){
showToast("setItem Called");
internalLayout.setOnTouchListener(gestureListener);
for(int i = 0; i< mItems.size();i++){
internalLayout.addView(featureLayout);
}
setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (mGestureDetector.onTouchEvent(event)) {
return true;
}else if(event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL ){
int scrollX = getScrollX();
int featureWidth = v.getMeasuredWidth();
mActiveFeature = ((scrollX + (featureWidth/2))/featureWidth);
int scrollTo = mActiveFeature*featureWidth;
smoothScrollTo(scrollTo, 0);
return true;
}else{
return false;
}
}
});
mGestureDetector = new GestureDetector(new MyGestureDetector());
}
class MyGestureDetector extends SimpleOnGestureListener {
public boolean isRightToLeft = false;
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
int featureWidth = getMeasuredWidth();
mActiveFeature = (mActiveFeature < (mItems.size() - 1))? mActiveFeature + 1:mItems.size() -1;
smoothScrollTo(mActiveFeature*featureWidth, 0);
isRightToLeft=true;
return true;
}else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
int featureWidth = getMeasuredWidth();
mActiveFeature = (mActiveFeature > 0)? mActiveFeature - 1:0;
smoothScrollTo(mActiveFeature*featureWidth, 0);
showToast("Gesture on right swipe");
isRightToLeft=false;
return true;
}
} catch (Exception e) {
Log.e("Fling", "There was an error processing the Fling event:" + e.getMessage());
}
return false;
}
}
}
我的带有 tabhost 的 XML 文件,我已将代码分成两部分以显示我实现 Horizontalscroll 视图的位置
<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/main_bckground"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</RelativeLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/Orange"
>
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!--First Tab-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabyourcity"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:paddingLeft="0dip"
android:paddingRight="0dip" >
</LinearLayout>
<!—Second Tab-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabnepal"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:id = "@+id/nepalpageview"
android:orientation="vertical">
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/loadContainernepal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#414141"
android:orientation="horizontal" >
<TextView
android:id="@+id/loadingTextnepal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Data is Loading...."
android:textColor="#FFF"
/> <ProgressBar
android:id="@+id/progressBar2Nepal"
android:layout_width="25dip"
android:layout_height="25dip"
android:indeterminate="true"
android:visibility="visible"
android:layout_marginLeft="160dip"
style="@android:style/Widget.ProgressBar.Small"
/>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/relativelayouttop"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/tv_sunrisetime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_sunrise"
android:layout_alignRight="@+id/tv_sunrise"
android:layout_below="@+id/tv_sunrise"
android:gravity="center"
android:text="rise time"
android:textColor="#ffffff" />
<TextView
android:id="@+id/tv_sunrise"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/img_SUNRISE"
android:layout_marginLeft="20dp"
android:gravity="center"
android:text="SUNRISE"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/Orange"
android:textSize="18sp" >
</TextView>
<ImageView
android:id="@+id/img_SUNRISE"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignLeft="@+id/tv_sunrise"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/tv_sunrise"
android:paddingTop="5dp"
android:src="@drawable/sunriseicon" />
<TextView
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/img_sunset"
android:layout_alignRight="@+id/img_sunset"
android:layout_below="@+id/img_sunset"
android:gravity="center"
android:text="SUNSET"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/Orange"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_sunsettime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_sunrise"
android:layout_alignRight="@+id/tv_sunrise"
android:layout_below="@+id/TextView02"
android:gravity="center"
android:text="SET time"
android:textColor="#ffffff" />
<ImageView
android:id="@+id/img_sunset"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignLeft="@+id/tv_sunrisetime"
android:layout_alignRight="@+id/tv_sunrisetime"
android:layout_below="@+id/tv_sunrisetime"
android:src="@drawable/sunseticon" />
<RelativeLayout
android:layout_width="130dp"
android:layout_height="120dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:gravity="right" >
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<HorizontalScrollView
android:id="@+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
当我在此布局中提供所有图像和文本字段时,代码工作正常,但由于我需要检查值,然后在滚动时将文本和图像放在这里,所以我在实现这个时遇到了问题
<!-- <LinearLayout
android:id="@+id/scroll_linearlayout "
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/regionll1 "
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_regionname"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Hilly Region"
android:textColor="#ffffff"
android:textSize="18sp" />
<ImageView
android:id="@+id/img_region"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:src="@drawable/iconsresized5" />
<TextView
android:id="@+id/tv_regiontemp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#ffffff" />
</LinearLayout>
<LinearLayout
android:id="@+id/regionll2 "
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="@+id/regionll3 "
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout> -->
</HorizontalScrollView>
</android.support.v4.view.ViewPager>
</RelativeLayout>
</RelativeLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="20dp"
android:background="@color/Orange"
android:gravity="center" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="25dp"
android:layout_height="20dp"
android:src="@drawable/rainfall_stastastics" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text="Rainfall Statistics"
android:textColor="#ffffff"
android:textSize="16sp" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/tbl_background" >
<ImageView
android:id="@+id/imageView3"
android:layout_width="4dp"
android:layout_height="wrap_content"
android:src="@drawable/bullet" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total this month to date "
android:textColor="#000000" />
<TextView
android:id="@+id/tv_totalrainfall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000" />
</TableRow>
<TableRow>
</TableRow>
</TableLayout>
<TableLayout>
</TableLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</FrameLayout>
</RelativeLayout>
</TabHost>
我想要一个滚动视图,就像我们通过 viewpager 使用 iphone 获得的那样