Constraint layout with child constraint layouts not expanding horizontally - android

I'm trying to build a rectangular view (spanning the width of the screen) which is split horizontally into three blocks, and I am doing so using a constraint layout. The left and right blocks have a specified width, but the centre block should expand to fit the remaining space. Because I require that the left-most block have a different background colour to the parent, I have grouped its children into another constraint layout. I have given the layout XML below:
<?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="100dp"
android:layout_marginTop="8dp"
android:background="#10000000"
xmlns:tools="http://schemas.android.com/tools">
<android.support.constraint.ConstraintLayout
android:id="#+id/abc"
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#50000000"
android:layout_marginEnd="8dp"
android:padding="2dp"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/jkl"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/def"
android:gravity="start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffffff"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/ghi"
android:gravity="start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffffff"
android:textSize="14sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/def" />
</android.support.constraint.ConstraintLayout>
<!-- This should expand to fit the remainder of the screen width -->
<android.support.constraint.ConstraintLayout
android:id="#+id/jkl"
android:layout_width="0dp"
android:layout_height="match_parent"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintEnd_toStartOf="#+id/stu"
app:layout_constraintStart_toEndOf="#+id/abc">
<TextView
android:id="#+id/mno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|start"
android:textColor="#a0000000"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/pqr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|start"
android:textColor="#a0000000"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/mno" />
</android.support.constraint.ConstraintLayout>
<ImageButton
android:id="#+id/stu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:src="#drawable/x"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/jkl"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Unfortunately, the middle block does not expand to fit the remaining width of the screen. Does anyone know how to achieve this? Am I doing anything fundamentally wrong here?
Edit:
Based on Ben P.'s response it may be that the XML above is fine, but the layouts in which this layout is contained are at fault. I have therefore provided them below.
This layout XML is the parent of the XML above:
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/parent_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.Parent">
</android.support.v7.widget.RecyclerView>
This layout XML is the parent of the RecyclerView XML above:
<?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"
tools:context="com.example.MainActivity">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<include layout="#layout/bottom_navigation" />
</android.support.constraint.ConstraintLayout>
Edit 2:
As requested I've provided the complete code base:
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private FragmentManager mFragmentManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, new Foo());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
Foo.java:
public class Foo extends Fragment {
private String[] mDataset;
private OnFragmentInteractionListener mListener;
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
public Foo() {
// 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 Foo.
*/
// TODO: Rename and change types and number of parameters
public static Foo newInstance(String param1, String param2) {
Foo fragment = new Foo();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
initDataset();
}
private void initDataset() {
mDataset = new String[1];
mDataset[0] = "Hello World";
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_foo, container, false);
mRecyclerView = view.findViewById(R.id.foo_view);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new FooAdapter(mDataset);
mRecyclerView.setAdapter(mAdapter);
return view;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
FooAdapter.java:
public class FooAdapter extends RecyclerView.Adapter<FooAdapter.ViewHolder> {
private String[] mDataset;
public static class ViewHolder extends RecyclerView.ViewHolder {
private TextView mFoofoo;
private TextView mBarbar;
public ViewHolder(View itemView) {
super(itemView);
mFoofoo = itemView.findViewById(R.id.foofoo);
mBarbar = itemView.findViewById(R.id.barbar);
}
public TextView getFoofoo() {
return mFoo;
}
public TextView getBarbar() {
return mBar;
}
}
public FooAdapter(String[] mDataset) {
this.mDataset = mDataset;
}
#Override
public FooAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.bar, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(FooAdapter.ViewHolder holder, int position) {
String text = mDataset[position];
holder.getBarbar().setText(text);
holder.getFoofoo().setText(text);
}
#Override
public int getItemCount() {
return mDataset.length;
}
}
activity_main.xml:
<?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"
tools:context="com.example.MainActivity">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eaff73"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
fragment.xml:
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/foo_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#8df78b"
tools:context="com.example.Foo">
</android.support.v7.widget.RecyclerView>
bar.xml:
<?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="104dp"
android:layout_marginTop="4dp"
android:background="#10000000"
xmlns:tools="http://schemas.android.com/tools">
<ImageView
android:id="#+id/backg"
android:background="#50000000"
android:layout_width="104dp"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/foofoo"
android:gravity="start"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:textColor="#ffffffff"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="#+id/action"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_width="wrap_content"
android:src="#drawable/pic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/barbar"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/barbar"
android:autoSizeMaxTextSize="24sp"
android:autoSizeMinTextSize="14dp"
android:autoSizeStepGranularity="2dp"
android:autoSizeTextType="uniform"
android:background="#ef0505"
android:gravity="top|start"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingStart="8dp"
android:textColor="#a0000000"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="#id/action"
app:layout_constraintStart_toEndOf="#id/backg"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Any help is greatly appreciated!

I do not know why this works, but it does.
In bar.xml, replace your outer ConstraintLayout's height with wrap_content (instead of 104dp).
Also in bar.xml, replace your ImageView's height with 104dp (instead of 0dp).
This has the net effect of keeping your layout's design the same... it just has the ConstraintLayout's height defined by the ImageView's height, instead of the other way around.
If I had to guess at why this makes a difference, I'd assume that there's some quirk in the way LinearLayoutManager measures its children, and that when it sees a fixed height it ignores the match_parent width. But that's wild speculation.

Update
Here is a flat ConstraintLayout that does what I believe you want:
<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="100dp">
<ImageView
android:id="#+id/image"
android:layout_width="100dp"
android:layout_height="0dp"
android:background="#33000000"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
<TextView
android:id="#+id/text1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="4dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="#+id/image"
app:layout_constraintRight_toRightOf="#+id/image"
tools:text="line 1"/>
<TextView
android:id="#+id/text2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="4dp"
app:layout_constraintTop_toBottomOf="#+id/text1"
app:layout_constraintLeft_toLeftOf="#+id/image"
app:layout_constraintRight_toRightOf="#+id/image"
tools:text="line 2"/>
<TextView
android:id="#+id/text3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="4dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="#+id/image"
app:layout_constraintRight_toLeftOf="#+id/button"
tools:text="line 3"/>
<TextView
android:id="#+id/text4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="4dp"
app:layout_constraintTop_toBottomOf="#+id/text3"
app:layout_constraintLeft_toRightOf="#+id/image"
app:layout_constraintRight_toLeftOf="#+id/button"
tools:text="line 4"/>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
tools:text="button"/>
</android.support.constraint.ConstraintLayout>
And what it looks like:
Original
This does not strictly answer the question (since as far as I can tell, there is no problem with the posted layout), but...
I see no reason to implement this UI using ConstraintLayout. The benefit of ConstraintLayout is that it allows you to avoid nesting ViewGroups inside your top-level View. But as your ConstraintLayout is written, you're still grouping your TextViews inside a mid-level View.
If you are not taking advantage of ConstraintLayout's ability to do organization without nesting, I think it is a better idea to use a simpler top-level view. Here is your UI reimplemented with LinearLayouts:
<?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="match_parent"
android:layout_height="100dp"
android:layout_marginTop="8dp"
android:background="#10000000"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/abc"
android:layout_width="100dp"
android:layout_height="match_parent"
android:padding="2dp"
android:background="#50000000"
android:orientation="vertical">
<TextView
android:id="#+id/def"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffffff"
android:textSize="18sp"
tools:text="line 1"/>
<TextView
android:id="#+id/ghi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffffff"
android:textSize="14sp"
tools:text="line 2"/>
</LinearLayout>
<LinearLayout
android:id="#+id/jkl"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/mno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#a0000000"
android:textSize="14sp"
android:textStyle="bold"
tools:text="line 1"/>
<TextView
android:id="#+id/pqr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#a0000000"
android:textSize="14sp"
tools:text="line 2"/>
</LinearLayout>
<ImageButton
android:id="#+id/stu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp"
android:src="#drawable/plus"/>
</LinearLayout>

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.

View is not added when using addView()

I am making a window to open an empty dialog and write comments.
The dialog initially has an empty item where i can write one comment.
Comments can be up to one line.
Pressing the Enter key dynamically creates an item that allows you to write the next comment.
I was wondering whether to use RecyclerView or addView() for this function.
Since this dialog was also opened from the recycler view item of the parent, it would be complicated when using RecyclerView again, so I used addView().
I don't know if this is the right choice.
This is because data management seems to be difficult because the created comment must be saved in the parent's recycler view item.
Anyway, using addView() does not add the view.
There is no such thing as an error, but it cannot be added.
What is the cause?
writeing_comment_item
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".fragment.WritingCommentDialogFragment">
<TextView
android:id="#+id/start_point"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" * "
android:textSize="15sp"
android:textStyle="bold"
android:gravity="center"
android:layout_marginTop="15dp"
android:layout_marginRight="2dp"
android:layout_marginLeft="10dp"
app:layout_constraintVertical_chainStyle="spread"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="#+id/comment_edit" />
<EditText
android:id="#+id/comment_edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginRight="10dp"
android:background="#null"
android:textSize="15sp"
android:inputType="text"
android:maxLines="1"
android:maxLength="22"
app:layout_constraintLeft_toRightOf="#+id/start_point"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_writing_comment_dialog
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".fragment.WritingCommentDialogFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<TextView
android:id="#+id/start_point"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" * "
android:textSize="15sp"
android:textStyle="bold"
android:gravity="center"
android:layout_marginTop="15dp"
android:layout_marginRight="2dp"
android:layout_marginLeft="10dp" />
<EditText
android:id="#+id/comment_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginRight="10dp"
android:background="#null"
android:textSize="15sp"
android:inputType="text"
android:maxLines="1"
android:maxLength="22" />
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
WritingCommentDialogFragment.java
public class WritingCommentDialogFragment extends DialogFragment {
LinearLayout mContainer;
EditText editText;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_writing_comment_dialog, container, false);
editText = view.findViewById(R.id.comment_edit);
mContainer = view.findViewById(R.id.container);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override // IME actionNone (Enter Key)
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
View inflater1 = LayoutInflater.from(getContext()).inflate(R.layout.writing_comment_item, mContainer, false);
mContainer.addView(inflater1);
return true;
}
});
return view;
}
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setCanceledOnTouchOutside(false);
return dialog;
}
#Override
public void onResume() {
super.onResume();
setDialogSize();
}
private void setDialogSize() {
getDialog().getWindow().setLayout(1000, 1000);
}
}
your problem is that you added a horizontal LinearLayout and after you added an EditText with android: layout_width = "match_parent" that's why your Comment is added on your container view but is not visible because of the EditText.
if I understood correctly you must change
android: orientation = "horizontal" by android: orientation = "vertical" in your fragment_writing_comment_dialog
so that the commentary will be displayed above the EditText
And also you need to change the orientation on your Container LinearLayout
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".fragment.WritingCommentDialogFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<TextView
android:id="#+id/start_point"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" * "
android:textSize="15sp"
android:textStyle="bold"
android:gravity="center"
android:layout_marginTop="15dp"
android:layout_marginRight="2dp"
android:layout_marginLeft="10dp" />
<EditText
android:id="#+id/comment_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginRight="10dp"
android:background="#null"
android:textSize="15sp"
android:inputType="text"
android:maxLines="1"
android:maxLength="22" />
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

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 not repeat recycle view inside nested layout?

