Lagging recycler view - android

I have made a recyclerView which is inside a fragment. It has an ImageView in each item and the image is loaded with Picasso from a url. But the problem is that the recyclerView lags when i scroll for the first time, but when the data is loaded, it scrolls normally. I tried to remove all the operations in the onBindViewHolder but the problem still persists. I then removed the complex part of the item layout but still no improvements. I am running on Snapdragon 845 so there is no issue with the phone's performance, I tried the same with other devices and it lags. I even tried with hardware acceleration, but it didn't help.
Here is my Adapter:
public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.MovieHolder> {
private List<Movie> mMovies;
private Context mContext;
public MovieAdapter(Context mContext, List<Movie> mMovies) {
this.mContext = mContext;
this.mMovies = mMovies;
}
#NonNull
#Override
public MovieHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item_movie_movie_layout, viewGroup, false);
return new MovieHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MovieHolder movieHolder, int i) {
Movie movie = mMovies.get(i);
if (movie.getTitle2().equals("")) {
movieHolder.mTextViewTitle1.setText(movie.getTitle1());
movieHolder.mTextViewTitle2.setVisibility(View.GONE);
} else {
movieHolder.mTextViewTitle1.setText(movie.getTitle1());
movieHolder.mTextViewTitle2.setVisibility(View.VISIBLE);
movieHolder.mTextViewTitle2.setText(movie.getTitle2());
}
movieHolder.mTextViewGenre.setText(movie.getGenre());
movieHolder.mRatingBar.setRating((float) 4.5);
String url = "https://image.tmdb.org/t/p/w342";
url += movie.getPoster_path();
Transformation transformation = new Rounded(8, ALL);
Picasso.get().load(url)
.placeholder(R.color.colorAccent)
.transform(transformation)
.resize(100, 150)
.centerCrop()
.into(movieHolder.mImageViewPoster);
}
#Override
public int getItemCount() {
return mMovies.size();
}
public class MovieHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private ImageView mImageViewPoster;
private TextView mTextViewTitle1;
private TextView mTextViewTitle2;
private TextView mTextViewGenre;
private RatingBar mRatingBar;
public MovieHolder(#NonNull View itemView) {
super(itemView);
mImageViewPoster = itemView.findViewById(R.id.image_view_poster);
mTextViewTitle1 = itemView.findViewById(R.id.text_view_title_1);
mTextViewTitle2 = itemView.findViewById(R.id.text_view_title_2);
mTextViewGenre = itemView.findViewById(R.id.text_view_genre);
mRatingBar = itemView.findViewById(R.id.rating_bar);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Movie movie = mMovies.get(getAdapterPosition());
Intent intent = new Intent(mContext, MovieActivity.class);
intent.putExtra("EXTRA", movie);
mContext.startActivity(intent);
}
}
}
Here is my item layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="wrap_content">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="192dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.v7.widget.CardView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="132dp"
android:layout_marginStart="28dp"
android:layout_marginEnd="28dp"
android:layout_marginBottom="8dp"
android:elevation="0dp"
app:cardBackgroundColor="#color/colorAccent"
app:cardCornerRadius="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
</android.support.v7.widget.CardView>
<ImageView
android:id="#+id/image_view_poster"
android:layout_width="100dp"
android:layout_height="150dp"
android:layout_marginStart="12dp"
android:layout_marginBottom="12dp"
android:adjustViewBounds="true"
android:elevation="16dp"
app:layout_constraintBottom_toBottomOf="#+id/imageView"
app:layout_constraintStart_toStartOf="#+id/imageView" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="12dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="12dp"
android:elevation="8dp"
android:gravity="center_vertical"
android:orientation="vertical"
android:paddingLeft="8dp"
app:layout_constraintBottom_toBottomOf="#+id/imageView"
app:layout_constraintEnd_toEndOf="#+id/imageView"
app:layout_constraintStart_toEndOf="#+id/image_view_poster"
app:layout_constraintTop_toTopOf="#+id/imageView">
<TextView
android:id="#+id/text_view_title_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:includeFontPadding="false"
android:maxLines="1"
android:text="Skyscraper"
android:textSize="16dp" />
<TextView
android:id="#+id/text_view_title_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alpha="0.3"
android:ellipsize="end"
android:includeFontPadding="false"
android:maxLines="1"
android:text="Skyscraper"
android:textSize="12dp" />
<Space
android:layout_width="0dp"
android:layout_height="8dp" />
<android.support.v7.widget.AppCompatRatingBar
android:id="#+id/rating_bar"
style="#style/Base.Widget.AppCompat.RatingBar.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isIndicator="true"
android:numStars="5"
android:outlineProvider="paddedBounds"
android:progressBackgroundTint="#FFFFFF"
android:progressTint="#FECE00"
android:stepSize="0.5" />
<Space
android:layout_width="0dp"
android:layout_height="8dp" />
<TextView
android:id="#+id/text_view_genre"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:includeFontPadding="false"
android:text="Action | Adventure"
android:textSize="12dp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>

Related

How to create Recyclerview game time line events

