【发布时间】:2018-03-06 12:29:36
【问题描述】:
详情:
依赖:(最新)
implementation 'com.android.support:recyclerview-v7:27.0.2'
XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bg_gray">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/marginTwenty"
android:layout_marginStart="@dimen/marginTwenty"
android:layout_marginTop="@dimen/marginTen"
android:orientation="vertical">
<LinearLayout
android:id="@+id/contactLoadingLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:padding="@dimen/marginThirty">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/waitText"
android:textAlignment="center"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="@+id/contactsLayoutView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="@+id/mobileView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/curved_background_white"
android:orientation="vertical"
android:padding="@dimen/marginTen">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="@dimen/marginTwenty"
android:layout_height="@dimen/marginTwenty"
android:layout_gravity="center_vertical"
android:layout_marginEnd="5dp"
android:contentDescription="@string/dummyContent"
android:src="@drawable/ic_mobile" />
<TextView
android:id="@+id/txtMobileNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/mobileNumber"
android:textColor="@color/colorPrimary"
android:textSize="@dimen/textSize_18sp"
android:textStyle="bold" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/mobileRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:adjustViewBounds="true"
android:scaleType="fitXY" />
<TextView
android:id="@+id/txtNoMobileData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="@dimen/marginTen"
android:text="@string/no_contact_found"
android:textSize="@dimen/textSize_18sp"
android:textStyle="bold"
android:visibility="gone" />
<TextView
android:id="@+id/btnAddMobile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="@string/add"
android:textColor="@color/colorPrimary"
android:textSize="@dimen/textSize_18sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="@+id/emailView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/marginTen"
android:background="@drawable/curved_background_white"
android:orientation="vertical"
android:padding="@dimen/marginTen">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="@dimen/marginTwenty"
android:layout_height="18dp"
android:layout_gravity="center_vertical"
android:layout_marginEnd="5dp"
android:contentDescription="@string/dummyContent"
android:src="@drawable/ic_email" />
<TextView
android:id="@+id/txtEmailNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/email"
android:textColor="@color/colorPrimary"
android:textSize="@dimen/textSize_18sp"
android:textStyle="bold" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/emailRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:adjustViewBounds="true"
android:scaleType="fitXY" />
<TextView
android:id="@+id/txtNoEmailData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="@dimen/marginTen"
android:text="@string/no_contact_found"
android:textSize="@dimen/textSize_18sp"
android:textStyle="bold"
android:visibility="gone" />
<TextView
android:id="@+id/btnAddEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="@string/add"
android:textColor="@color/colorPrimary"
android:textSize="@dimen/textSize_18sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="@+id/facebookView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/marginTen"
android:background="@drawable/curved_background_white"
android:orientation="vertical"
android:padding="@dimen/marginTen">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="@dimen/marginTwenty"
android:layout_height="@dimen/marginTwenty"
android:layout_gravity="center_vertical"
android:layout_marginEnd="5dp"
android:contentDescription="@string/dummyContent"
android:src="@drawable/ic_facebook" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/facebookHint"
android:textColor="@color/colorPrimary"
android:textSize="@dimen/textSize_18sp"
android:textStyle="bold" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/facebookRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:adjustViewBounds="true"
android:scaleType="fitXY" />
<TextView
android:id="@+id/txtNoFacebookData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="@dimen/marginTen"
android:text="@string/no_contact_found"
android:textSize="@dimen/textSize_18sp"
android:textStyle="bold"
android:visibility="gone" />
<TextView
android:id="@+id/btnAddFacebook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="@string/add"
android:textColor="@color/colorPrimary"
android:textSize="@dimen/textSize_18sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="@+id/linkedInView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/marginTen"
android:background="@drawable/curved_background_white"
android:orientation="vertical"
android:padding="@dimen/marginTen">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="@dimen/marginTwenty"
android:layout_height="@dimen/marginTwenty"
android:layout_gravity="center_vertical"
android:layout_marginEnd="5dp"
android:contentDescription="@string/dummyContent"
android:src="@drawable/ic_linkedi" />
<TextView
android:id="@+id/txtLinkedIn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/linkedInHint"
android:textColor="@color/colorPrimary"
android:textSize="@dimen/textSize_18sp"
android:textStyle="bold" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/linkedInRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:adjustViewBounds="true"
android:scaleType="fitXY" />
<TextView
android:id="@+id/txtNoLinkedInData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="@dimen/marginTen"
android:text="@string/no_contact_found"
android:textSize="@dimen/textSize_18sp"
android:textStyle="bold"
android:visibility="gone" />
<TextView
android:id="@+id/btnAddLinkedIn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="@string/add"
android:textColor="@color/colorPrimary"
android:textSize="@dimen/textSize_18sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="@+id/twitterView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/marginTen"
android:background="@drawable/curved_background_white"
android:orientation="vertical"
android:padding="@dimen/marginTen">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="@dimen/marginTwenty"
android:layout_height="@dimen/marginTwenty"
android:layout_gravity="center_vertical"
android:layout_marginEnd="5dp"
android:contentDescription="@string/dummyContent"
android:src="@drawable/ic_twitter" />
<TextView
android:id="@+id/txtTwitter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/twitterHint"
android:textColor="@color/colorPrimary"
android:textSize="@dimen/textSize_18sp"
android:textStyle="bold" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/twitterRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:adjustViewBounds="true"
android:scaleType="fitXY" />
<TextView
android:id="@+id/txtNoTwitterData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="@dimen/marginTen"
android:text="@string/no_contact_found"
android:textSize="@dimen/textSize_18sp"
android:textStyle="bold"
android:visibility="gone" />
<TextView
android:id="@+id/btnAddTwitter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="@string/add"
android:textColor="@color/colorPrimary"
android:textSize="@dimen/textSize_18sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="@+id/youtubeView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/marginTen"
android:background="@drawable/curved_background_white"
android:orientation="vertical"
android:padding="@dimen/marginTen">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="@dimen/marginTwenty"
android:layout_height="@dimen/marginTwenty"
android:layout_gravity="center_vertical"
android:layout_marginEnd="5dp"
android:contentDescription="@string/dummyContent"
android:src="@drawable/ic_youtube" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/youtubeHint"
android:textColor="@color/colorPrimary"
android:textSize="@dimen/textSize_18sp"
android:textStyle="bold" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/youtubeRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:adjustViewBounds="true"
android:scaleType="fitXY" />
<TextView
android:id="@+id/txtNoYoutubeData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="@dimen/marginTen"
android:text="@string/no_contact_found"
android:textSize="@dimen/textSize_18sp"
android:textStyle="bold"
android:visibility="gone" />
<TextView
android:id="@+id/btnAddYoutube"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="@string/add"
android:textColor="@color/colorPrimary"
android:textSize="@dimen/textSize_18sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="@+id/gitHubView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/marginTwenty"
android:layout_marginTop="@dimen/marginTen"
android:background="@drawable/curved_background_white"
android:orientation="vertical"
android:padding="@dimen/marginTen">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="@dimen/marginTwenty"
android:layout_height="@dimen/marginTwenty"
android:layout_gravity="center_vertical"
android:layout_marginEnd="5dp"
android:contentDescription="@string/dummyContent"
android:src="@drawable/ic_github" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:text="@string/gitHubHint"
android:textColor="@color/colorPrimary"
android:textSize="@dimen/textSize_18sp"
android:textStyle="bold" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/githubRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp" />
<TextView
android:id="@+id/txtNoGitHubData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="@dimen/marginTen"
android:text="@string/no_contact_found"
android:textSize="@dimen/textSize_18sp"
android:textStyle="bold"
android:visibility="gone" />
<TextView
android:id="@+id/btnAddGithub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="@string/add"
android:textColor="@color/colorPrimary"
android:textSize="@dimen/textSize_18sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<include
android:id="@+id/noInternetLayoutContact"
layout="@layout/no_internet_layout"
android:visibility="gone" />
</LinearLayout>
</ScrollView>
JAVA 代码:
youtubeRecyclerView.setLayoutManager(new
LinearLayoutManager(getActContext(), LinearLayoutManager.VERTICAL, false));
youtubeRecyclerView.setHasFixedSize(true);
youtubeRecyclerView.setItemAnimator(new DefaultItemAnimator());
youtubeRecyclerView.setAdapter(attributeAdapter);
youtubeRecyclerView.setVisibility(View.VISIBLE);
注意:
- 我在单个活动中有 6 个回收站视图,在这 6 个回收站视图中,任何一个回收站视图都可以滚动,其余部分可以正常工作(wrap_content),具体取决于屏幕大小。
- 我的父视图是 ScrollView。
我对互联网和 stackoverflow 做了足够的研究,找不到解决方案,所以在这里提出问题。请不要标记为重复。
【问题讨论】:
-
android:adjustViewBounds="true" android:scaleType="fitXY"是 imageView 的属性 -
请发布整个 xml 文件。很难理解你想得到什么;)
-
我的 recyclerview 没有这些属性,我试过只是实验它没有成功。
-
@muminers,添加了整个 XML 文件代码。
-
试试this解决方案
标签: android android-recyclerview