I am new at android programming and I getting the error "No adapter attached; skipping layout". My app runs but when I click to open my fragment wich have an cardview inside, only displays the first content of my ArrayList.
I have been following this tutorial: https://code.tutsplus.com/tutorials/getting-started-with-recyclerview-and-cardview-on-android--cms-23465, but I am doing in a different way. I am using fragments instead of activity, but I think that is not a problem. So, here I have my Activity:
public class Av_ninja extends AppCompatActivity {
TabLayout tabLayout;
ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_av_ninja);
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setAdapter(new CustomAdapter(getSupportFragmentManager(), getApplicationContext()));
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
private class CustomAdapter extends FragmentPagerAdapter{
private String fragmentes [] = {"Info","Abilitities"};
public CustomAdapter(FragmentManager suppFragmentManager, Context applicaContext){
super(suppFragmentManager);
}
#Override
public Fragment getItem(int position){
switch (position){
case 0:
return new Fragment1();
case 1:
return new Fragment2();
default:
return null;
}
}
#Override
public int getCount(){
return fragmentes.length;
}
#Override
public CharSequence getPageTitle(int position) {
return fragmentes[position];
}
}
}
Fragment
public class Fragment1 extends Fragment {
ImageView Icons;
TextView Titulos;
TextView Textos;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
// return inflater.inflate(R.layout.frag1,container,false);
View rootView = inflater.inflate(R.layout.card_ninja_info,container,false);
Icons = (ImageView) rootView.findViewById(R.id.Icons);
Titulos = (TextView) rootView.findViewById(R.id.Titulos);
Textos = (TextView) rootView.findViewById(R.id.Textos);
Icons.setImageResource(R.drawable.offenseicon);
Titulos.setText("Role");
Textos.setText("Scout");
return rootView;
}
}
RecyclerView
public class RecyclerViewAv extends Fragment{
private List<Heroi> herois;
private RecyclerView rv;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
//return inflater.inflate(R.layout.frag2,container,false);
View rootView = inflater.inflate(R.layout.card_ninja_info,container,false);
rv = (RecyclerView)rootView.findViewById(R.id.rv_frag1);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);
rv.setHasFixedSize(true);
initializeData();
initializeAdapter();
return rootView;
}
private void initializeData(){
herois = new ArrayList<>();
herois.add(new Heroi(R.drawable.offenseicon, "Role", "Scout"));
herois.add(new Heroi(R.drawable.identityicon, "Identity", "Genji Shimada"));
herois.add(new Heroi(R.drawable.workicon, "Work", "Adventurer"));
herois.add(new Heroi(R.drawable.localizationicon, "Base of Operation", "Nepal"));
herois.add(new Heroi(R.drawable.empresaicon, "Affiliation", "Shiamda Clan"));
herois.add(new Heroi(R.drawable.ageicon, "Age", "27"));
herois.add(new Heroi(R.drawable.healthicon, "Health", "100"));
}
private void initializeAdapter(){
RVAdapter adapter = new RVAdapter(herois);
rv.setAdapter(adapter);
}
}
Adapter
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.HeroiViewHolder>{
public static class HeroiViewHolder extends RecyclerView.ViewHolder{
CardView cv;
ImageView Icons;
TextView Titulos;
TextView Textos;
public HeroiViewHolder(View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cv);
Icons = (ImageView)itemView.findViewById(R.id.Icons);
Titulos = (TextView)itemView.findViewById(R.id.Titulos);
Textos = (TextView)itemView.findViewById(R.id.Textos);
}
}
List<Heroi> herois;
RVAdapter(List<Heroi> herois){
this.herois=herois;
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView){
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public RVAdapter.HeroiViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
HeroiViewHolder hvh = new HeroiViewHolder(v);
return hvh;
}
#Override
public void onBindViewHolder(HeroiViewHolder holder, int position) {
holder.Icons.setImageResource(herois.get(position).photoId);
holder.Titulos.setText(herois.get(position).tit);
holder.Textos.setText(herois.get(position).res);
}
#Override
public int getItemCount() {
return herois.size();
}
}
class Heroe
public class Heroi {
int photoId;
String tit;
String res;
public Heroi(int photoId, String tit, String res) {
this.photoId = photoId;
this.tit = tit;
this.res = res;
}
}
Thanks in advance!
Here you could do following changes to make it properly :
1) Update your onCreateView method
private RVAdapter rvAdapter;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
//return inflater.inflate(R.layout.frag2,container,false);
View rootView = inflater.inflate(R.layout.card_ninja_info,container,false);
rv = (RecyclerView)rootView.findViewById(R.id.rv_frag1);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
// Added here
llm.setOrientation(LinearLayoutManager.VERTICAL);
rv.setHasFixedSize(true); // Here we changed order you should call it first before setting LayoutManager
rv.setLayoutManager(llm);
//Set empty adapter and pass only context if you will needed inside it
rvAdapter = new RVAdapter(getActivity());
rv.setAdapter(rvAdapter);
initializeData();
initializeAdapter();
return rootView;
}
2) Create method in your adapter class to update your content list
public void updateList(List<Heroi> herois){
this.herois = herois;
notifyDataSetChanged();
}
3) Only update your contentList inside initializeAdapter method(No need to set adapter again)
private void initializeAdapter(){
rvAdapter.updateList(herois);
}
That's it!
Update your adapter as per below :
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.HeroiViewHolder>{
List<Heroi> herois;
public static class HeroiViewHolder extends RecyclerView.ViewHolder{
CardView cv;
ImageView Icons;
TextView Titulos;
TextView Textos;
Context context;
public HeroiViewHolder(View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cv);
Icons = (ImageView)itemView.findViewById(R.id.Icons);
Titulos = (TextView)itemView.findViewById(R.id.Titulos);
Textos = (TextView)itemView.findViewById(R.id.Textos);
}
}
RVAdapter(Context context){
this.context = context;
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView){
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public RVAdapter.HeroiViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
HeroiViewHolder hvh = new HeroiViewHolder(v);
return hvh;
}
#Override
public void onBindViewHolder(HeroiViewHolder holder, int position) {
holder.Icons.setImageResource(herois.get(position).photoId);
holder.Titulos.setText(herois.get(position).tit);
holder.Textos.setText(herois.get(position).res);
}
#Override
public int getItemCount() {
return herois.size();
}
public void updateList(List<Heroi> herois){
this.herois = herois;
notifyDataSetChanged();
}
}
Problem Solved I just deleted RecyclerView Class and putted it into Fragment1 class.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
// return inflater.inflate(R.layout.frag1,container,false);
View rootView = inflater.inflate(R.layout.frag1,container,false);
rv = (RecyclerView)rootView.findViewById(R.id.rv_frag1);
LinearLayoutManager llm = new LinearLayoutManager(getContext());
// llm.setOrientation(LinearLayoutManager.VERTICAL);
// rv.setHasFixedSize(true);
rv.setLayoutManager(llm);
initializeData();
rvAdapter = new RVAdapter(herois);
rv.setAdapter(rvAdapter);
return rootView;
}
Thanks Anyway
Related
I am building an Android Application which have Tab Layout in which there are Fragments and in Fragment I am displaying some value.
I have just only added hardcoded values to test the layout, but even these are not visible in the fragment.
Here is the code for ViewPagerAdapter:
public class FavouriteViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> favouriteFragmentList = new ArrayList<>(); // this line can cause crashes
private final List<String> favouriteFragmentListTitles = new ArrayList<>();
public FavouriteViewPagerAdapter(#NonNull FragmentManager fm, int behavior) {
super(fm, behavior);
}
#NonNull
#Override
public Fragment getItem(int position) {
return favouriteFragmentList.get(position);
}
#Override
public int getCount() {
return favouriteFragmentListTitles.size();
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
return favouriteFragmentListTitles.get(position);
}
public void AddFragment(Fragment fragment, String title) {
favouriteFragmentList.add(fragment); // this line can cause crashes
favouriteFragmentListTitles.add(title);
}
}
I have tried the answers available already none of them solves my problem.
I debugged the application and my RecyclerViewAdapter is not even called, very strange. I have done this many time but this never happened.
Here is my code for RecyclerViewAdapter :
public class ProductsRecyclerAdapter extends RecyclerView.Adapter<ProductsRecyclerAdapter.viewHolder> {
Context context;
private List<FavouriteProducts> favouriteProductsData;
public ProductsRecyclerAdapter(Context context, List<FavouriteProducts> favouriteProductsData) {
this.context = context;
this.favouriteProductsData = favouriteProductsData;
}
#NonNull
#Override
public viewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.single_favourite_product_item, parent, false);
viewHolder vHolder = new viewHolder(v);
return vHolder;
}
#Override
public void onBindViewHolder(#NonNull viewHolder holder, int position) {
holder.product_name.setText(favouriteProductsData.get(position).getProduct_name());
holder.product_price.setText(favouriteProductsData.get(position).getProduct_price());
holder.product_image.setImageResource(favouriteProductsData.get(position).getProduct_image());
Toast.makeText(context,favouriteProductsData.get(position).getProduct_price() , Toast.LENGTH_SHORT).show();
/*holder.heart_button.setOnLikeListener(new OnLikeListener() {
#Override
public void liked(LikeButton likeButton) {
showSuccessToast(context,"Liked");
}
#Override
public void unLiked(LikeButton likeButton) {
showSuccessToast(context,"Unliked");
}
});*/
}
#Override
public int getItemCount() {
return favouriteProductsData.size();
}
static class viewHolder extends RecyclerView.ViewHolder {
MaterialTextView product_name, product_price;
ImageView product_image;
LikeButton heart_button;
public viewHolder(#NonNull View itemView) {
super(itemView);
product_name = itemView.findViewById(R.id.product_name);
product_price = itemView.findViewById(R.id.product_price);
product_image = itemView.findViewById(R.id.product_image);
heart_button = itemView.findViewById(R.id.heart_button);
}
}
}
I am not getting any errors but after I check adapter is not even called. Some help will be really helpful.
Fragment Code:
public class FavouriteProductsFragment extends Fragment {
View v;
private List<FavouriteProducts> productList;
public FavouriteProductsFragment() {
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
productList = new ArrayList<>();
productList.add(new FavouriteProducts("All Weather", "200/Ltr", R.drawable.all_weather));
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
v = inflater.inflate(R.layout.products_favourite_fragment, container, false);
RecyclerView productRecyclerView = v.findViewById(R.id.favourite_products_rv);
ProductsRecyclerAdapter recyclerAdapter = new ProductsRecyclerAdapter(getContext(), productList);
productRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
productRecyclerView.hasFixedSize();
productRecyclerView.setAdapter(recyclerAdapter);
return v;
}
}
productList = new ArrayList<>();
productList.add(new FavouriteProducts("All Weather", "200/Ltr",R.drawable.all_weather));
recyclerAdapter.notifyDataSetChanged()
RecyclerView productRecyclerView = v.findViewById(R.id.favourite_products_rv);
ProductsRecyclerAdapter recyclerAdapter = new ProductsRecyclerAdapter(getContext(), productList);
productRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
productRecyclerView.hasFixedSize();
productRecyclerView.setAdapter(recyclerAdapter);
You need to add
RecyclerView productRecyclerView = v.findViewById(R.id.favourite_products_rv);
ProductsRecyclerAdapter recyclerAdapter = new ProductsRecyclerAdapter(getContext(), productList);
productRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
productRecyclerView.hasFixedSize();
productRecyclerView.setAdapter(recyclerAdapter);
productList = new ArrayList<>();
productList.add(new FavouriteProducts("All Weather", "200/Ltr",R.drawable.all_weather));recyclerAdapter.notifyDataSetChanged()
and Remove oncreate() method
try productRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); befor setting adapter in recyclerview.
Step1: First Create one or MoreFragment For Your ViewPager and in This Fragment Add your Recyclerview Code as per Your Requirement.
Like Below Code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_whatsapp_image, container, false);
context = getActivity();
((HomeActivity) getActivity()).toolbar.setVisibility(View.VISIBLE);
setupView(root);
return root;
}
private void setupView(ViewGroup root) {
recyclerView = (RecyclerView) root.findViewById(R.id.recyclerViewImage);
progressBar=root.findViewById(R.id.simpleProgressBar);
txtinfo=root.findViewById(R.id.txtinfo);
setUPList();
}
Step2: Then Create ViewPager Adapter for Your TabLayout
public class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
#Override
public Fragment getItem(int position) {
if(position == 0) return new AudioFragment();
if(position == 1) return new VideoFragment();
if(position == 2) return new ImageFragment();
throw new IllegalStateException("Unexpected position " + position);
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
if(position == 0) return "Audio";
if(position == 1) return "Video";
if(position == 2) return "Image";
throw new IllegalStateException("Unexpected position " + position);
}
}
Step3 : Then Call Your SetupViewpager Method in Your Oncreate method of your Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=findViewById(R.id.toolbar);
viewPager=findViewById(R.id.viewpager);
tabLayout=findViewById(R.id.tabs);
//add toolbar
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setupViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
setTabIcons();
}
NOTE: You Can Download ViewPager Code from Below Link.Click Here to View and Download
I'm am trying to open a new fragment by selecting the image from the card view within the recycler view. Preferable using both adapter and fragment classes. How do I do that?
This is my adapter class:
public class CorporateAdapter extends RecyclerView.Adapter {
private int[] images = {
R.drawable.sample1,
R.drawable.sample2
};
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.corporate_list_row, viewGroup, false);
final MyViewHolder vHold = new MyViewHolder(view);
return vHold;
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder viewHolder, int i) {
((MyViewHolder) viewHolder).bindView(i);
}
#Override
public int getItemCount() {
return images.length;
}
private class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private ImageView mImageView;
public MyViewHolder(View itemView) {
super(itemView);
mImageView = (ImageView) itemView.findViewById(R.id.showImage);
mImageView.setOnClickListener(this);
}
public void bindView(int position) {
mImageView.setImageResource(images[position]);
}
#Override
public void onClick(View v) {
}
}
This is my Fragment class:
public class CorporateFragment extends Fragment {
private final int LAYOUT_LISTCORPORATE_FRAGMENT = R.layout.corporate_membership;
private View v;
private Date date;
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
public CorporateFragment() {
}
public static CorporateFragment newInstance(Date date) {
CorporateFragment fragment = new CorporateFragment();
Bundle args = new Bundle();
//args.putSerializable(ARG_DATE,date);
fragment.setArguments(args);
return fragment;
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
// Inflate the layout for this fragment
v = inflater.inflate(LAYOUT_LISTCORPORATE_FRAGMENT, container, false);
ButterKnife.bind(this, v);
return v;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
recyclerView = (RecyclerView) v.findViewById(R.id.recyclerView);
final CorporateAdapter listAdapter = new CorporateAdapter();
recyclerView.setAdapter(listAdapter);
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
}
I've seen some ways where by we can open up the new fragment straight from the adapter, but the more correct way would be having the Onclicklistener in both the fragment class and adapter class.
Is there any way I could do that? Thank you
Here is An example for You paste as it is.
Make constructor which will have an interface as parameter in adapter and override it in Fragment.
Your Fragment Code will look like this.
public class CorporateFragment extends Fragment {
private final int LAYOUT_LISTCORPORATE_FRAGMENT = R.layout.corporate_membership;
private View v;
private Date date;
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
public CorporateFragment() {
}
private int[] images = {
R.drawable.sample1,
R.drawable.sample2
};
public static CorporateFragment newInstance(Date date) {
CorporateFragment fragment = new CorporateFragment();
Bundle args = new Bundle();
//args.putSerializable(ARG_DATE,date);
fragment.setArguments(args);
return fragment;
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
// Inflate the layout for this fragment
v = inflater.inflate(LAYOUT_LISTCORPORATE_FRAGMENT, container, false);
ButterKnife.bind(this, v);
return v;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
recyclerView = (RecyclerView) v.findViewById(R.id.recyclerView);
final CorporateAdapter listAdapter = new CorporateAdapter(getActivity(), images, new CorporateAdapter.onItemClickListener() {
#Override
public void onItemClick(View view, int position) {
/////////////////// do your on Click Stuff here...
/////////// for ex .. go to next fragment
}
});
recyclerView.setAdapter(listAdapter);
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
}
}
And Your adapter is set to go like this
public class CorporateAdapter extends RecyclerView.Adapter<CorporateAdapter.MyViewHolder> {
Context context;
private int[] images;
onItemClickListener onItemClickListener;
public CorporateAdapter(Context context, int[] images, CorporateAdapter.onItemClickListener onItemClickListener) {
this.context = context;
this.images = images;
this.onItemClickListener = onItemClickListener;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.corporate_list_row, viewGroup, false);
final MyViewHolder vHold = new MyViewHolder(view);
return vHold;
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, final int position) {
((MyViewHolder) holder).bindView(position);
holder.mImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onItemClickListener.onItemClick(view,position);
}
});
}
#Override
public int getItemCount() {
return images.length;
}
private class MyViewHolder extends RecyclerView.ViewHolder {
private ImageView mImageView;
public MyViewHolder(View itemView) {
super(itemView);
mImageView = (ImageView) itemView.findViewById(R.id.showImage);
}
public void bindView(int position) {
mImageView.setImageResource(images[position]);
}
}
public interface onItemClickListener{
void onItemClick(View view , int position);
}
}
Hope it will help. Good Luck
My app has a fragment that host a nested viewPager (One fragment added every time) all views are linked to the same layout.
Each fragment has a fixed button + recylerview that retrieve its data from firebase adapter.
When I click on the button, it show the correct viewPager position. But if I click on the listview item (viewHolder) it show the next pre-loaded position in the viewPager.
here's the viewpager and the pager adapter of the root fragment
Explore.java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_explore, container, false);
array = controller.SELECTED_CATEGORIES;
viewPager = (ViewPager) root.findViewById(R.id.viewPager);
tabLayout = (TabLayout) root.findViewById(R.id.categories_tablayout);
tabLayout.setupWithViewPager(viewPager);
mAdapter = new MyAdapter(getFragmentManager());
viewPager.setAdapter(mAdapter);
return root;
}
public static class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return controller.SELECTED_CATEGORIES.length;
}
#Override
public Fragment getItem(int position) {
return CategoriesFragment.newInstance(position);
}
#Override
public CharSequence getPageTitle(final int position) {
return controller.SELECTED_CATEGORIES[position];
}
}
CategoriesFragment.java
static CategoriesFragment newInstance(int num) {
CategoriesFragment f = new CategoriesFragment();
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_categories, container, false);
initialize();
listCategoriesFeed = (RecyclerView) rootView.findViewById(R.id.listCategoriesFeeds);
categoriesFeedAdapter = new CategoriesFeedAdapter(CategoriesFeedItem.class, R.layout.categoriesfeed_list_item, CategoriesFeedHolder.class, myref);
llm = new LinearLayoutManager(rootView.getContext());
llm.setOrientation(LinearLayoutManager.VERTICAL);
listCategoriesFeed.setLayoutManager(llm);
listCategoriesFeed.setAdapter(categoriesFeedAdapter);
swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_refresh_layout);
swipeRefreshLayout.setOnRefreshListener(this);
//Holder itemClickListener
CategoriesFeedHolder.registerOnItemClickListener(this);
//TextView onclick listener
View tv = rootView.findViewById(R.id.text);
((TextView)tv).setText("Fragment #" + mNum);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e("position",mNum + "");
}
});
return rootView;
}
#Override
public void onItemClick(View view, int position) {
super.onItemClick(view, position);
switch (view.getId()) {
case R.id.firstId:
Log.e("position",mNum+ "");
break;
case R.id.secondId:
secondAction....
.....
.....
.....
break;
}
}
private void initialize() {
controller.showProgressDialog();
list = controller.getIniCategoriesDetailsList();
list.clear();
controller.categoriesDetailsList.put(controller.SELECTED_CATEGORIES[mNum],list);
reference = FirebaseDatabase.getInstance().getReference("firebase-path/" + controller.SELECTED_CATEGORIES[mNum]);
myref = reference.orderByChild("order-by").limitToFirst(i);
myref.addChildEventListener(this);
}
CategoriesFeedHolder.java
private CategoriesFeedItem model;
private final ImageView mImage;
private static OnItemClickListener onItemClickListener;
public CategoriesFeedHolder(View itemView) {
super(itemView);
this.context = itemView.getContext();
mImage = (ImageView) itemView.findViewById(R.id.m_image);
mImage.setOnClickListener(this);
}
public static void registerOnItemClickListener(OnItemClickListener onItemClickListener) {
CategoriesFeedHolder.onItemClickListener = onItemClickListener;
}
public void setModel(CategoriesFeedItem model) {
this.model = model;
}
#Override
public void onClick(View view) {
onItemClickListener.onItemClick(view, getAdapterPosition());
}
Does anyone know why this happens?
The problem was with the viewholder class, there was no data binding inside it. So when i clicked on any item inside the viewholder, it performed the action on the current adapter's data (preloaded data).
the viewholder class should be something like this:
public ViewHolder(View itemView) {
super(itemView);
share = (ImageView) itemView.findViewById(R.id.share);
}
public void bindToPost(Post post, View.OnClickListener onClickListener) {
share.setOnClickListener(onClickListener);
}
There is no errors in adapter or any other classes. I attached a text view below this recycler view for testing and it displays nicely without recyclerview.
I am just passing a string array with two values as a parameter to the adapter class.
public class HomeFragment extends Fragment {
private String[] dataset = {"One","Two"};
private RecyclerView resview;
private RecyclerView.Adapter madapter;
private RecyclerView.LayoutManager mlayoutManager;
private View view;
public HomeFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
initializeRecyclerView();
View view = inflater.inflate(R.layout.fragment_home, container, false);
mlayoutManager = new LinearLayoutManager(getActivity());
resview.setLayoutManager( mlayoutManager);
resview = (RecyclerView) view.findViewById(R.id.resview);
madapter = new ReAdapter(dataset);
resview.setHasFixedSize(true);
resview.setAdapter(madapter);
return view;
}
//Even without this initialize method, i am getting this same error
void initializeRecyclerView() {
resview = (RecyclerView) view.findViewById(R.id.resview);
resview.setAdapter(new ReAdapter(dataset));
resview.setLayoutManager(new LinearLayoutManager(getActivity()));
resview.setHasFixedSize(true);
}
}
Here is the adapter class.
public class ReAdapter extends RecyclerView.Adapter<ReAdapter.Reviewholder> {
String[] dataset1;
public ReAdapter (String[] dataset){
dataset = this.dataset1;
}
#Override
public Reviewholder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclecontent,parent,false);
Reviewholder holder = new Reviewholder(view);
return holder;
}
#Override
public void onBindViewHolder(Reviewholder holder, int position) {
holder.restext.setText(dataset1[position]);
}
#Override
public int getItemCount() {
return dataset1.length;
}
public class Reviewholder extends RecyclerView.ViewHolder{
public TextView restext;
public Reviewholder(View view) {
super(view);
restext = (TextView) view.findViewById(R.id.restext);
}
}
}
change your constructor like below
public ReAdapter (String[] dataset){
this.dataset1 = dataset;
}
EDIT:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);
resview = (RecyclerView) view.findViewById(R.id.resview);
mlayoutManager = new LinearLayoutManager(getActivity());
resview.setLayoutManager( mlayoutManager);
madapter = new ReAdapter(dataset);
resview.setHasFixedSize(true);
resview.setAdapter(madapter);
return view;
}
Try this
public class HomeFragment extends Fragment {
private RecyclerView resview;
private ReAdapter madapter;
private LinearLayoutManager mlayoutManager;
public HomeFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);
resview = (RecyclerView) view.findViewById(R.id.recycler_view);
resview.setHasFixedSize(true);
mlayoutManager = new LinearLayoutManager(getActivity());
mlayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
resview.setLayoutManager(mlayoutManager);
return view;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
String[] dataset = {"One", "Two"};
madapter = new ReAdapter(dataset);
resview.setAdapter(madapter);
}
class ReAdapter extends RecyclerView.Adapter<ReAdapter.Reviewholder> {
String[] mDataset;
public ReAdapter(String[] collection) {
mDataset = collection;
}
#Override
public Reviewholder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclecontent, parent, false);
return (new Reviewholder(view));
}
#Override
public void onBindViewHolder(Reviewholder holder, int position) {
holder.restext.setText(mDataset[position]);
}
#Override
public int getItemCount() {
return mDataset.length;
}
public class Reviewholder extends RecyclerView.ViewHolder {
public TextView restext;
public Reviewholder(View view) {
super(view);
restext = (TextView) view.findViewById(R.id.restext);
}
}
}
}
I am using FragmentStatePagerAdapter to load fragment with viewpager with tab layout. I am passing different id's to load same fragment with different data in getItem() but it is always returning fragment with same data on first time and when I swipe through next screens and came back to first screen then it will showing correct data.
Any solution or info is very helpful.
I am calling adapter like this in my onCreate() and it always return same data for position 0 and 1.
MyPagerAdapter adapter = new MyPagerAdapter (getSupportFragmentManager(),bundle);
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
My Adapter
static class MyPagerAdapter extends FragmentStatePagerAdapter {
private Bundle b;
public MyPagerAdapter (FragmentManager fm,Bundle b) {
super(fm);
this.b=b;
}
#Override
public Fragment getItem(int position) {
ContentListFragment fragment = new ContentListFragment();
b.putString("id", tabList.get(position).get("id"));
fragment.setArguments(b);
return fragment;
}
#Override
public int getCount() {
return tabList.size();
}
#Override
public CharSequence getPageTitle(int position) {
return tabList.get(position).get("title");
}
}
Edit
ContentListFragment.java
public class ContentListFragment extends Fragment{
private static final String TAG="ContentListFragment";
private String id,type;
private TextView norecord;
private View rootView;
private List<AppReading> appReadingList;
private RecyclerView recyclerView;
private ContentRecyclerAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.d(TAG, "onCreateView");
rootView = inflater.inflate(R.layout.content_list, container,false);
initView();
initData();
return rootView;
}
private void initView() {
// TODO Auto-generated method stub
norecord=(TextView)rootView.findViewById(R.id.norecord);
recyclerView=(RecyclerView) rootView.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
}
private void initData() {
id=getArguments().getString("id");
getContentList(category);
}
// Get records by category
private void getContentList(String id){
Log.d(TAG, "getContentList: "+id);
Dao dao=new Dao(getActivity());
dao.open();
appReadingList=dao.getContentList(id);
dao.close();
//Set adapter if data available
if(appReadingList.size()>0){
adapter=new ContentRecyclerAdapter();
recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(),R.drawable.divider));
recyclerView.setItemAnimator(new FadeInLeftAnimator());
recyclerView.setAdapter(new SlideInLeftAnimationAdapter(adapter));
}else{
norecord.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.GONE);
}
}
private class ContentRecyclerAdapter extends RecyclerView.Adapter<ContentRecyclerAdapter.ViewHolder>{
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.listitem, parent, false);
ViewHolder holder = new ViewHolder(v);
return holder;
}
#Override
public void onBindViewHolder(ViewHolder holder,final int position) {
holder.itemContent.setText(appReadingList.get(position).getTitle());
holder.itemDate.setText(appReadingList.get(position).getAddDate());
}
#Override
public int getItemCount() {
return appReadingList.size();
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
class ViewHolder extends RecyclerView.ViewHolder{
TextView itemContent, itemDate ;
RelativeLayout listItem;
ViewHolder(View itemView){
super(itemView);
itemContent = (TextView) itemView.findViewById(R.id.itemcontent);
itemDate = (TextView) itemView.findViewById(R.id.itemdate);
listItem=(RelativeLayout) itemView.findViewById(R.id.rlListItem);
}
}
}