在水平滚动视图中单击时如何滚动到芯片?

时间:2020-07-13 21:48:19

标签: android android-studio scroll material-design android-chips

我有一个带有ChipGroup和一些芯片的Horizo​​ntalScrollView。当我检查从屏幕上切出的芯片时,我希望ScrollView可以捕捉到并完整显示。

This is how it looks like when I select it.

我的布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorWhite"
    android:theme="@style/Theme.MaterialComponents">

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="165dp" />

    <LinearLayout
        android:id="@+id/linear_layout"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_marginTop="120dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <HorizontalScrollView
            android:id="@+id/scroll_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <com.google.android.material.chip.ChipGroup
                android:id="@+id/chip_group"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                app:chipSpacingHorizontal="10dp"
                app:singleLine="true"
                app:singleSelection="true"
                app:selectionRequired="true">

                <com.google.android.material.chip.Chip
                    android:id="@+id/chip_all"
                    android:layout_width="wrap_content"
                    android:layout_height="44dp"
                    android:layout_marginLeft="15dp"
                    android:backgroundTint="@color/indicator_chips"
                    android:checkable="true"
                    app:chipCornerRadius="10dp"
                    android:text="ALL"
                    android:textColor="@color/indicator_text"
                    app:checkedIconEnabled="false"
                    app:chipStrokeColor="@color/indicator_stroke"
                    app:chipStrokeWidth="1dp" />

                <com.google.android.material.chip.Chip
                    android:id="@+id/chip_watching"
                    android:layout_width="wrap_content"
                    android:layout_height="44dp"
                    android:checkable="true"
                    android:text="WATCHING"
                    android:textColor="@color/indicator_text"
                    app:chipCornerRadius="10dp"
                    app:checkedIconEnabled="false"
                    android:backgroundTint="@color/indicator_chips"
                    app:chipStrokeColor="@color/indicator_stroke"
                    app:chipStrokeWidth="1dp" />

                <com.google.android.material.chip.Chip
                    android:id="@+id/chip_completed"
                    android:layout_width="wrap_content"
                    android:layout_height="44dp"
                    android:checkable="true"
                    android:text="COMPLETED"
                    android:textColor="@color/indicator_text"
                    app:chipCornerRadius="10dp"
                    app:checkedIconEnabled="false"
                    android:backgroundTint="@color/indicator_chips"
                    app:chipStrokeColor="@color/indicator_stroke"
                    app:chipStrokeWidth="1dp" />

                <com.google.android.material.chip.Chip
                    android:id="@+id/chip_onhold"
                    android:layout_width="wrap_content"
                    android:layout_height="44dp"
                    android:checkable="true"
                    android:text="ON HOLD"
                    android:textColor="@color/indicator_text"
                    app:chipCornerRadius="10dp"
                    app:checkedIconEnabled="false"
                    android:backgroundTint="@color/indicator_chips"
                    app:chipStrokeColor="@color/indicator_stroke"
                    app:chipStrokeWidth="1dp" />

                <com.google.android.material.chip.Chip
                    android:id="@+id/chip_dropped"
                    android:layout_width="wrap_content"
                    android:layout_height="44dp"
                    android:checkable="true"
                    android:text="DROPPED"
                    android:textColor="@color/indicator_text"
                    app:chipCornerRadius="10dp"
                    app:checkedIconEnabled="false"
                    android:backgroundTint="@color/indicator_chips"
                    app:chipStrokeColor="@color/indicator_stroke"
                    app:chipStrokeWidth="1dp" />

                <com.google.android.material.chip.Chip
                    android:id="@+id/chip_plantowatch"
                    android:layout_width="wrap_content"
                    android:layout_height="44dp"
                    android:layout_marginRight="15dp"
                    android:checkable="true"
                    android:text="PLAN TO WATCH"
                    android:textColor="@color/indicator_text"
                    app:chipCornerRadius="10dp"
                    app:checkedIconEnabled="false"
                    android:backgroundTint="@color/indicator_chips"
                    app:chipStrokeColor="@color/indicator_stroke"
                    app:chipStrokeWidth="1dp" />

            </com.google.android.material.chip.ChipGroup>

        </HorizontalScrollView>

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

我的班级文件:

public class LibraryFragment extends Fragment {

    private HorizontalScrollView scrollView;
    Chip chip_dropped;
    ChipGroup chipGroup;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_library_anime, container, false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {

        scrollView = view.findViewById(R.id.scroll_view);
        chipGroup = view.findViewById(R.id.chip_group);

        chipGroup.setOnCheckedChangeListener(checkedListener);
    }

