gpt4 book ai didi

android - 在水平 ScrollView 中单击时如何滚动到芯片?

转载 作者:行者123 更新时间:2023-12-04 01:18:59 25 4
gpt4 key购买 nike

我有一个带有 ChipGroup 和一些芯片的 Horizo​​ntalScrollView。当我检查从屏幕上切出的 Chip 时,我希望 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>

还有我的类(class)文件:

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 在被点击时滚动到 Chip,like the play store with its.我尝试了 .scroolTo 和 .smoothScrollTo,但都不起作用。

最佳答案

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

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 将滚动到适当的位置以显示完整的按钮。

关于android - 在水平 ScrollView 中单击时如何滚动到芯片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62884792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com