Android Recycler View orientation can't be changed to horizontal - android

Currently I have done the code to implement recyclerview with cards view inside. But when I tried to change the recyclerview's orientation from vertical to horizontal it just doesn't change. Since I put the recyclerview inside a fragment, I don't know if it's because of this. My xml code:
<android.support.v7.widget.RecyclerView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:scrollbars="horizontal"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>
I have tried both of these:
View rootView = inflater.inflate(R.layout.discover_fragment, container, false);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(layoutManager);
and:
View rootView = inflater.inflate(R.layout.discover_fragment, container, false);
LinearLayoutManager layoutManager = new GridLayoutManager(getActivity(), 1, GridLayoutManager.HORIZONTAL, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(layoutManager);

Related

Recyclerview has no LayoutManager android studio

I'm trying to implement RecyclerView in my android app but it gives me error
"RecyclerView has no Layoutmanager"
even though I initialize it in my code. My java and xml file:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_list, container, false);
mRecyclerView = rootView.findViewById(R.id.recyclerView);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
mRecyclerView.setLayoutManager(layoutManager);
mAdapter = new CustomAdapter(mDataset);
mRecyclerView.setAdapter(mAdapter);
return rootView;
}
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="match_parent"
tools:context=".List"
android:background="#color/black">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="495dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent"/
</androidx.constraintlayout.widget.ConstraintLayout>
You can't use signal activity for recy.... And main activity . Plz create a new xml file like list_file
Are you sure that added recyclerview dependency into gradle
AndroidX
implementation 'androidx.recyclerview:recyclerview:1.1.0'
Support library
implementation 'com.android.support:recyclerview-v7:28.0.0'

how to expand horizantal recyclerview into vertical

I using normal recyclerview and set to LinearLayoutManager.HORIZONTAL, now when I click on button recyclerview should expand to vertical and If I click on the button again it should set back to horizontal recylerview again.
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="horizontal"
android:layout_marginTop="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/topCategoryTxt" />
onButtonClick
if (LinearLayoutManager.HORIZONTAL == 0) {
layoutManager = new LinearLayoutManager(HomeScreen.this, LinearLayoutManager.VERTICAL, false);
recyclerview.setLayoutManager(layoutManager);
recyclerview.setAdapter(popularItemsAdapter);
adapter.notifyDataSetChanged();
}else{
layoutManager = new LinearLayoutManager(HomeScreen.this, LinearLayoutManager.HORIZONTAL, false);
recyclerview.setLayoutManager(layoutManager);
recyclerview.setAdapter(popularItemsAdapter);
adapter.notifyDataSetChanged();
}
Condition is always being 0
how to change it dynamically
Thank you
boolean orientaion = false
if (!orientaion) {
layoutManager = new LinearLayoutManager(HomeScreen.this, LinearLayoutManager.VERTICAL, false);
recyclerview.setLayoutManager(layoutManager);
recyclerview.setAdapter(popularItemsAdapter);
adapter.notifyDataSetChanged();
orientaion=true;
}else{
layoutManager = new LinearLayoutManager(HomeScreen.this, LinearLayoutManager.HORIZONTAL, false);
recyclerview.setLayoutManager(layoutManager);
recyclerview.setAdapter(popularItemsAdapter);
adapter.notifyDataSetChanged();
orientaion=false;
}

Unable to update views through data binding on Android

I was trying to migrate from ButterKnife to Android data binding.
When using ButterKnife (which is working perfectly fine):
// Called after a network call
public void updateView(MyAdapter adapter, String title) {
toolbar.setTitle(title);
recyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
Using Android data binding:
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
binding = MyFragmentBinding.inflate(inflater, container, false);
binding.toolbar.setTitle(R.string.app_name); // this works
return binding.getRoot();
}
// Called after a network call
public void updateView(MyAdapter adapter, String title) {
binding.toolbar.setTitle(title);
binding.recyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
binding.recyclerView.setLayoutManager(layoutManager);
binding.recyclerView.setAdapter(adapter);
}
After calling updateView on the later code, the screen goes blank.
I debugged to find out that view remains attached to the Fragment.
Also, if I am updating something inside onCreateView then that works.
Is there anything am I doing wrong?
Layout:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
android:elevation="#dimen/_6sdp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:navigationIcon="#drawable/default_nav_icon_back"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize" />
</android.support.design.widget.CoordinatorLayout>
</layout>

How to set Horizontal Recyclerview in one of the fragment in ViewPager

I m having a ViewPager with three fragments , one fragment has something that scrolls horizontally but when m trying to scroll it slides the page
if you need a horizontal RecyclerView you just need to add a the RecyclerView to your fragment and use LinearLayoutManager.HORIZONTAL
LinearLayoutManager layoutManager
= new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
RecyclerView myList = (RecyclerView) findViewById(R.id.my_recycler_view);
myList.setLayoutManager(layoutManager);
OR
just add this RecyclerView in your fragment
<android.support.v7.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layoutManager="android.support.v7.widget.LinearLayoutManager" />

Setting scrollbars property in RecyclerView crashes my app

I recently started using RecyclerView replacing older ListView. But whenever I use android:scrollbars="vertical" in android.support.v7.widget.RecyclerView element, my app crashes with the following error:
Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollVertically()' on a null object reference
This is how I'm using RecyclerView:
View rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.my_fragment, container, false);
return rootView;
}
protected void onPostExecute(Boolean result) {
RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.recyclerview);
LinearLayoutManager llm = new LinearLayoutManager(context);
rv.setLayoutManager(llm);
RecycleViewAdapter adapter = new RecycleViewAdapter(myRecyclerViewAdapter);
rv.setHasFixedSize(true);
rv.setAdapter(adapter);
}
And this is my_fragment.xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
Everything works perfectly fine if I don't use android:scrollbars="vertical". Thanks for your help.
When using the RecyclerView you don't really need to specify the scrollbars display; it is the LayoutManager job where you specify if it is Vertical or Horizontal orientation and of course the scrollbar display will automatically change whatever orientation you choose.
//for vertical scrollbar and orientation
LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
//for horizontal scrollbar and orientation
LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);

Categories

Resources