    ChipGroup.OnCheckedChangeListener checkedListener = new ChipGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(ChipGroup group, int checkedId) {
            FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
            switch (group.getCheckedChipId()) {
                case R.id.chip_all:
                    fragmentTransaction.replace(R.id.fragment_container, new ListALL()).commit();
                    break;
                case R.id.chip_watching:
                    fragmentTransaction.replace(R.id.fragment_container, new ListWATCHING()).commit();
                    break;
                case R.id.chip_completed:
                    fragmentTransaction.replace(R.id.fragment_container, new ListCOMPLETED()).commit();
                    break;
                case R.id.chip_onhold:
                    fragmentTransaction.replace(R.id.fragment_container, new ListONHOLD()).commit();
                    break;
                case R.id.chip_dropped:
                    fragmentTransaction.replace(R.id.fragment_container, new ListDROPPED()).commit();
                    break;
                case R.id.chip_plantowatch:
                    fragmentTransaction.replace(R.id.fragment_container, new ListPLANTOWATCH()).commit();
                    break;
            }

        }
    };
}

因此要重复一遍,我试图使ScrollView在单击时滚动到某个芯片上,like the play store with its.我尝试了.scroolTo和.smoothScrollTo,但没有用。

2 个答案:

答案 0 :(得分:3)

我为您制作了一个示例项目。我认为您可以轻松将其转换为您的应用。

MainActivity.java

public class MainActivity extends AppCompatActivity
{
    HorizontalScrollView scroll;
    int widthScreen;

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

        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        widthScreen = displayMetrics.widthPixels;

        scroll = findViewById(R.id.scroll);

        LinearLayout linearLayout = findViewById(R.id.linLay);
        for (int index = 0; index <= linearLayout.getChildCount() - 1; index++)
        {
            linearLayout.getChildAt(index).setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    Rect r = new Rect();
                    v.getGlobalVisibleRect(r);

                    if (r.right == widthScreen)
                    {
                        Rect rr = new Rect();
                        v.getDrawingRect(rr);
                        scroll.smoothScrollBy(rr.right - (widthScreen - r.left), 0);
                    }
                    else if (r.left == 0)
                    {
                        Rect rr = new Rect();
                        v.getDrawingRect(rr);
                        scroll.smoothScrollBy(rr.right - (widthScreen - r.right), 0);
                    }
                }
            });
        }
    }
}

MainActivity.XML(只是ScrollView中的按钮)

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scroll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/linLay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="3" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="4" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="5" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="6" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="7" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="8" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="9" />

    </LinearLayout>

</HorizontalScrollView>

当您单击屏幕上未完全显示的按钮时,ScrollView将滚动到适当位置以显示完整按钮。

答案 1 :(得分:1)

使用@iknow发布的代码,我将其更改为与具有其Chips的Play商店完全一样

 @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {

        chipGroup = view.findViewById(R.id.chip_group);

        final Chip chip_all = view.findViewById(R.id.chip_all);
        final Chip chip_watching = view.findViewById(R.id.chip_watching);
        final Chip chip_completed = view.findViewById(R.id.chip_completed);
        final Chip chip_onhold = view.findViewById(R.id.chip_onhold);
        final Chip chip_dropped = view.findViewById(R.id.chip_dropped);
        final Chip chip_plantowatch = view.findViewById(R.id.chip_plantowatch);

        DisplayMetrics displayMetrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        widthScreen = displayMetrics.widthPixels;

        scrollView = view.findViewById(R.id.scroll_view);
        chipGroup = view.findViewById(R.id.chip_group_anime);
        for (int index = 0; index <= chipGroup.getChildCount() - 1; index++) {
            chipGroup.getChildAt(index).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Rect r = new Rect();
                    view.getGlobalVisibleRect(r);

                    if(chip_all.isChecked()) {
                        Rect rr = new Rect();
                        view.getDrawingRect(rr);
                        scrollView.smoothScrollBy(rr.right - (widthScreen - r.right), 0);
                    }

                    if(chip_watching.isChecked()) {
                        Rect rr = new Rect();
                        view.getDrawingRect(rr);
                        scrollView.smoothScrollBy(rr.right - (widthScreen - r.right), 0);
                    }


                    if(chip_completed.isChecked()) {
                        scrollView.smoothScrollTo(chip_completed.getLeft() - (widthScreen / 2) + (chip_completed.getWidth() / 2), 0);
                    }

                    if(chip_onhold.isChecked()) {
                        scrollView.smoothScrollTo(chip_onhold.getLeft() - (widthScreen / 2) + (chip_onhold.getWidth() / 2), 0);
                    }


                    if(chip_dropped.isChecked()) {
                        Rect rr = new Rect();
                        view.getDrawingRect(rr);
                        scrollView.smoothScrollBy(r.right, 0);
                    }

                    if(chip_plantowatch.isChecked()) {
                        Rect rr = new Rect();
                        view.getDrawingRect(rr);
                        scrollView.smoothScrollBy(r.right, 0);
                    }
                }
            });
        }

    }

The end product looks like this.

最终产品看起来像上面的gif,再次感谢@iknow谁提供了代码!

相关问题