模仿renren的左右划动菜单栏,主要通过HorizontalScrollView来实现横向划动布局。通过判HorizontalScrollView对象的scrollX来控制左右两边的图片显隐。显示效果如图:
具体Java代码如下:
/**
*
* @author Shaodong Wu
* 模仿renren的左右划动菜单栏
*/
public class JustTestActivity extends Activity {
private static final String TAG="JustTestActivity";
/** Called when the activity is first created. */
HorizontalScrollView scrollBar;
RadioGroup radioGroup;
int widthX;
ImageView arrowl;
ImageView arrowr;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
scrollBar=(HorizontalScrollView)this.findViewById(R.id.scroll_bar);
radioGroup=(RadioGroup)this.findViewById(R.id.radioGroup);
arrowl=(ImageView)this.findViewById(R.id.arrowl);
arrowr=(ImageView)this.findViewById(R.id.arrowr);
scrollBar.fling(1000);
//通过判断目标的scrollX来控制左右两边的图片显隐
scrollBar.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
Log.i(TAG, "scrollX ---->"+scrollBar.getScrollX());
Log.i(TAG, "windth ---->"+scrollBar.getWidth());
LinearLayout linear=(LinearLayout)scrollBar.getChildAt(0);
int radioWidth= linear.getChildAt(0).getWidth();
Log.i(TAG, "radio width--->"+radioWidth);
if(scrollBar.getScrollX()<6){
arrowl.setVisibility(View.GONE);
}else if(scrollBar.getScrollX()+scrollBar.getWidth()>radioWidth-6){
arrowr.setVisibility(View.GONE);
}else{
arrowr.setVisibility(View.VISIBLE);
arrowl.setVisibility(View.VISIBLE);
}
return false;
}
});
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
}
Xml布局代码如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="@+id/frameLayout1" android:scrollbars="none" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:layout_width="fill_parent">
<HorizontalScrollView android:layout_width="fill_parent" android:scrollbars="none" android:fillViewport="true"
android:layout_height="wrap_content" android:fadingEdge="none"
android:id="@+id/scroll_bar">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/radioGroup"
android:orientation="horizontal" android:gravity="center_vertical">
<RadioButton android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="游戏1" style="@style/main_tab_bottom"/>
<RadioButton android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="游戏2" style="@style/main_tab_bottom"/>
<RadioButton android:id="@+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="游戏3" style="@style/main_tab_bottom"/>
<RadioButton android:id="@+id/radio4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="游戏4" style="@style/main_tab_bottom"/>
<RadioButton android:id="@+id/radio5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="游戏5" style="@style/main_tab_bottom"/>
<RadioButton android:id="@+id/radio6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="游戏6" style="@style/main_tab_bottom"/>
<RadioButton android:id="@+id/radio7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="游戏7" style="@style/main_tab_bottom"/>
<RadioButton android:id="@+id/radio7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="游戏8" style="@style/main_tab_bottom"/>
<RadioButton android:id="@+id/radio9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="游戏9" style="@style/main_tab_bottom"/>
</RadioGroup>
</LinearLayout>
</HorizontalScrollView>
<ImageView android:id="@+id/arrowl" android:layout_height="wrap_content" android:visibility="gone" android:layout_width="wrap_content" android:src="@drawable/arrowl" android:layout_gravity="left|center_vertical"></ImageView>
<ImageView android:layout_height="wrap_content" android:layout_gravity="right|center_vertical" android:visibility="gone" android:layout_width="wrap_content" android:src="@drawable/arrowr" android:id="@+id/arrowr"/>
</FrameLayout>
完整项目见附件。