Android 模仿renren的左右划动菜单栏

本文介绍了一种模仿人人网左右滑动菜单栏的实现方法,利用HorizontalScrollView完成横向滑动布局,并通过监听scrollX控制两侧箭头图标显示与隐藏。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    模仿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>

 

 

 

完整项目见附件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值