I am developing a Game fans app. I want to create a layout given below
I have great experience in making custom recycelerviews with multiple LAYOUT_VIEW_TYPES.
My question is this the best way to create that recyclerview or I can do this in more easyway
Yes the best way to achieve that result is by creating a recyclerview with multiple layouts and I think that it's the only way if you are using xml.
But this could be a lot easier if you make it using jetpack compose no need for recycler view or adapter anymore with compose and you can make any custom layout that you want.
Phew... Finally I did it with Recyclerview custom layout items left and right.
Sharing my work.
here is my layout_item_time_line_left.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".livescore.TimeLineFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:background="#drawable/rounded_corner_time_line_item_bg"
android:padding="10dp"
app:layout_constraintEnd_toStartOf="#+id/txtPlayerName2"
app:layout_constraintTop_toTopOf="parent">
<com.mikhaellopez.circularimageview.CircularImageView
android:id="#+id/imgPlayerImage"
android:layout_width="32dp"
android:layout_height="32dp"
android:src="#drawable/img_upload_profile"
app:border_color="#color/white"
app:civ_border_width="1dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:selector_stroke_color="#color/white" />
<TextView
android:id="#+id/txtPlayerName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginStart="5dp"
android:gravity="center_horizontal"
android:text="Neymar"
android:textColor="#66666a"
android:textSize="12sp"
app:layout_constraintStart_toEndOf="#+id/imgPlayerImage"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/txtEventName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginStart="5dp"
android:gravity="center_horizontal"
android:text="Goal"
android:textColor="#color/txt_color_live_score"
android:textSize="12sp"
app:layout_constraintStart_toEndOf="#+id/imgPlayerImage"
app:layout_constraintTop_toBottomOf="#+id/txtPlayerName" />
<com.mikhaellopez.circularimageview.CircularImageView
android:id="#+id/imgEventImage"
android:layout_width="12dp"
android:layout_height="12dp"
android:layout_marginStart="4dp"
android:layout_marginTop="2dp"
android:src="#drawable/ic_sports_footbal"
app:border_width="1dp"
app:civ_border_width="1dp"
app:layout_constraintStart_toEndOf="#+id/txtEventName"
app:layout_constraintTop_toBottomOf="#+id/txtPlayerName"
app:selector_stroke_color="#color/white" />
</androidx.constraintlayout.widget.ConstraintLayout>
<ImageView
android:id="#+id/imgEventImage2"
android:layout_width="12dp"
android:layout_height="12dp"
android:scaleType="fitXY"
android:src="#drawable/ic_online"
app:border_width="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:selector_stroke_color="#color/white" />
<View
android:id="#+id/viewLineUpper"
android:layout_width="1dp"
android:layout_height="20dp"
android:background="#41415A"
app:layout_constraintBottom_toTopOf="#+id/imgEventImage2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="#+id/viewLineLower"
android:layout_width="1dp"
android:layout_height="20dp"
android:background="#41415A"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imgEventImage2" />
<TextView
android:id="#+id/txtPlayerName2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginTop="15dp"
android:gravity="center_horizontal"
android:text="33''"
android:textColor="#66666a"
android:textSize="8sp"
app:layout_constraintEnd_toStartOf="#+id/imgEventImage2"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
here is my layout_item_time_line_right.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:background="#drawable/rounded_corner_time_line_item_bg"
android:padding="10dp"
app:layout_constraintStart_toEndOf="#+id/txtPlayerName2"
app:layout_constraintTop_toTopOf="parent">
<com.mikhaellopez.circularimageview.CircularImageView
android:id="#+id/imgPlayerImage"
android:layout_width="32dp"
android:layout_height="32dp"
android:src="#drawable/img_upload_profile"
app:border_color="#color/white"
app:civ_border_width="1dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:selector_stroke_color="#color/white" />
<TextView
android:id="#+id/txtPlayerName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginStart="5dp"
android:gravity="center_horizontal"
android:text="Neymar"
android:textColor="#66666a"
android:textSize="12sp"
app:layout_constraintStart_toEndOf="#+id/imgPlayerImage"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/txtEventName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginStart="5dp"
android:gravity="center_horizontal"
android:text="Goal"
android:textColor="#color/txt_color_live_score"
android:textSize="12sp"
app:layout_constraintStart_toEndOf="#+id/imgPlayerImage"
app:layout_constraintTop_toBottomOf="#+id/txtPlayerName" />
<com.mikhaellopez.circularimageview.CircularImageView
android:id="#+id/imgEventImage"
android:layout_width="12dp"
android:layout_height="12dp"
android:layout_marginStart="4dp"
android:layout_marginTop="2dp"
android:src="#drawable/ic_sports_footbal"
app:civ_border_width="1dp"
app:layout_constraintStart_toEndOf="#+id/txtEventName"
app:layout_constraintTop_toBottomOf="#+id/txtPlayerName"
app:selector_stroke_color="#color/white" />
</androidx.constraintlayout.widget.ConstraintLayout>
<ImageView
android:id="#+id/imgEventImage2"
android:layout_width="12dp"
android:layout_height="12dp"
android:scaleType="fitXY"
android:src="#drawable/ic_online"
app:border_width="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:selector_stroke_color="#color/white" />
<View
android:id="#+id/viewLineUpper"
android:layout_width="1dp"
android:layout_height="20dp"
android:background="#41415A"
app:layout_constraintBottom_toTopOf="#+id/imgEventImage2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="#+id/viewLineLower"
android:layout_width="1dp"
android:layout_height="20dp"
android:background="#41415A"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imgEventImage2" />
<TextView
android:id="#+id/txtPlayerName2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginTop="15dp"
android:gravity="center_horizontal"
android:text="33''"
android:textColor="#66666a"
android:textSize="8sp"
app:layout_constraintStart_toEndOf="#+id/imgEventImage2"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
here is my fragment fragment_time_line.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView 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="#020321"
tools:context=".livescore.TimeLineFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#drawable/rounded_border_live_score_tab_table"
android:padding="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/txtBallPossession"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:gravity="center_horizontal"
android:text="Ball possession"
android:textColor="#66666a"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="#+id/layout_possession_percentage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
app:layout_constraintTop_toBottomOf="#+id/txtBallPossession"
tools:layout_editor_absoluteX="0dp">
<TextView
android:id="#+id/txtLeftPercentage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="70%"
android:textColor="#00ffac"
android:textSize="12sp"
android:textStyle="bold" />
<ProgressBar
android:id="#+id/progressBarLeft"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:indeterminate="false"
android:indeterminateTint="#00ffac"
android:max="100"
android:progress="50"
android:rotation="180" />
<TextView
android:id="#+id/txtRightPercentage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="30%"
android:textColor="#ff0909"
android:textSize="12sp"
android:textStyle="bold" />
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#drawable/rounded_corner_card"
android:clipToOutline="true"
android:scaleType="fitXY"
android:src="#drawable/img_footbal_ground_green"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/layout_possession_percentage" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view_match_timeline"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/constraintLayout" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
and here is my JAVA code for fragment
/**
* A simple {#link Fragment} subclass.
* Use the {#link TimeLineFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class TimeLineFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public TimeLineFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* #param param1 Parameter 1.
* #param param2 Parameter 2.
* #return A new instance of fragment TimeLineFragment.
*/
// TODO: Rename and change types and number of parameters
public static TimeLineFragment newInstance(String param1, String param2) {
TimeLineFragment fragment = new TimeLineFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
private View mView;
private RecyclerView recyclerViewMatchTimeline;
private ArrayList<MatchTimeLine> items = new ArrayList<>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mView = inflater.inflate(R.layout.fragment_time_line, container, false);
recyclerViewMatchTimeline = mView.findViewById(R.id.recycler_view_match_timeline);
LinearLayoutManager matchTimelineLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
recyclerViewMatchTimeline.setHasFixedSize(true);
recyclerViewMatchTimeline.setLayoutManager(matchTimelineLayoutManager);
recyclerViewMatchTimeline.setAdapter(new TimeLineMatchEventsRecyclerViewAdapter(items));
return mView;
}
}
and lastly my magic Adapter TimeLineMatchEventsRecyclerViewAdapter.java (copied from stackoverflow answer)
public class TimeLineMatchEventsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<MatchTimeLine> mModelList;
public TimeLineMatchEventsRecyclerViewAdapter(List<MatchTimeLine> modelList) {
this.mModelList = modelList;
}
#Override
public int getItemViewType(int position) {
// Just as an example, return 0 or 2 depending on position
// Note that unlike in ListView adapters, types don't have to be contiguous
return position % 2 * 2;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case 0:
LayoutItemTimeLineLeftBinding bindingLeftView = LayoutItemTimeLineLeftBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
return new ViewHolderLeft(bindingLeftView);
case 2:
LayoutItemTimeLineRightBinding bindingRightView = LayoutItemTimeLineRightBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
return new ViewHolderRight(bindingRightView);
default:
LayoutItemTimeLineRightBinding bindingRightDefault = LayoutItemTimeLineRightBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
return new ViewHolderRight(bindingRightDefault);
}
}
#Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, #SuppressLint("RecyclerView") int position) {
// final Team team = mModelList.get(position);
switch (holder.getItemViewType()) {
case 0:
ViewHolderLeft viewHolderLeft = (ViewHolderLeft) holder;
break;
case 2:
ViewHolderRight viewHolderRight = (ViewHolderRight) holder;
break;
}
}
#Override
public int getItemCount() {
// return mModelList == null ? 0 : mModelList.size();
return 20;
}
class ViewHolderLeft extends RecyclerView.ViewHolder {
LayoutItemTimeLineLeftBinding binding;
public ViewHolderLeft(LayoutItemTimeLineLeftBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
}
class ViewHolderRight extends RecyclerView.ViewHolder {
LayoutItemTimeLineRightBinding binding;
public ViewHolderRight(LayoutItemTimeLineRightBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
}
}
Here is the output I got

TableRow in TableLayout and TableRow from RecyclerView aren’t align

I believe TableRow from activity_main and TableRow from recycler_item_header_row have the same values of fields. But they are not aligned! They are shown like this:
enter image description here
enter image description here
Why the positions of textViews in rows from the activity_main and from the RecyclerView aren’t aligned?
activity_main structure:
<TableLayout>
<TableRow ><\TableRow>
<RecyclerView> <\RecyclerView>
<\Table Layout>
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1">
<TextView
android:layout_column="0"
android:layout_gravity="center"
android:layout_weight="0.3"
android:width="0dp"
android:gravity="center"
android:text="ID"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:layout_column="1"
android:layout_gravity="center"
android:layout_weight="0.4"
android:width="0dp"
android:gravity="center"
android:text="Name"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:layout_column="2"
android:layout_gravity="center"
android:layout_weight="0.3"
android:width="0dp"
android:gravity="center"
android:text="Payment"
android:textSize="16dp"
android:textStyle="bold" />
</TableRow>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.recyclerview.widget.RecyclerView>
</TableLayout>
Two types of item for Recycler View:
recycler_item_header_row:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1">
<TextView
android:layout_column="0"
android:layout_gravity="center"
android:layout_weight="0.3"
android:width="0dp"
android:gravity="center"
android:text="ID"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:layout_column="1"
android:layout_gravity="center"
android:layout_weight="0.4"
android:width="0dp"
android:gravity="center"
android:text="Name"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:layout_column="2"
android:layout_gravity="center"
android:layout_weight="0.3"
android:width="0dp"
android:gravity="center"
android:text="Payment"
android:textSize="16dp"
android:textStyle="bold" />
</TableRow>
recycler_item_regular_row:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
>
<TextView
android:id="#+id/recycler_item_regular_cell_ID"
android:layout_column="0"
android:layout_weight="0.3"
android:width="0dp"
android:gravity="center"
android:text="ID"
android:textSize="16dp" />
<TextView
android:id="#+id/cell_name"
android:layout_column="1"
android:layout_weight="0.4"
android:width="0dp"
android:gravity="center"
android:text="Name"
android:textSize="16dp" />
<TextView
android:id="#+id/cell_payment"
android:layout_column="2"
android:layout_weight="0.3"
android:width="0dp"
android:gravity="center"
android:text="Payment"
android:textSize="16dp" />
</TableRow>
MainActivity
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private ArrayList<PaymentModel> paymentData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
paymentData = new ArrayList<>();
add10TestItems(paymentData);
recyclerView = findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(this, paymentData);
recyclerView.setAdapter(adapter);
}
private void add10TestItems(ArrayList<PaymentModel> paymentData) {
for (int i = 0; i < 10; i++) {
paymentData.add(new PaymentModel("A" + i, "Name",
String.valueOf(5 * i)));
}
paymentData.add(new PaymentModel("IDDDDDDDDDDDDDDDDDD", "dddddddddddddddd", "dsfdfdf"));
paymentData.add(new PaymentModel("ID", "Name", "Payment"));
}
}
Adapter:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public static final int HEADER_ROW_TYPE = 0;
public static final int REGULAR_ROW_TYPE = 1;
private Context context;
private List<PaymentModel> paymentModelList;
public RecyclerViewAdapter(Context context, List<PaymentModel> paymentModelList) {
this.context = context;
this.paymentModelList = paymentModelList;
}
#Override
public int getItemViewType(int position) {
if (0 == position) {
return HEADER_ROW_TYPE;
} else {
return REGULAR_ROW_TYPE;
}
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view;
RecyclerView.ViewHolder viewHolder;
if (viewType == HEADER_ROW_TYPE) {
view = LayoutInflater.from(context).inflate(R.layout.recycler_item_header_row,
parent, false);
viewHolder = new ViewHolderHeaderRow(view);
} else {
view = LayoutInflater.from(context).inflate(R.layout.recycler_item_regular_row,
parent, false);
viewHolder = new ViewHolderRegularRow(view);
}
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
if (position == 0) {
ViewHolderHeaderRow headerAdapter = (ViewHolderHeaderRow) holder;
} else {
ViewHolderRegularRow regularAdapter = (ViewHolderRegularRow) holder;
regularAdapter.setData(paymentModelList.get(position - 1));
}
}
#Override
public int getItemCount() {
return (paymentModelList.size() + 1);
}
public class ViewHolderRegularRow extends RecyclerView.ViewHolder {
private TextView cellID;
private TextView cellName;
private TextView cellPayment;
public ViewHolderRegularRow(#NonNull View itemView) {
super(itemView);
cellID = itemView.findViewById(R.id.recycler_item_regular_cell_ID);
cellName = itemView.findViewById(R.id.cell_name);
cellPayment = itemView.findViewById(R.id.cell_payment);
}
public void setData(PaymentModel paymentModel) {
cellID.setText(paymentModel.getId());
cellName.setText(paymentModel.getName());
cellPayment.setText(paymentModel.getPayment());
}
}
public class ViewHolderHeaderRow extends RecyclerView.ViewHolder {
public ViewHolderHeaderRow(#NonNull View itemView) {
super(itemView);
}
}
}
Model:
public class PaymentModel {
private String id;
private String name;
private String payment;
public PaymentModel(String id, String name, String payment) {
this.id = id;
this.name = name;
this.payment = payment;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getPayment() {
return payment;
}
}
I would not use TableLayout and TableRow as TableLayout extra logic to resize any child that is a TableRow BUT as you only have one TableRow as a child of the TableLayout that logic is redundant and is less efficient.
The TableRow's that are inside the RecyclerView are the children of the RecyclerView not children of the TableLayout and as the Docs says
If a TableRow's parent is not a TableLayout, the TableRow will behave as an horizontal LinearLayout
And thus won't do any of their normal resizing of the table columns
So in practice you have actually got a structure like:-
<TableLayout>
<TableRow ><\TableRow>
<\TableLayout>
<RecyclerView>
<\LinearLayout>
<\LinearLayout>
.....
<\RecyclerView>
So as the layout weights are based on content and how much each is allowed to grow if need to, the long content RecyclerView rows grow the size of the long content cells differently to the items in the unrelated TableLayout Rows which don't need to grow.
I've designed a similar type layout but used a ConstraintLayout with a layout_constraintWidth_percent value to achieve wider Name column you are trying to achieve with the weights. This works as this is based on the percentage of the fixed width of the parent NOT the variable width of the content and thus the items in the RecyclerView behave the same are the Header line as they both have the same parent size.
Below are the new layout files need
activity_main
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/HeaderID"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="ID"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="#+id/HeaderName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.3"/>
<TextView
android:id="#+id/HeaderName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Name"
android:textSize="16dp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="#+id/HeaderID"
app:layout_constraintEnd_toStartOf="#+id/HeaderPayment"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="#+id/HeaderID"
app:layout_constraintWidth_percent="0.4"/>
<TextView
android:id="#+id/HeaderPayment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Payment"
android:textSize="16dp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="#+id/HeaderName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="#+id/HeaderID"
app:layout_constraintWidth_percent="0.3"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/HeaderID"/>
</androidx.constraintlayout.widget.ConstraintLayout>
recycler_item_header_row
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/HeaderRowID"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="ID"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="#+id/HeaderRowName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.3"/>
<TextView
android:id="#+id/HeaderRowName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Name"
android:textSize="16dp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="#+id/HeaderRowID"
app:layout_constraintEnd_toStartOf="#+id/HeaderRowPayment"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="#+id/HeaderRowID"
app:layout_constraintWidth_percent="0.4"/>
<TextView
android:id="#+id/HeaderRowPayment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Payment"
android:textSize="16dp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="#+id/HeaderRowName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="#+id/HeaderRowID"
app:layout_constraintWidth_percent="0.3"/>
</androidx.constraintlayout.widget.ConstraintLayout>
recycler_item_regular_row
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/recycler_item_regular_cell_ID"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="16dp"
app:layout_constraintEnd_toStartOf="#+id/cell_name"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.3"
tools:text="ID" />
<TextView
android:id="#+id/cell_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="16dp"
app:layout_constraintStart_toEndOf="#+id/recycler_item_regular_cell_ID"
app:layout_constraintEnd_toStartOf="#+id/cell_payment"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="#+id/recycler_item_regular_cell_ID"
app:layout_constraintWidth_percent="0.4"
tools:text="Name" />
<TextView
android:id="#+id/cell_payment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="16dp"
app:layout_constraintBottom_toBottomOf="#+id/recycler_item_regular_cell_ID"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/cell_name"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.3"
tools:text="Payment" />
</androidx.constraintlayout.widget.ConstraintLayout>
This produces the following result with all the columns lined up.

Constraintlayout does not behave well as recyclerview 's item

I have a recyclerview whose items look like this:
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_food_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="4dp"
app:cardCornerRadius="4dp">
<LinearLayout
android:id="#+id/linear_food_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="start"
android:orientation="horizontal"
android:padding="4dp">
<com.mikhaellopez.circleview.CircleView
android:id="#+id/food_image"
android:layout_width="80dp"
android:layout_height="80dp"
app:cv_color="#color/colorAccent" />
<LinearLayout
android:id="#+id/food_contents"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:orientation="vertical">
<TextView
android:id="#+id/food_title_homepage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:fontFamily="#font/sans_regular"
android:text="عنوان غذا"
android:textAppearance="#style/TextAppearance.AppCompat.Title" />
<TextView
android:id="#+id/ingredients_homepage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:fontFamily="#font/sans_regular"
android:text="مواد اولیه"
android:textAppearance="#style/TextAppearance.MaterialComponents.Subtitle1" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
I'm trying to convert this into a flat hierarchy using Constraintlayout.
Here's how it looks:
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_food_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="4dp"
app:cardCornerRadius="4dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/linear_food_item"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/food_title_homepage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"
android:text="عنوان غذا"
android:textAppearance="#style/TextAppearance.AppCompat.Title"
app:layout_constraintStart_toEndOf="#+id/food_image"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/ingredients_homepage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="مواد اولیه"
android:textAppearance="#style/TextAppearance.MaterialComponents.Subtitle1"
app:layout_constraintStart_toEndOf="#+id/food_image"
app:layout_constraintTop_toBottomOf="#+id/food_title_homepage" />
<com.mikhaellopez.circleview.CircleView
android:id="#+id/food_image"
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
app:cv_color="#color/colorAccent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Here's where my Recyclerview is inserted:
<androidx.constraintlayout.widget.ConstraintLayout 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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".main.homepage.HomepageFragment">
<ProgressBar
android:id="#+id/homepage_progressbar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recipes"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:scrollbars="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/homepage_toolbar"
tools:listitem="#layout/fragment_homepage_food_item" />
<include
android:id="#+id/homepage_toolbar"
layout="#layout/fragment_homepage_toolbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Here's how each item should be: https://i.stack.imgur.com/AcfAm.png
But I don't get the same results in the Recyclerview: https://i.stack.imgur.com/jpUG4.png
I didn't have the problem before I converted the view into Constraintlayout!! Everything was working just fine until now that I converted them. They all become left-aligned in the Recyclerview!!
Does anyone know what the problem is?? Thanks in advance
-- Updated --
How it looks like on an android emulator or any other devices: https://i.stack.imgur.com/tvGz4.png
When I scroll down to the bottom (I update the list after each scroll) some items become right-aligned (they somehow get fixed) but the rest stays the same (still left aligned)
-- Java Code --
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
viewModel = new ViewModelProvider(this).get(HomepageViewModel.class);
getData(0);
setRecyclerView(linearLayoutManager);
EndlessRecyclerViewScrollListener scrollListener = new EndlessRecyclerViewScrollListener(linearLayoutManager) {
#Override
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
getData(page);
}
};
recyclerView.addOnScrollListener(scrollListener);
private void getData(int offset) {
viewModel.init(this, offset);
viewModel.getFoodsLiveData().observe(getViewLifecycleOwner(), foodList -> {
if (foodList != null) {
foods.addAll(foodList);
foodList.clear();
foodAdapter.notifyDataSetChanged();
}
});
}
private void setRecyclerView(LinearLayoutManager linearLayoutManager) {
foodAdapter = new FoodAdapter(foods);
recyclerView.setAdapter(foodAdapter);
recyclerView.setLayoutManager(linearLayoutManager);
}
My adapter:
private void getData(int offset) {
viewModel.init(this, offset);
viewModel.getFoodsLiveData().observe(getViewLifecycleOwner(), foodList -> {
if (foodList != null) {
foods.addAll(foodList);
foodList.clear();
foodAdapter.notifyDataSetChanged();
}
});
}
private void setRecyclerView(LinearLayoutManager linearLayoutManager) {
foodAdapter = new FoodAdapter(foods);
recyclerView.setAdapter(foodAdapter);
recyclerView.setLayoutManager(linearLayoutManager);
}
My adapter:
private List<Food> foods;
FoodAdapter(List<Food> foods) {
this.foods = foods;
}
static class FoodViewHolder extends RecyclerView.ViewHolder {
CircleView foodImage;
TextView foodTitle, ingredients;
CardView foodItem;
FoodViewHolder(View itemView) {
super(itemView);
foodImage = itemView.findViewById(R.id.food_image);
foodTitle = itemView.findViewById(R.id.food_title_homepage);
ingredients = itemView.findViewById(R.id.ingredients_homepage);
foodItem = itemView.findViewById(R.id.card_food_item);
}
}
#NonNull
#Override
public FoodViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_homepage_food_item, parent, false);
return new FoodViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull FoodViewHolder holder, int position) {
final Food currentFood = foods.get(position);
StringBuilder food_ingredient_str = new StringBuilder();
int ingredient_position = 0;
int ingredients_size = currentFood.getFood_ingredients().size();
for (Food.FoodIngredient foodIngredient : currentFood.getFood_ingredients()) {
food_ingredient_str.append(foodIngredient.getIngredient());
if (ingredient_position != ingredients_size / 3)
food_ingredient_str.append(", ");
if (ingredient_position == ingredients_size / 3) {
food_ingredient_str.append(" و ...");
break;
}
ingredient_position += 1;
}
holder.foodTitle.setText(currentFood.getName());
holder.ingredients.setText(food_ingredient_str);
holder.foodItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Bundle bundle = new Bundle();
bundle.putSerializable("current_food", currentFood);
Navigation.findNavController(v).navigate(R.id.action_navigation_home_to_navigation_food, bundle);
}
});
}
#Override
public int getItemCount() {
return foods.size();
}
If you want to create a list using recyclerview and each item to be right aligned as in Arabic.. then try these approaches
replace your "recycle_view_item_layout.xml" with this
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_food_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="4dp"
app:cardCornerRadius="4dp">
<LinearLayout
android:id="#+id/linear_food_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="4dp">
<LinearLayout
android:id="#+id/food_contents"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/food_title_homepage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:ellipsize="start"
android:fontFamily="#font/sans_regular"
android:singleLine="true"
android:text="عنوان غذا"
android:textAppearance="#style/TextAppearance.AppCompat.Title" />
<TextView
android:id="#+id/ingredients_homepage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:ellipsize="start"
android:fontFamily="#font/sans_regular"
android:singleLine="true"
android:text="مواد اولیه"
android:textAppearance="#style/TextAppearance.MaterialComponents.Subtitle1" />
</LinearLayout>
<com.mikhaellopez.circleview.CircleView
android:id="#+id/food_image"
android:layout_width="80dp"
android:layout_height="80dp"
app:cv_color="#color/colorAccent" />
</LinearLayout>
</androidx.cardview.widget.CardView>
replace your "recycle view" with this
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recipes"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#id/homepage_toolbar"
tools:listitem="#layout/fragment_homepage_food_item" />
and attach a layout manager like this (important)
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
setRecyclerView(layoutManager);
and rest of your code seems okay

