知識(shí)
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X(jué)表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營(yíng)銷的便利,運(yùn)營(yíng)的高效,讓網(wǎng)站成為營(yíng)銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!
自定義橫向的ScrollView
發(fā)表時(shí)間:2021-1-10
發(fā)布人:葵宇科技
瀏覽次數(shù):49
介紹一個(gè)自定義橫向的ScrollView,可以監(jiān)聽(tīng)滑動(dòng)的狀態(tài),可以監(jiān)聽(tīng)滑動(dòng)到了最左側(cè),正在中間滑動(dòng)和滑到了最右邊。
思路:
1.根據(jù)ScrollView中第一層的子View,其實(shí)第一層也就一個(gè)View,這是ScrollView規(guī)定的,ScrollView包含的內(nèi)容,必須全部放到一個(gè)子View中。
2.根據(jù)最第一個(gè)子View的左側(cè)坐標(biāo),右側(cè)坐標(biāo),ScrollView的寬度和橫向滑動(dòng)的距離,來(lái)判斷滑動(dòng)的位置。
3.定義滑動(dòng)位置的監(jiān)聽(tīng)接口。
public class MyScrollView extends HorizontalScrollView { public MyScrollView(Context context) { super(context); } public MyScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public MyScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { View view = (View) getChildAt(getChildCount() - 1); // 如果為0,證明滑動(dòng)到最左邊 if (view.getLeft() - getScrollX() == 0) { onScrollListener.onLeft(); // 如果為0證明滑動(dòng)到最右邊 } else if ((view.getRight() - (getWidth() + getScrollX())) == 0) { onScrollListener.onRight(); // 說(shuō)明在中間 } else { onScrollListener.onScroll(); } super.onScrollChanged(l, t, oldl, oldt); } /** * 定義接口 * * @author admin */ public interface OnScrollListener1 { void onRight(); void onLeft(); void onScroll(); } private OnScrollListener1 onScrollListener; public void setOnScrollListener(OnScrollListener1 onScrollListener) { this.onScrollListener = onScrollListener; } }首先獲取ScrollView的第一個(gè)孩子,獲取到左側(cè)的坐標(biāo)view.getLeft(),獲取ScrollView在X方向滑動(dòng)的距離:getScrollX(),就是ScrollView和左側(cè)邊緣的距離。獲取第一個(gè)孩子最右側(cè)X的坐標(biāo):view.getRight().ScrllView的寬度:getWidth()
view.getLeft()和view.getLeft()的值是不變的。
view.getLeft():ScrollView的寬度,其實(shí)也是屏幕的寬度。
[img]http://img.blog.csdn.net/20150108111043953?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZm9yd2FyZHl6aw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
使用步驟:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#fef4dc" android:gravity="center" android:orientation="horizontal" > <com.example.view.MyScrollView android:id="@+id/myView" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="none" > <LinearLayout android:id="@+id/sortliner" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="horizontal" android:paddingLeft="10dp" android:paddingRight="10dp" > </LinearLayout> </com.example.view.MyScrollView> </LinearLayout> </LinearLayout>
MainActivity.java
public class MainActivity extends Activity { private final String TAG = MainActivity.class.getSimpleName(); final String[] arr = { "骨科", "婦科", "普外科", "神經(jīng)內(nèi)科", "神經(jīng)科", "神經(jīng)外科", "普外", "普內(nèi)", "呼吸科", "消化科", "兒科", "心內(nèi)" }; private MyScrollView myView;// 自定義的滑動(dòng)view private LinearLayout sortliner;// 滑動(dòng)條 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); Toast.makeText(getApplicationContext(), ""+screenWidth, 0).show(); } public void initView() { myView = (MyScrollView) findViewById(R.id.myView); sortliner = (LinearLayout) findViewById(R.id.sortliner); sortliner.removeAllViews(); myView.setOnScrollListener(new OnScrollListener1() { @Override public void onScroll() { Log.d(TAG, "在中間滑動(dòng)"); } @Override public void onRight() { Log.d(TAG, "滑動(dòng)到了最右邊"); Toast.makeText(getApplicationContext(), "滑到了最右邊", 0).show(); } @Override public void onLeft() { Log.d(TAG, "滑動(dòng)到了最左邊"); Toast.makeText(getApplicationContext(), "滑到了最左邊", 0).show(); } }); initScrollChildView(); } /** * 準(zhǔn)備向Scroller中添加View * * @param params */ private void initScrollChildView() { LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); for (int i = 0; i < arr.length; i++) {// 往二級(jí)分類中加載數(shù)據(jù) Button sort = new Button(MainActivity.this); sort.setText(arr[i]); sort.setTextSize(15); sort.setMinHeight(30); sort.setPadding(20, 5, 20, 5); // 把TextView添加到滑動(dòng)條內(nèi) sortliner.addView(sort, i, params); } } }
源碼下載地址:http://download.csdn.net/detail/forwardyzk/8339959
效果圖:
[img]http://img.blog.csdn.net/20150108113330250?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZm9yd2FyZHl6aw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
相關(guān)案例查看更多
相關(guān)閱讀
- 微信分銷
- 貴州小程序開(kāi)發(fā)
- 網(wǎng)站搭建
- 網(wǎng)站排名優(yōu)化
- 江蘇小程序開(kāi)發(fā)
- 軟件定制
- 云南建站公司
- 網(wǎng)站優(yōu)化公司
- 紅河小程序開(kāi)發(fā)
- 網(wǎng)站建設(shè)公司網(wǎng)站
- 云南網(wǎng)站建設(shè)電話
- 服務(wù)器
- 云南網(wǎng)站開(kāi)發(fā)哪家好
- flex
- 云南網(wǎng)站建設(shè)百度官方
- 云南網(wǎng)站建設(shè)專家
- 云南小程序開(kāi)發(fā)制作公司
- 網(wǎng)站建設(shè)報(bào)價(jià)
- 開(kāi)發(fā)制作小程序
- 昆明小程序定制開(kāi)發(fā)
- 小程序技術(shù)
- 網(wǎng)站建設(shè)選
- 關(guān)鍵詞快速排名
- 云南網(wǎng)站建設(shè)選
- 公眾號(hào)模板消息
- 花農(nóng)小程序
- 保山小程序開(kāi)發(fā)
- APP
- 昆明小程序開(kāi)發(fā)聯(lián)系方式
- 云南小程序哪家好