Android中实现循环滚动
16lz
2022-05-14
实现的效果图如下:
可循环滚动,当触摸结束时,会自动滚动到被选中的位置,同时显示该选中项。
由于Gallery已经过时,所以决定使用HorizontalScrollView控件。
下面是布局文件activity_main.xml:
<RelativeLayout 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" > <HorizontalScrollView android:id="@+id/horizontalScrollView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:scrollbars="none" > <LinearLayout android:id="@+id/linearLayout" android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="horizontal" > </LinearLayout> </HorizontalScrollView> <View android:layout_width="1dp" android:layout_height="match_parent" android:layout_centerHorizontal="true" android:background="#000000" /> </RelativeLayout>
以下为相对应的Activity,MainActivity.java
package com.example.hscrollcircle; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.Gravity; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.ViewTreeObserver; import android.widget.HorizontalScrollView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private HorizontalScrollView horizontalScrollView; private LinearLayout linearLayout; // 滚动条的宽度 private int hsv_width; // 总共有多少个view private int child_count; // 每一个view的宽度 private int child_width; // 预计显示在屏幕上的view的个数 private int child_show_count; // 一开始居中选中的view private int child_start; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } /** * 初始化控件及变量 */ private void init() { horizontalScrollView = (HorizontalScrollView) findViewById(R.id.horizontalScrollView); linearLayout = (LinearLayout) findViewById(R.id.linearLayout); child_count = 12; child_show_count = 9; child_start = 6; } /** * 给滚动控件添加view,只有重复两个列表才能实现循环滚动 */ private void initData() { for (int i = 0; i < child_count; i++) { TextView textView = new TextView(this); textView.setLayoutParams(new ViewGroup.LayoutParams(child_width, ViewGroup.LayoutParams.MATCH_PARENT)); textView.setText("" + (i + 1)); textView.setGravity(Gravity.CENTER); linearLayout.addView(textView); } for (int i = 0; i < child_count; i++) { TextView textView = new TextView(this); textView.setLayoutParams(new ViewGroup.LayoutParams(child_width, ViewGroup.LayoutParams.MATCH_PARENT)); textView.setText("" + (i + 1)); textView.setGravity(Gravity.CENTER); linearLayout.addView(textView); } } /** * 实现滚动的循环处理,及停止触摸时的处理 */ private void initHsvTouch() { horizontalScrollView.setOnTouchListener(new View.OnTouchListener() { private int pre_item; @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub boolean flag = false; int x = horizontalScrollView.getScrollX(); int current_item = (x + hsv_width / 2) / child_width + 1; switch (event.getAction()) { case MotionEvent.ACTION_MOVE: flag = false; if (x <= child_width) { horizontalScrollView.scrollBy( child_width * child_count, 0); current_item += child_count; } else if (x >= (child_width * child_count * 2 - hsv_width - child_width)) { horizontalScrollView.scrollBy(-child_width * child_count, 0); current_item -= child_count; } break; case MotionEvent.ACTION_UP: flag = true; horizontalScrollView.smoothScrollTo(child_width * current_item - child_width / 2 - hsv_width / 2, horizontalScrollView.getScrollY()); Toast.makeText(MainActivity.this, "" + (current_item % child_count), Toast.LENGTH_LONG).show(); break; } if (pre_item == 0) { isChecked(current_item, true); } else if (pre_item != current_item) { isChecked(pre_item, false); isChecked(current_item, true); } pre_item = current_item; return flag; } }); } /** * 设置指定位置的状态 * * @param item * @param isChecked */ private void isChecked(int item, boolean isChecked) { TextView textView = (TextView) linearLayout.getChildAt(item - 1); if (isChecked) { textView.setTextColor(Color.RED); } else { textView.setTextColor(Color.BLACK); } } /** * 刚开始进入界面时的初始选中项的处理 */ private void initStart() { final ViewTreeObserver observer = horizontalScrollView .getViewTreeObserver(); observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { @Override public boolean onPreDraw() { // TODO Auto-generated method stub observer.removeOnPreDrawListener(this); int child_start_item = child_start; if ((child_start * child_width - child_width / 2 - hsv_width / 2) <= child_width) { child_start_item += child_count; } horizontalScrollView.scrollTo(child_width * child_start_item - child_width / 2 - hsv_width / 2, horizontalScrollView.getScrollY()); isChecked(child_start_item, true); return false; } }); } /** * 只有到了这个方法才能获取控件的尺寸 */ @Override public void onWindowFocusChanged(boolean hasFocus) { // TODO Auto-generated method stub super.onWindowFocusChanged(hasFocus); hsv_width = horizontalScrollView.getWidth(); int child_width_temp = hsv_width / child_show_count; if (child_width_temp % 2 != 0) { child_width_temp++; } child_width = child_width_temp; initData(); initHsvTouch(); initStart(); } }
更多相关文章
- android GridView选择照片的完整实现
- 3.3详解四种基本布局
- Android控件开发之一----TextView
- Android通过selector改变界面状态
- android EditText控件如何禁止输入内容
- Android(安卓)自定义RadioButton的样式
- 一张图看遍LinearLayout的所有特有属性
- Android(安卓)Market的 Loading效果
- 【Android(安卓)初学】2、View初步