How to load images in recyclerview without affecting defined size in imageview?

I am working with a Webservice from which I am fetching the images using retrofit.
I am showing those images in imageView and used a recyclerview to load multiple images.
Here are my layout files:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="#+id/planscontainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/subscplans"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Button
android:id="#+id/backplans"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="50dp"
android:background="#drawable/leftarrow" />
<ImageView
android:id="#+id/appiconhere"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="400dp"
android:src="#drawable/moremovelogo" />
</android.support.v7.widget.Toolbar>
<TextView
android:id="#+id/planname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginTop="41dp"
android:layout_marginEnd="319dp"
android:text="Plan Id:"
app:layout_constraintEnd_toStartOf="#+id/textView3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/subscplans" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="324dp"
android:layout_marginTop="41dp"
android:layout_marginBottom="28dp"
android:text="1"
app:layout_constraintBottom_toTopOf="#+id/textView11"
app:layout_constraintStart_toEndOf="#+id/planname"
app:layout_constraintTop_toBottomOf="#+id/subscplans"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginTop="28dp"
android:text="Plan Name:"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/planname" />
<TextView
android:id="#+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginTop="20dp"
android:text="Price:"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/duration" />
<TextView
android:id="#+id/noofmoviesdownload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginTop="18dp"
android:layout_marginEnd="191dp"
android:text="No. of movies can be download:"
app:layout_constraintEnd_toStartOf="#+id/textView13"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/price" />
<TextView
android:id="#+id/canbeviewed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="213dp"
android:text="No. of movies can be viewed"
app:layout_constraintEnd_toStartOf="#+id/textView14"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/noofmoviesdownload" />
<TextView
android:id="#+id/samemovieviewed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="143dp"
android:text="No. of times same movie can be viewed"
app:layout_constraintEnd_toStartOf="#+id/textView15"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/canbeviewed" />
<TextView
android:id="#+id/timesdownload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginTop="17dp"
android:layout_marginEnd="126dp"
android:text="No. of times same movie can be download"
app:layout_constraintEnd_toStartOf="#+id/textView16"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/samemovieviewed" />
<TextView
android:id="#+id/rentpe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="26dp"
android:layout_marginTop="18dp"
android:layout_marginEnd="119dp"
android:text="No. of times same movie can be get on rent"
app:layout_constraintEnd_toStartOf="#+id/textView17"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/timesdownload" />
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="310dp"
android:layout_marginTop="28dp"
android:text="TextView"
app:layout_constraintStart_toEndOf="#+id/duration"
app:layout_constraintTop_toBottomOf="#+id/textView3" />
<TextView
android:id="#+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="362dp"
android:layout_marginTop="19dp"
android:text="TextView"
app:layout_constraintStart_toEndOf="#+id/price"
app:layout_constraintTop_toBottomOf="#+id/textView11" />
<TextView
android:id="#+id/textView14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="194dp"
android:layout_marginTop="19dp"
android:text="TextView"
app:layout_constraintStart_toEndOf="#+id/noofmoviesdownload"
app:layout_constraintTop_toBottomOf="#+id/textView13" />
<TextView
android:id="#+id/textView15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="216dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="480dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/canbeviewed"
app:layout_constraintTop_toBottomOf="#+id/textView14" />
<TextView
android:id="#+id/textView16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="146dp"
android:layout_marginTop="25dp"
android:text="TextView"
app:layout_constraintStart_toEndOf="#+id/samemovieviewed"
app:layout_constraintTop_toBottomOf="#+id/textView15" />
<TextView
android:id="#+id/textView17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="130dp"
android:layout_marginTop="16dp"
android:text="TextView"
app:layout_constraintStart_toEndOf="#+id/timesdownload"
app:layout_constraintTop_toBottomOf="#+id/textView16" />
<Button
android:id="#+id/buyplan"
android:layout_width="194dp"
android:layout_height="wrap_content"
android:layout_marginStart="227dp"
android:layout_marginTop="106dp"
android:layout_marginEnd="59dp"
android:background="#color/tw__composer_blue"
android:text="Buy Plan"
android:textColor="#color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/textView13"
app:layout_constraintTop_toBottomOf="#+id/subscplans" />
<Button
android:id="#+id/showplans"
android:layout_width="194dp"
android:layout_height="wrap_content"
android:layout_marginStart="224dp"
android:layout_marginTop="37dp"
android:layout_marginEnd="62dp"
android:layout_marginBottom="229dp"
android:background="#d156b8"
android:text="Show plans"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/textView15"
app:layout_constraintTop_toBottomOf="#+id/buyplan"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/textView19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="121dp"
android:layout_marginTop="19dp"
android:text="TextView"
app:layout_constraintStart_toEndOf="#+id/rentpe"
app:layout_constraintTop_toBottomOf="#+id/textView17" />
<RelativeLayout
android:id="#+id/linearlayoutimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/textView19">
<android.support.v7.widget.RecyclerView
android:id="#+id/myrecyclerview"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
<Button
android:id="#+id/cancelplan"
android:layout_width="194dp"
android:layout_height="wrap_content"
android:layout_marginStart="221dp"
android:layout_marginTop="37dp"
android:layout_marginEnd="60dp"
android:background="#color/tw__composer_deep_gray"
android:text="cancel plan"
android:textColor="#color/white"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/textView17"
app:layout_constraintTop_toBottomOf="#+id/showplans" />
</android.support.constraint.ConstraintLayout>
and for the items I have:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:id="#+id/imagethumb"
android:layout_width="180dp"
android:layout_height="200dp"
android:scaleType="fitEnd"/>
<TextView
android:id="#+id/nothing"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
And my Adapter class looks like:
this is my adapter class name ImageAdapter.java
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.SubsImageHolder> {
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.newlogo)
.showImageForEmptyUri(R.drawable.newlogo)
.showImageOnFail(R.drawable.newlogo)
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
private Context context;
private List<ThumbPojo> imageList;
public ImageAdapter(Context context, List<ThumbPojo> items) {
this.context = context;
this.imageList = items;
}
public Context getContext() {
return context;
}
public void setContext(Context context) {
this.context = context;
}
public List<ThumbPojo> getImageList() {
return imageList;
}
public void setImageList(List<ThumbPojo> imageList) {
this.imageList = imageList;
}
#NonNull
#Override
public SubsImageHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(R.layout.subscriptionimages, viewGroup, false);
return new SubsImageHolder(view);
}
#Override
public void onBindViewHolder(#NonNull SubsImageHolder subsImageHolder, int i) {
ThumbPojo item = imageList.get(i);
subsImageHolder.textView.setText(item.getMovieName());
subsImageHolder.imageView.setImageDrawable(null);
subsImageHolder.imageView.layout(0,0,0,0);
ImageLoader.getInstance().displayImage(item.getThumbnail(),subsImageHolder.imageView,options);
// Glide.with(context)
// .load(item.getThumbnail())
// .into(subsImageHolder.imageView);
}
#Override
public int getItemCount() {
return imageList.size();
}
class SubsImageHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView textView;
public SubsImageHolder(#NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imagethumb);
textView = itemView.findViewById(R.id.nothing);
}
}
}
And my Main Activity where I am setting my adpater is :
SubscriptionPlans.java:
public class SubscriptionPlans extends AppCompatActivity {
TextView planid, planname, planprice, noofmoviedownload, noofmovieviewed, sameviewed, samedownload, rentit, nomovi;
Intent intent = getIntent();
ImageView imageView3, imageView4;
String one, two, three, four, five, six, seven, eight;
List<ThumbPojo> list;
Button button;
Button show, buy, cancel;
int position;
String allvalues;
RelativeLayout linearLayout;
String id;
int userid, usertype;
int po;
String ress;
public String subs;
List<CheckActiveOrNot> list1;
RecyclerView recyclerView;
ImageAdapter imageAdapter;
List<ThumbPojo> imageList;
List<String> images = new ArrayList<>();
AVLoadingIndicatorView progressBar;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.premiumsubscriptionplans);
// progressBar=findViewById(R.id.progressBar);
// showProgressDialog();
* recyclerView = findViewById(R.id.myrecyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));*
userid = SharedPrefsHelper.getUserId(SharedPrefsHelper.USER_ID, 0);
usertype = SharedPrefsHelper.getUserType(SharedPrefsHelper.USER_TYPE, 0);
linearLayout = findViewById(R.id.linearlayoutimage);
// imageView4 = findViewById(R.id.imageView4);
planid = findViewById(R.id.textView3);
planname = findViewById(R.id.textView11);
planprice = findViewById(R.id.textView13);
noofmoviedownload = findViewById(R.id.textView14);
noofmovieviewed = findViewById(R.id.textView15);
sameviewed = findViewById(R.id.textView16);
samedownload = findViewById(R.id.textView17);
rentit = findViewById(R.id.textView19);
TextView notshow = findViewById(R.id.planname);
notshow.setVisibility(View.GONE);
planid.setVisibility(View.GONE);
button = findViewById(R.id.backplans);
show = findViewById(R.id.showplans);
buy = findViewById(R.id.buyplan);
cancel = findViewById(R.id.cancelplan);
id = getIntent().getStringExtra("planid");
imageList = new ArrayList<>();
Call<List<ThumbPojo>> caller = ApiClient.getInstance().getApiService().getThumbimages(id);
caller.enqueue(new Callback<List<ThumbPojo>>() {
#Override
public void onResponse(Call<List<ThumbPojo>> call, Response<List<ThumbPojo>> response) {
list =response.body();
// JSONArray jsonArray=new JSONArray(list);
// for(int i=0;i<jsonArray.length();i++)
// {
// try {
// JSONObject jsonObject=jsonArray.getJSONObject(i);
// String name=jsonObject.getString("movieName");
// String imageurl=jsonObject.getString("thumbnail");
//
// ThumbPojo thumbPojo=new ThumbPojo(name,imageurl);
// imageList.add(thumbPojo);
// } catch (JSONException e) {
// e.printStackTrace();
// }
//
// }
*imageAdapter=new ImageAdapter(SubscriptionPlans.this,list);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(imageAdapter);
// dismissProgressDialog();*
// if (list.get(position).getThumbnail() != null) {
// ImageLoader.getInstance().displayImage(list.get(position).getThumbnail(), imageView3, options);
// // ImageLoader.getInstance().displayImage(list.get(position).getThumbnail(),imageView4,options);
// } else {
// nomovi.setText("No movies under this plan");
// }
}
#Override
public void onFailure(Call<List<ThumbPojo>> call, Throwable t) {
t.getMessage();
}
});
for image loading purposes I am using universal image loader.
Now coming to the problem, when a single image is coming from the server then it is loaded according to the width and height mentioned in the imageview .but when two or more are coming the images' sizes are different.
I have tried almost all the solution from the internet but nothing works fine.
I have also set the layout to (0,0,0,0) but that also not able to fix this.
Images are looking like this I don't know why?
Change scaleType of ImageView in items to be center_crop, this will make the image of defined size (dps) and will crop the remaining portion off, making all the images of same size.
Like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imagethumb"
android:layout_width="180dp"
android:layout_height="200dp"
android:scaleType="center_crop"/>
<TextView
android:id="#+id/nothing"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Try changing scaleType in Imageview, whichever suits best.
<ImageView
android:id="#+id/imagethumb"
android:layout_width="180dp"
android:layout_height="200dp"
android:scaleType="center_inside"/>
or Try staggeredGridLayout.

