Reputation: 43
I have used a fragment inside which recyler view is used to load list elements.
Following is the layout file for fragment.
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/languagebar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:liftOnScroll="true">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleGravity="top"
app:expandedTitleMarginStart="5dp"
app:expandedTitleMarginTop="5dp"
android:layout_gravity="top"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
<com.google.android.flexbox.FlexboxLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginLeft="@dimen/spacing_large"
android:layout_marginRight="@dimen/spacing_large"
app:flexWrap="wrap"
app:justifyContent="flex_start">
<Button
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:id="@+id/buttonEnglish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/btn_rounded_yellow_white"
android:onClick="outletTypeClick"
android:text="English"
android:textAllCaps="false"
android:textColor="@color/grey_80" />
<Button
style="?android:attr/borderlessButtonStyle"
android:id="@+id/buttonHindi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/btn_rounded_yellow_white"
android:onClick="outletTypeClick"
android:text="Hindi"
android:textAllCaps="false"
android:textColor="@color/grey_80" />
</com.google.android.flexbox.FlexboxLayout>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:scrollbars="none"
android:scrollingCache="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Following is the initialization of recylerview inside fragment -
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
// Defines the xml file for the fragment
View rootView = inflater.inflate(R.layout.fragment_timeline1, parent, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
RecyclerLinearLayoutManager recyclerLinearLayoutManager =
new RecyclerLinearLayoutManager(rootView.getContext(), RecyclerView.VERTICAL, false);
recyclerView.setLayoutManager(recyclerLinearLayoutManager);
recyclerView.setHasFixedSize(true);
Here is the code from viewadapter -
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
RecyclerView.ViewHolder viewHolder;
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
switch (viewType) {
case TITLE:
View v1 = inflater.inflate(R.layout.item_timeline_title, viewGroup, false);
v1.setMinimumWidth(viewGroup.getMinimumWidth());
viewHolder = new TitleViewHolder(v1);
break;
case HEADLINE:
View v2 = inflater.inflate(R.layout.item_timeline_headline, viewGroup, false);
viewHolder = new ViewHolder1(v2);
break;
.....
}
return viewHolder;
}
Adding row item's xml.
item-layout-1 (heterogeneous view) -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/spacing_smlarge"
android:layout_marginStart="@dimen/spacing_smlarge"
android:gravity="center_horizontal"
android:orientation="vertical">
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:background="@color/grey_10" />
<ImageView
android:layout_width="@dimen/spacing_middle"
android:layout_height="@dimen/spacing_middle"
android:layout_marginTop="@dimen/spacing_large"
android:tint="@color/pink_400"
app:srcCompat="@drawable/shape_round_solid" />
</RelativeLayout>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/spacing_medium"
android:layout_marginLeft="@dimen/spacing_middle"
android:layout_marginRight="@dimen/spacing_middle"
android:layout_marginTop="@dimen/spacing_medium"
android:visibility="visible"
app:cardCornerRadius="2dp"
app:cardElevation="1dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/spacing_medium"
android:layout_marginLeft="@dimen/spacing_large"
android:layout_marginRight="@dimen/spacing_large"
android:layout_marginTop="@dimen/spacing_large"
android:gravity="center_vertical"
android:orientation="horizontal">
<View
android:layout_width="@dimen/spacing_xxlarge"
android:layout_height="@dimen/spacing_xlarge" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/item_itinerary_text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="casual"
android:text="Aman Weds Shreya"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
android:textColor="@color/red_500"
android:textSize="40sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<ImageView
android:id="@+id/item_itinerary_image1"
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="140dp"
android:layout_marginStart="@dimen/spacing_large"
android:layout_marginTop="@dimen/spacing_large"
android:layout_marginEnd="@dimen/spacing_large"
android:layout_marginBottom="@dimen/spacing_large"
android:foreground="@color/overlay_light_20"
android:src="@drawable/wedding_logo" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</LinearLayout>
item-layout-2 (heterogeneous view)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content">
<LinearLayout
android:id="@+id/lyt_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/spacing_smlarge"
android:layout_marginStart="@dimen/spacing_smlarge"
android:gravity="center_horizontal"
android:orientation="vertical">
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:background="@color/grey_10" />
<ImageView
android:layout_width="@dimen/spacing_middle"
android:layout_height="@dimen/spacing_middle"
android:layout_marginTop="@dimen/spacing_large"
android:tint="@color/light_green_400"
app:srcCompat="@drawable/shape_round_solid" />
</RelativeLayout>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/spacing_medium"
android:layout_marginLeft="@dimen/spacing_middle"
android:layout_marginRight="@dimen/spacing_middle"
android:layout_marginTop="@dimen/spacing_middle"
android:visibility="visible"
app:cardCornerRadius="2dp"
app:cardElevation="1dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/spacing_medium"
android:layout_marginLeft="@dimen/spacing_large"
android:layout_marginRight="@dimen/spacing_large"
android:layout_marginTop="@dimen/spacing_large"
android:gravity="center_vertical"
android:orientation="horizontal">
<com.mikhaellopez.circularimageview.CircularImageView
android:id="@+id/item_itinerary_image1"
android:layout_width="@dimen/spacing_xlarge"
android:layout_height="@dimen/spacing_xlarge"
android:foreground="@color/overlay_light_20"
android:src="@drawable/hotel_highwayking"
app:civ_shadow="true"
app:civ_shadow_radius="0"
app:civ_border="false" />
<View
android:layout_width="@dimen/spacing_large"
android:layout_height="0dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/item_itinerary_text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="Refreshments"
android:textAppearance="@style/TextAppearance.AppCompat.Caption"
android:textColor="@color/light_blue_400"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="@+id/item_itinerary_text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/spacing_small"
android:gravity="center_vertical"
android:text="30 mins stay"
android:textColor="@color/grey_20"
android:textSize="@dimen/spacing_middle" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/item_itinerary_text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/spacing_large"
android:lineSpacingExtra="4dp"
android:text="Morning Refreshment at Hotel Highway King, Jaipur ."
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
android:textColor="@color/grey_60" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</LinearLayout>
In reference to the attached image - When I set image and text in layout file itself, it is occupying more width(as shown in attached screenshot). Whereas when I load the image and text via code, view renders to 20% of width. Also, the width of the recyler-view-section(as in below image) remains same in both landscape and portrait mode. Above observations looks like recyler-view width is rendering as wrap-content and not as match_parent. But I am not able to understand why this is happening as I have not used that logic in my code.
Upvotes: 0
Views: 499
Reputation: 1436
As you are loading fragment in the activity, check your FrameLayout container, you could have set its width to wrap_content, please change it to match_parent.
Upvotes: 2