I am trying to create a nested recycle view. So far i have a parent recycle view and under this i have a child recycle view. The problem that i am having is my child recycle view continues to be recycled with my parent recycle view. I do not want this i would like the child recycle view to only be recycled once. Does any one know how to go about fixing this. I understand this might sound a little confusing, but an example of what i want is instagram news feed. The top of the app have circle image views with your followers stories which can be swiped and the bottom have the news feed itself, in my case my app loads the user stories continuously under the news feed as i continue to scroll users stories are being continuously recycled under the news feed post.
Main UI with parent recycleView
<?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"
android:background="#FFFFFF">
<androidx.appcompat.widget.Toolbar
android:id="#+id/newsFeedToolBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginTop="12dp"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="#+id/backButton"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_gravity="left"
android:background="#drawable/ic_arrow_back_black_24dp"
android:onClick="backImageView"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="15dp" />
</androidx.appcompat.widget.Toolbar>
<SearchView
android:id="#+id/searchView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_margin="6dp"
android:background="#FFFFFF"
android:iconifiedByDefault="false"
android:queryHint="Search"
app:layout_constraintBottom_toBottomOf="#+id/newsFeedToolBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#+id/newsFeedToolBar"
app:layout_constraintTop_toTopOf="#+id/newsFeedToolBar"
app:searchIcon="#null" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/newsRecycleVIew"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/News"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/News"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="News"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.045"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/searchView" />
</androidx.constraintlayout.widget.ConstraintLayout>
*Row Item for parent recycleview added child recycle view*
<?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:id="#+id/constraintlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp">
<androidx.cardview.widget.CardView
android:id="#+id/cardView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
app:cardElevation="5dp"
app:cardUseCompatPadding="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/newsThumbNail"
android:layout_width="0dp"
android:layout_height="200dp"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.448"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="#+id/newsTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Dangerous fire is out of control at 24 and park"
android:textSize="17sp"
app:layout_constraintEnd_toEndOf="#+id/newsThumbNail"
app:layout_constraintStart_toStartOf="#+id/newsThumbNail"
app:layout_constraintTop_toBottomOf="#+id/newsThumbNail" />
<TextView
android:id="#+id/newsBody"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Some text is here"
app:layout_constraintEnd_toEndOf="#+id/newsTitle"
app:layout_constraintStart_toStartOf="#+id/newsTitle"
app:layout_constraintTop_toBottomOf="#+id/newsTitle" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/earningsRecycleView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.448"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView5" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Upcoming Earnings"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.045"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardView" />
</androidx.constraintlayout.widget.ConstraintLayout>
**Parent RecycleView adapter **
public class SearchStockAdapter extends RecyclerView.Adapter<SearchStockAdapter.SearchStocksViewHolder> {
private Context context;
private ArrayList<News_Model> stockInformaiton;
private RequestQueue requestQueue;
private SearchStockSubAdapter searchStockSubAdapter;
private RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
public SearchStockAdapter(Context context, ArrayList<News_Model> stockInformaiton) {
this.context = context;
this.stockInformaiton = stockInformaiton;
}
#NonNull
#Override
public SearchStocksViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.search_stock_row, viewGroup, false);
requestQueue = Volley.newRequestQueue(context);
return new SearchStocksViewHolder(view);
}
//Loads data into views
#Override
public void onBindViewHolder(#NonNull SearchStocksViewHolder viewHolder, int i) {
viewHolder.newsTitle.setText(stockInformaiton.get(i).getTitle());
viewHolder.newsBody.setText(stockInformaiton.get(i).getDescription());
Glide.with(this.context).asBitmap().load(stockInformaiton.get(i).getUrlToImage()).into(viewHolder.newsThumbImage);
//Second recycle view...
LinearLayoutManager layoutManager = new LinearLayoutManager(
viewHolder.upcomingEarningsRecycleView.getContext(),
LinearLayoutManager.HORIZONTAL,
false
);
layoutManager.setInitialPrefetchItemCount(stockInformaiton.size());
//second recycle view..
SearchStockSubAdapter stockSubAdapter = new SearchStockSubAdapter(context, stockInformaiton);
viewHolder.upcomingEarningsRecycleView.setLayoutManager(layoutManager);
viewHolder.upcomingEarningsRecycleView.setAdapter(stockSubAdapter);
viewHolder.upcomingEarningsRecycleView.setHasFixedSize(true);
viewHolder.upcomingEarningsRecycleView.setRecycledViewPool(viewPool);
}
#Override
public int getItemCount() {
return stockInformaiton.size();
}
static class SearchStocksViewHolder extends RecyclerView.ViewHolder {
private TextView newsTitle, newsBody;
private ImageView newsThumbImage;
private RecyclerView upcomingEarningsRecycleView;
public SearchStocksViewHolder(#NonNull View itemView) {
super(itemView);
//newsThumbnail = itemView.findViewById(R.id.newsThumbNail);
newsTitle = itemView.findViewById(R.id.newsTitle);
newsBody = itemView.findViewById(R.id.newsBody);
newsThumbImage = itemView.findViewById(R.id.newsThumbNail);
upcomingEarningsRecycleView = itemView.findViewById(R.id.earningsRecycleView);
}
}
}

Categories

Resources