Android 实例讲解HorizontalScrollView实现左右滑动

本博文主要讲解怎么使用HorizontalScrollView实现左右滑动的效果。
HorizontalScrollView实际上是一个FrameLayout ,一般通过只放置一个LinearLayout子控件。如果要使其添加其他的控件,就使用LinearLayout子控件来添加其他的控件,最后达到丰富其内容的效果。其中,LinearLayout设置的orientation布局为Horizontal.HorizontalScrollView不可以和ListView同时用,因为ListView有自己的滚动条设置。类似TextView也一样。
下面通过一个实际的实例来讲解HorizontalScrollView:

1.效果:


2.实现代码

需求分析:主要实现一个父LinearLayout中包含代码创建的10个子LinearLayout,而每个子LinearLayout添加一个ImageView和TextView,超出的部分实现左右滑动

activity_main.xml

<span style="font-family:Microsoft YaHei;font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <HorizontalScrollView
        android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#00FF00"
        android:scrollbarAlwaysDrawHorizontalTrack="false" >

        <LinearLayout
            android:id="@+id/linear"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
        </LinearLayout>
    </HorizontalScrollView>

</LinearLayout></span>

MainActivity.java

package com.example.horizontalscrollviewdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private HorizontalScrollView scrollView;
	private LinearLayout linear;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		scrollView = (HorizontalScrollView) this.findViewById(R.id.scroll_view);
		linear = (LinearLayout) this.findViewById(R.id.linear);
		createChildLinearLayout();
	}

	private void createChildLinearLayout() {
		for (int i = 0; i < 10; i++) {
			LinearLayout.LayoutParams linearLp = new LinearLayout.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			LinearLayout myLinear = new LinearLayout(this);
			linearLp.setMargins(5, 0, 5, 20);
			myLinear.setOrientation(LinearLayout.VERTICAL);
			myLinear.setTag(i);
			linear.addView(myLinear, linearLp);

			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			ImageView imageView = new ImageView(this);
			imageView.setBackgroundResource(R.drawable.img);
			myLinear.addView(imageView, lp);

			LinearLayout.LayoutParams textViewLp = new LinearLayout.LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
			TextView textView = new TextView(this);
			textView.setText(i + "");
			textView.setGravity(Gravity.CENTER);
			myLinear.addView(textView, textViewLp);

			myLinear.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Toast.makeText(MainActivity.this, v.getTag().toString(),
							Toast.LENGTH_SHORT).show();
				}
			});
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}


以上为本博客所讲内容。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android开发中,我们可以使用HorizontalScrollView实现图片左右滚动的功能。HorizontalScrollView是一个可以水平滚动的视图容器,可以包含任意数量的子视图,在用户滑动时自动滚动。 首先,在布局文件中使用HorizontalScrollView作为容器,设置其宽度和高度适配图片的尺寸,例如: ``` <HorizontalScrollView android:layout_width="match_parent" android:layout_height="200dp"> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent"> <!--在这里添加需要滚动的图片--> </LinearLayout> </HorizontalScrollView> ``` 然后,在代码中动态地添加图片到LinearLayout中: ```java LinearLayout linearLayout = findViewById(R.id.linear_layout); for (int i = 0; i < imageNameList.size(); i++) { ImageView imageView = new ImageView(this); imageView.setImageResource(imageNameList.get(i)); linearLayout.addView(imageView); } ``` 其中,`imageNameList`是一个包含图片资源ID的列表,可以根据自己的需求进行设置。 滚动效果的实现是通过监听用户的手势来实现的,可以使用`OnTouchListener`接口来监听HorizontalScrollView滑动事件: ```java horizontalScrollView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: // 手指按下时的操作 break; case MotionEvent.ACTION_MOVE: // 手指滑动时的操作 break; case MotionEvent.ACTION_UP: // 手指抬起时的操作 break; } return false; } }); ``` 在滑动事件中,可以通过调用HorizontalScrollView的`scrollTo()`方法来实现滚动的效果: ```java int scrollX = horizontalScrollView.getScrollX(); // 获取滚动的距离 int scrollTo = scrollX + distance; // 根据手指滑动的距离计算要滚动到的位置 horizontalScrollView.scrollTo(scrollTo, 0); // 进行滚动 ``` 其中,`distance`是根据手指滑动的速度和方向计算出来的滑动距离。 这样,就可以通过使用HorizontalScrollView实现图片的左右滚动了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值