Demo_对应条目置顶

这里写图片描述

MAINACTIVITY

package com.example.kadh.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {
    @Bind(R.id.bt_name)
    Button     btName;
    @Bind(R.id.bt_dir)
    Button     btDir;
    @Bind(R.id.bt_content)
    Button     btContent;
    @Bind(R.id.scrollView)
    ScrollView scrollView;
    private LinearLayout mView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        mView = (LinearLayout) scrollView.getChildAt(0);
        initEvent();
    }

    private void initEvent() {
        btName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                scrollView.scrollTo(0, mView.getChildAt(0).getTop());
            }
        });
        btDir.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                scrollView.scrollTo(0, mView.getChildAt(2).getTop());
            }
        });
        btContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                scrollView.scrollTo(0, mView.getChildAt(4).getTop());
            }
        });
    }
}

activity_main

<?xml version="1.0" encoding="utf-8"?>
<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="horizontal"
    >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <Button
            android:id="@+id/bt_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="书名"/>

        <Button
            android:id="@+id/bt_dir"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="目录"/>

        <Button
            android:id="@+id/bt_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="内容"/>
    </LinearLayout>

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tv_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#888888"
                android:text="书名"
                android:textSize="18dp"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="简介88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888简介88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888"
                android:textSize="18dp"/>

            <TextView
                android:id="@+id/tv_dir"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#888888"
                android:text="目录"
                android:textSize="18dp"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="目录777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777目录777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777"
                android:textSize="18dp"/>

            <TextView
                android:id="@+id/tv_content"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#888888"
                android:text="内容"
                android:textSize="18dp"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="标题111111111111111111111111111111111111111111111111111111111111111111111111111标题111111111111111111111111111111111111111111111111111111111111111111111111111标题111111111111111111111111111111111111111111111111111111111111111111111111111标题111111111111111111111111111111111111111111111111111111111111111111111111111标题111111111111111111111111111111111111111111111111111111111111111111111111111标题111111111111111111111111111111111111111111111111111111111111111111111111111标题111111111111111111111111111111111111111111111111111111111111111111111111111"
                android:textSize="18dp"/>


        </LinearLayout>

    </ScrollView>

</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值