NestedScrollView not scrolling in recyclerView generated data

I'm using a NestedScrollView in a CardView that's generated in a RecyclerView. I can't get the scrolling to work - there seems to be sporadic moments when I can scroll through the list in the top item but no other scrolls seem to work. Cheers!
This is the layout of the cardView that's generated in the RecyclerView:
<?xml version="1.0" encoding="utf-8"?
<android.support.constraint.ConstraintLayout 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:id="#+id/checkout_relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/merchantPurchase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginLeft="12dp"
android:layout_marginTop="4dp"
android:text="TextView"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="#+id/checkoutCard"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
android:layout_marginStart="12dp" />
<android.support.v7.widget.CardView
android:id="#+id/checkoutCard"
android:layout_width="340dp"
android:layout_height="90dp"
android:layout_margin="5dp"
android:layout_marginBottom="11dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:elevation="5dp"
app:cardCornerRadius="5dp"
app:contentPadding="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_goneMarginLeft="5dp"
app:layout_goneMarginRight="5dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingRight="5dp">
<TextView
android:id="#+id/textCentre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:text="Test2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.7"
app:layout_constraintLeft_toLeftOf="#+id/extraContainer"
app:layout_constraintRight_toRightOf="#+id/extraContainer" />
<TextView
android:id="#+id/textRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:text="Test3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.533"
app:layout_constraintLeft_toLeftOf="#+id/otherContainer"
app:layout_constraintRight_toRightOf="#+id/otherContainer" />
<TextView
android:id="#+id/textLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:text="Test1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.444"
app:layout_constraintLeft_toLeftOf="#+id/priceContainer"
app:layout_constraintRight_toRightOf="#+id/priceContainer" />
<TextView
android:id="#+id/extraContainer"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
android:clickable="true"
android:scaleType="centerCrop"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="#+id/priceContainer"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.17" />
<TextView
android:id="#+id/priceContainer"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
android:clickable="true"
android:scaleType="centerCrop"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="#+id/itemTitle"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.17" />
<TextView
android:id="#+id/otherContainer"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
android:clickable="true"
android:scaleType="centerCrop"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="#+id/extraContainer"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.17" />
<TextView
android:id="#+id/itemTitle"
android:layout_width="71dp"
android:layout_height="39dp"
android:layout_marginLeft="4dp"
android:layout_marginStart="4dp"
android:text="TextView"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.512" />
<LinearLayout
android:layout_width="63dp"
android:layout_height="53dp"
android:layout_marginLeft="0dp"
android:orientation="vertical"
app:layout_constraintLeft_toRightOf="#+id/otherContainer"
tools:layout_editor_absoluteY="19dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
tools:layout_editor_absoluteX="258dp">
<android.support.v4.widget.NestedScrollView
android:id="#+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:layout_gravity="fill_vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView45"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/textView44"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/textView43"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
Then, my abridged code for MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.checkout_home);
cardPurchasesDataSet = new ArrayList<>();
for (int i = 0; i < productsForPurchase.length; i++) {
cardPurchasesDataSet.add(productsForPurchase[i]);
}
card_totalPrice = new ArrayList<>();
for (int i = 0; i < totalPriceData.length; i++) {
card_totalPrice.add(totalPriceData[i]);
}
card_extras = new ArrayList<>();
for (int i = 0; i < extrasData.length; i++) {
card_extras.add(extrasData[i]);
}
card_more = new ArrayList<>();
for (int i = 0; i < moreData.length; i++) {
card_more.add(moreData[i]);
}
merchants = new ArrayList<>();
for (int i = 0; i < merchantsData.length; i++) {
merchants.add(merchantsData[i]);
}
card_recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
card_layoutManger = new LinearLayoutManager(this);
card_recyclerView.setLayoutManager(card_layoutManger);
card_adapter = new checkout_card_Adapter(cardPurchasesDataSet, card_totalPrice, card_extras, card_more, merchants);
card_recyclerView.setAdapter(card_adapter);
}
UPDATE 01: Added mainActivity layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="368dp"
android:layout_height="551dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:overScrollMode="never"
android:layout_marginBottom="8dp" />
Adapter
public class Checkout_Card_Adapter extends
RecyclerView.Adapter<Checkout_Card_Adapter.ViewHolder>{
private ArrayList<String> titleOfPurchase;
private ArrayList<String> priceOfPurchase;
private ArrayList<String> extrasOfPurchase;
private ArrayList<String> moreOfPurchase;
private ArrayList<String> merchantOfPurchase;
public Checkout_Card_Adapter(ArrayList<String> titleOfPurchase, ArrayList<String> priceOfPurchase, ArrayList<String> extrasOfPurchase, ArrayList<String> moreOfPurchase, ArrayList<String> merchantOfPurchase) {
this.titleOfPurchase = titleOfPurchase;
this.priceOfPurchase = priceOfPurchase;
this.extrasOfPurchase = extrasOfPurchase;
this.moreOfPurchase = moreOfPurchase;
this.merchantOfPurchase = merchantOfPurchase;
}
public interface VenueAdapterInterface {
void onVenueButtonClick(int position);
}
#Override
public Checkout_Card_Adapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.checkout_scrollable_card, viewGroup, false);
Checkout_Card_Adapter.ViewHolder viewHolder = new Checkout_Card_Adapter.ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(Checkout_Card_Adapter.ViewHolder viewHolder, final int position) {
viewHolder.itemTitle.setText(titleOfPurchase.get(position));
viewHolder.itemPrice.setText(priceOfPurchase.get(position));
viewHolder.itemExtras.setText(extrasOfPurchase.get(position));
viewHolder.itemMore.setText(moreOfPurchase.get(position));
viewHolder.merchant.setText(merchantOfPurchase.get(position));
}
#Override
public int getItemCount() {
return titleOfPurchase.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ImageButton itemImage;
public TextView itemTitle;
public TextView itemPrice;
public TextView itemExtras;
public TextView itemMore;
public TextView merchant;
public ViewHolder(final View itemView) {
super(itemView);
itemTitle = (TextView) itemView.findViewById(R.id.itemTitle);
itemPrice = (TextView) itemView.findViewById(R.id.priceContainer);
itemExtras = (TextView) itemView.findViewById(R.id.extraContainer);
itemMore = (TextView) itemView.findViewById(R.id.otherContainer);
merchant = (TextView) itemView.findViewById(R.id.merchantPurchase);
}
}
}
As per your screen shot how do you expect it to scroll the inner view while it showing all the views? it will only scroll on small devices where your all textview won't be able to display. Can you please post a proper screenshot of this view from app.

Categories

Resources