我是 Android 開發的初學者。目前,我正在為我們的專案制作一個應用程式。在我的應用程式中,我TabLayout在布局中間有一個帶有ViewPager below it.在我的 XML 布局中,我設定了ViewPagerto的高度match_parent以填充下面的剩余空間。
我有兩個片段用于ViewPager,第一個片段包含 a GridView,另一個包含ListView.
這些視圖將填充來自我的 Firebase 的資料。現在我所做的是ViewPager根據螢屏的高度減去TabLayout狀態欄的高度來設定高度,這樣當用戶滾動到最底部時,TabLayout就會像他們一樣在應用欄下方合并在一起了。
我的問題是,在以ViewPager編程方式設定高度后,NestedScrollView 自動滾動到最底部。我已經在 Android 11 (API 30) 上測驗了我的應用程式并且運行良好,但是當我在 Android 5 (API 21) 上運行它時,我看到了滾動問題。
我也曾嘗試滾動NestedScrollView編程方式nestedScrollView.fullScroll(View.FOCUS_UP)和nestedScrollView.scrollTo(0, 0)高度設定之后,但好像沒有什么改變。
這是我的片段布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/toolbar"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme2">
<androidx.core.widget.NestedScrollView
android:id="@ id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:fillViewport="true"
android:overScrollMode="never">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@ id/topLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@ id/civ_profile_picture"
android:layout_width="125dp"
android:layout_height="125dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:scaleType="centerCrop"
android:src="@drawable/placeholder_profile_picture"
app:civ_border_color="@color/white"
app:civ_border_width="1dp"
tools:ignore="MissingConstraints" />
<RelativeLayout
android:id="@ id/layout_change_profile_picture"
android:layout_width="125dp"
android:layout_height="125dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:visibility="gone">
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#88000000"
app:civ_border_color="@color/white"
app:civ_border_width="1dp" />
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerInParent="true"
android:src="@drawable/ic_baseline_photo_camera_white_24" />
</RelativeLayout>
<EditText
android:id="@ id/type_edit"
android:layout_width="170dp"
android:layout_height="35dp"
android:layout_below="@id/username_edit"
android:layout_centerHorizontal="true"
android:gravity="center"
android:hint="type"
android:inputType="textNoSuggestions"
android:lines="1"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="14sp"
android:visibility="gone"
app:boxBackgroundMode="none" />
<EditText
android:id="@ id/username_edit"
android:layout_width="170dp"
android:layout_height="40dp"
android:layout_below="@id/civ_profile_picture"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:gravity="center"
android:hint="Username"
android:inputType="textNoSuggestions"
android:lines="1"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="20sp"
android:visibility="gone"
app:boxBackgroundMode="none" />
<TextView
android:id="@ id/Username"
android:layout_width="190dp"
android:layout_height="wrap_content"
android:layout_below="@id/civ_profile_picture"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:background="@null"
android:ellipsize="end"
android:fontFamily="@font/roboto"
android:gravity="center"
android:maxWidth="240dp"
android:maxLines="1"
android:scrollHorizontally="true"
android:text="Username"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="20dp" />
<TextView
android:id="@ id/TypesArtists"
android:layout_width="190dp"
android:layout_height="wrap_content"
android:layout_below="@id/Username"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:background="@null"
android:ellipsize="end"
android:fontFamily="@font/poppins"
android:gravity="center"
android:maxWidth="240dp"
android:maxLines="1"
android:scrollHorizontally="true"
android:text="Type"
android:textAlignment="center"
android:textColor="@color/gray_text_light"
android:textSize="14dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/TypesArtists"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:layout_marginBottom="20dp"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@ id/txt_rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="0.0"
android:textColor="@color/white"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@ id/txt_rating"
android:layout_centerHorizontal="true"
android:layout_marginTop="4dp"
android:fontFamily="@font/poppins"
android:text="Rating"
android:textColor="@color/accent1"
android:textSize="14sp" />
</RelativeLayout>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginHorizontal="20dp"
android:background="@color/divider_light" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@ id/txt_post_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="0"
android:textColor="@color/white"
android:textSize="18sp" />
<TextView
android:id="@ id/txt_post"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@ id/txt_post_count"
android:layout_centerHorizontal="true"
android:layout_marginTop="4dp"
android:fontFamily="@font/poppins"
android:text="Posts"
android:textColor="@color/accent1"
android:textSize="14sp" />
</RelativeLayout>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginHorizontal="16dp"
android:background="@color/divider_light" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@ id/txt_reviews_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="0"
android:textColor="@color/white"
android:textSize="18sp" />
<TextView
android:id="@ id/txt_review"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@ id/txt_reviews_count"
android:layout_centerHorizontal="true"
android:layout_marginTop="4dp"
android:fontFamily="@font/poppins"
android:text="Reviews"
android:textColor="@color/accent1"
android:textSize="14sp" />
</RelativeLayout>
</LinearLayout>
<TextView
android:id="@ id/ratingTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/TypesArtists"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:fontFamily="@font/poppins"
android:text="rating 0/5"
android:textColor="@color/white"
android:textSize="14dp"
android:visibility="gone" />
<com.iarcuschin.simpleratingbar.SimpleRatingBar
android:id="@ id/rating_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/ratingTxt"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:visibility="gone"
app:srb_drawBorderEnabled="false"
app:srb_fillColor="@color/accent2"
app:srb_isIndicator="true"
app:srb_maxStarSize="14dp"
app:srb_numberOfStars="5"
app:srb_rating="5"
app:srb_starBackgroundColor="#172435"
app:srb_starSize="20dp"
app:srb_stepSize="0.5" />
</RelativeLayout>
<com.google.android.material.tabs.TabLayout
android:id="@ id/tab_layout"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_below="@id/topLayout"
android:layout_centerInParent="true"
android:background="@color/item_dark"
android:outlineSpotShadowColor="@color/Magenta"
app:tabIndicatorColor="@color/Magenta"
app:tabIndicatorHeight="3dp"
app:tabSelectedTextColor="@color/white"
app:tabTextAppearance="@style/TabLayoutTextAppearance"
app:tabTextColor="@color/gray_text_dark"
tools:layout_editor_absoluteX="0dp" />
<androidx.viewpager.widget.ViewPager
android:id="@ id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/tab_layout"
android:background="@color/Background0" />
</RelativeLayout>
</androidx.core.widget.NestedScrollView>
</RelativeLayout>
這是設定 ViewPager 高度的方法:
private void setViewPagerParams() {
tabLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
tabLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
//get height of tab layout (px)
int tabLayoutHeight = tabLayout.getHeight();
//get height of screen (px)
Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
//get height of status bar
Rect rectangle = new Rect();
Window window = getActivity().getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
int statusBarHeight = rectangle.top;
heightOfViewPager = size.y - (tabLayoutHeight statusBarHeight);
ViewGroup.LayoutParams viewPagerParams = viewPager.getLayoutParams();
viewPagerParams.height = heightOfViewPager;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(viewPagerParams);
params.addRule(RelativeLayout.BELOW, R.id.tab_layout);
viewPager.setLayoutParams(params);
}
});
}
uj5u.com熱心網友回復:
更新:
經過數小時的研究和代碼擺弄,結果我的問題出在我的 XML 檔案中。
我將根元素從 RelativeLayout 更改為 LinearLayout,并將android:descendantFocusability="blocksDescendants"其添加為它的屬性。
這個答案幫助了我
另一個更新:雖然滾動停止了,但根布局會LinearLayout停止其所有后代視圖的聚焦,從而阻止所有輸入EditText包含在其中。更好的解決方案是將android:focusableInTouchMode屬性設定為true,這樣編輯文本仍然可以聚焦,同時防止不需要的滾動。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/403859.html
標籤:
