How to add right and left swipes to the RecyclerView? - java

I have been using the fragments(Java). The list is showing in the RecyclerView but when I swipe left or right, it is not getting detected.
I do not know Kotlin. Need help in Java implementation.
Bible - Chapter view
The expected behavior is to go to the next chapter or previous chapter when I swipe left or right. Each chapter is a List.
I have tried with ViewPager2 but I am unable to understand how to use RecyclerView inside ViewPager2.
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.handsoncloud.holybible.R;
import com.handsoncloud.holybible.adapters.RecyclerBibleAdapter;
import com.handsoncloud.holybible.ui.chapter.ChaptersFragment;
import com.handsoncloud.holybible.utils.Books;
public class HomeFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
RecyclerView recyclerView = rootView.findViewById(R.id.recycler_bibleBooks);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
RecyclerBibleAdapter recyclerBibleAdapter = new RecyclerBibleAdapter(getContext(), Books.getBooks(), (book, position) -> {
ChaptersFragment.BOOK_NAME_ENGLISH = Books.booksInEnglish.get(position);
ChaptersFragment.BOOK_NAME_TELUGU = Books.booksInTelugu.get(position);
});
recyclerView.setAdapter(recyclerBibleAdapter);
return rootView;
}
#Override
public void onDestroyView() {
super.onDestroyView();
}
}

Related

Recycler View on Fragment Activity

I am making 50 data appear in my code, but I am putting it in a recycler view in a fragment because I have a dropdown menu, but when I put the code to call everything I get these two lines of error:
package com.example.tallerof.ui.gallery;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.tallerof.R;
import com.example.tallerof.adapters.UsuariosAdapter;
import java.util.ArrayList;
public class GalleryFragment extends Fragment {
private GalleryViewModel galleryViewModel;
ArrayList<String> ListadeDatos;
RecyclerView recyclerView;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
recyclerView=(RecyclerView) findViewById(R.id.Recycler1); /* Error #1 Cannot resolve method findViewById in GalleryFragment*/
recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)); /* Error #2 required type: context provide: Gallery Fragment*/
ListadeDatos=new ArrayList<String>();
for(int i=0; i<=50; i++){
ListadeDatos.add("Dato # "+i+"");
}
UsuariosAdapter adapter= new UsuariosAdapter(ListadeDatos);
recyclerView.setAdapter(adapter);
return null;
}
}
you must to do this.
recyclerView = getActivity().findViewbyId(R.id.Recycler1);
Regards.

Toolbar in Fragment crashes app on launch

This seems like a common issue with lots of accepted answers but none of them seem to be working for me.
Most answers suggest using this piece of code to add a toolbar into a fragment:
mToolbar = (Toolbar)rootView.findViewById(R.id.toolbar);
if (mToolbar != null) {
((AppCompatActivity) getActivity()).setSupportActionBar(mToolbar);
}
Alas, this still crashes my app even though I'm using AppCompatActivity. I think the error may lie in the fact that my Fragment class extends Fragment and not AppCompatActivity but I don't know enough about android as of yet to be sure of this. It crashes on the setSupportActionBar line.
My Fragment code:
package erikligai.ribbitapplication;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* Created by erik on 2017-06-07.
*/
public class MessageFragment extends Fragment {
Toolbar mToolbar;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
// return inflater.inflate(R.layout.message_fragment_layout, container, false);
View rootView = inflater.inflate(R.layout.message_fragment_layout, container, false);
mToolbar = (Toolbar)rootView.findViewById(R.id.toolbar);
if (mToolbar != null) {
((AppCompatActivity) getActivity()).setSupportActionBar(mToolbar);
}
return rootView;
}
Would appreciate any suggestions.
You need to cast your activity from getActivity() to AppCompatActivity
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle();
Toolbar getting from Activity, not from Fragment
mToolbar = (Toolbar)rootView.findViewById(R.id.toolbar);

Cardview inside fragment

Im trying to put cards in one of the fragment but I got this error: There is no applicable constuctor to '(com.sample.app.FragmentOne)'. By the way Im using AIDE IDE.
The error is the (this) here:
mLayoutManager = new LinearLayoutManager(this);
FragmentOne.java
package com.sample.app;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import com.sample.app.MainActivity;
import com.sample.app.R;
public class FragmentOne extends Fragment {
RecyclerView mRecyclerView;
RecyclerView.LayoutManager mLayoutManager;
RecyclerView.Adapter mAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View view = inflater.inflate(R.layout.fragment_one, container, false);
((MainActivity) getActivity()).getSupportActionBar().setTitle("Fragment One");
mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new CardAdapter();
mRecyclerView.setAdapter(mAdapter);
return view;
}
}
Please help me, thanks in advance.
I changed this
mLayoutManager = new LinearLayoutManager(this);
Into
mLayoutManager = new LinearLayoutManager(getActivity());
Change that this to getActivity()
For Activity, use something like (MainActivity.this);
For Fragments, use getActivity(); or requirActivity();

NavigationDrawer's menu is not working

When I click at the menu items, then layouts not coming to view.
And MainActicity wanna "menu1_Fragment.java" encode with "android.support.v4.app.Fragment"
İf I encode only "Fragment", MainActivity is getting error.
In compatible types.
Required :android.support.v4.app.Fragment
Found :intizamyazilim.navigationdrawernew.menu1_Fragment
Here is menu1_Fragment.java
package intizamyazilim.navigationdrawernew;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Administrator on 01.03.2015.
*/
public class menu1_Fragment extends android.support.v4.app.Fragment {
View rootview;
#Nullable
// #Override
public View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.menu1_layout, container, false);
return rootview;
}
}
I Fix it.
I changed this "menu1_Fragment.java" with this codes:
package intizamyazilim.navigationdrawernew;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class menu1_Fragment extends android.support.v4.app.Fragment {
public menu1_Fragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.menu1_layout, container, false);
return rootView;
}
}
No it is not the proper way to fix it. The proble here is that you are extending your menu1_Fragment using support library and on the other hand you are importing fragment from SDK(import android.app.Fragment;). Make it consistent. Either use fragment from support or from android SDK.

LayoutInflater wordSpan

I want to set wordtoSpan string at OnCreateView.
my code:
package com.tachles;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Spannable;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class History_moadim_a extends Fragment {
TextView horef;
TextView kaiz;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.history_moadim_a, container, false);
return rootView;
horef = (TextView) findViewById(R.id.textView3);
Spannable wordtoSpan = new SpannableString("hgh");
horef.setText(wordtoSpan);
}
}
Eclipse says that I cant use findViewById, so I tried to insert getView() but still the same problem. Please show me my mistake
Thank you
If history_moadim_a is your layout containing the textView3, change your onCreateView() to something like
View rootView = inflater.inflate(R.layout.history_moadim_a, container, false);
horef = (TextView) rootView.findViewById(R.id.textView3);
Spannable wordtoSpan = new SpannableString("hgh");
horef.setText(wordtoSpan);
return rootView;
You want to find the TextView in the layout you just inflated (rootView), and return the layout at the end (and not in the middle of the method).
use
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Spannable;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class History_moadim_a extends Fragment {
TextView horef;
TextView kaiz;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.history_moadim_a, container, false);
horef = (TextView) rootView.findViewById(R.id.textView3);
Spannable wordtoSpan = new SpannableString("hgh");
horef.setText(wordtoSpan);
return rootView;
}
}
i can not see a return statement at onCreateView at perfect place so please do return rootView ; inside onCreateView at the end of the logics by means at the end of the onCreateView

Categories