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;
}
Related
I am using two RecyclerViews in my app, one in the MainActivity and one in another activity. The one in the MainActivity works fine but the other has a NullPointerException and I don't know where the problem here is.
Here is a code snippet of the second RecyclerView, where the logcat says that there is a Null object reference. I used the same code as the first RecyclerView where everything works.
The error message says that there is a problem in this line: recyclerView.setLayoutManager(layoutManager);
and in this line
initRecyclerViewFriendOverView();
Here is where I implemented my second RecyclerView:
private void initRecyclerViewFriendOverView() {
LinearLayoutManager layoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
RecyclerView recyclerView = findViewById(R.id.recyclerViewFriendOverView);
recyclerView.setLayoutManager(layoutManager);
RecyclerViewAdapterFriendOverView adpater = new RecyclerViewAdapterFriendOverView(mNames, mImageUrls, this);
recyclerView.setAdapter(adpater);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_overview);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
init();
initRecyclerViewFriendOverView();
getImages();
}
Here ist the XML where the second RecyclerView is used:
<?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"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerViewFreundesUebersicht"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Edit:
The problem was that I called the RecyclerView before initializing it in the onCreate method.
EDIT:
You are including another xml file in your activity using include statement, findViewById() won't be able to find views from included layouts. Give an id to your include statement, find that included view first and then call findViewById () in that view:
In Activity layout file:
<include id="includedRecyclerLayout"
....
</include>
And in your code:
ConstraintLayout includedLayout = findViewById(R.id.inckudedRecyclerLayout);
RecyclerView recyclerview = includedLayout.findViewById(R.id.RECYCLER_VIEW_ID);
If it shows error at recyclerView.setLayoutManager(layoutManager); it means your recyclerView is null. Then you look at where you initialized it:
RecyclerView recyclerView = findViewById(R.id.recyclerViewFreundesUebersicht);
It can be null because of two reasons:
You didn't add the RecyclerView to XML
Your R.id.recyclerViewFreundesUebersicht is referring to a view from another xml
Check your XML and make sure your recyclerview was added to XML and the id you are using here is same as the one used in activity.
Initialize LinearLayoutManager like this
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
How can i set up my RecyclerView properly to detect swipe Up and Down?
When i place recycler.setOnTouchListener(this)
I get this warning: recyclerview has setontouchlistener but does not override perfoclick and nothing works.
Thank you.
recyclerView = (RecyclerView)rootview.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(layoutManager);
Adapter adapter=new
Adapter (List);
recyclerView.setAdapter(adapter);
You should be using SwipeRefreshLayout and handle the callback with setOnRefreshListener example.
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swRefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
SwipeRefreshLayout mSwRefresh=findViewById(R.id.SwRefresh);;
mSwRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
//TODO:
}
});
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" />
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);
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);