CardView not shown on RecyclerView (In Fragment) - java

I want to make CardViews visible on a RecyclerView inside a Fragment, but all I get is a infinite loading sign and no cardviews are being showed. I'm very new to Android Programming so please be kind..
MainActivity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentPage fragment = new FragmentPage();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.placeholder, fragment);
fragmentTransaction.commit();
ViewPager viewpager = findViewById(R.id.viewpager);
viewpager.setOffscreenPageLimit(1);
SwipeAdapter adapter = new SwipeAdapter(getSupportFragmentManager());
viewpager.setAdapter(adapter);
viewpager.setPageTransformer(true, new DepthPageTransformer());
viewpager.setCurrentItem(0);
}
}
This is my Adapter class(I followed a Tutorial)
public class ListAdapterIT extends RecyclerView.Adapter {
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return null;
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
}
#Override
public int getItemCount() {
return 0;
}
private class ListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private ImageView mImageView;
public ListViewHolder (View itemView) {
super(itemView);
mImageView = itemView.findViewById(R.id.image2);
itemView.setOnClickListener(this);
}
public void BindViewHolder(int position) {
mImageView.setImageResource(OurData.picturePath[position]);
}
public void onClick(View view) {
}
}
}
This is my Fragment I also think the Problem is in that class
public class FragmentPage extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.page_fragment, container, false);
ArrayList<Integer> items = new ArrayList<>();
items.add(R.layout.example_item);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.listRecyclerView);
ListAdapterIT listAdapter = new ListAdapterIT() {};
recyclerView.setAdapter(listAdapter);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
return view;
}
}

Related

App doesn't display items on RecyclerView

I'm new to android studio . I'm trying to display items in RecycleView but it's doesn't show anything.
This is what android studio says:
E/RecyclerView: No layout manager attached; skipping layout
My Fragment :
public class Articles extends Fragment {
View root;
RecyclerView recyclerView;
NewsList[] myListData = new NewsList[] {
new NewsList("Article 1", "Some Author"),
new NewsList("Article2", "Some Author"),
new NewsList("Article3", "Some Author"),
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_articles, container, false);
recyclerView = root.findViewById(R.id.recyclerView);
NewsAdapter adapter = new NewsAdapter(myListData);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(adapter);
return root;
}
Here is the code of my Adapter class :
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {
private NewsList[] listdata;
// RecyclerView recyclerView;
public NewsAdapter(NewsList[] listdata) {
this.listdata = listdata;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View listItem= layoutInflater.inflate(R.layout.list_item, parent, false);
ViewHolder viewHolder = new ViewHolder(listItem);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
final NewsList myListData = listdata[position];
holder.textView1.setText(listdata[position].getTitle());
holder.textView2.setText(listdata[position].getAuthor());
holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(view.getContext(),"click on item: "+myListData.getTitle(),Toast.LENGTH_LONG).show();
}
});
}
#Override
public int getItemCount() {
return listdata.length;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView textView1;
public TextView textView2;
public RelativeLayout relativeLayout;
public ViewHolder(View itemView) {
super(itemView);
this.textView1 = (TextView) itemView.findViewById(R.id.textView1);
this.textView2 = (TextView) itemView.findViewById(R.id.textView2);
relativeLayout = (RelativeLayout)itemView.findViewById(R.id.relativeLayout);
}
}
Previously I had an error where the app crashed when opened the fragment but now it's skips the RecyclerView.
I will be gratefull if you help me to resolve this issue.
You need to add a layoutManager to your recycler view:
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_articles, container, false);
recyclerView = root.findViewById(R.id.recyclerView);
NewsAdapter adapter = new NewsAdapter(myListData);
recyclerView.setHasFixedSize(true);
// Add this line
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()))
recyclerView.setAdapter(adapter);
return root;
}
It's mandatory to have a Layout manager for your Recyclerview. For example, you can use a linear one by adding this after inflating your view :
recyclerView.setLayoutManager(new LinearLayoutManager( context ));
where context is whatever your context is

RecyclerView: No adapter attached; skipping layout? What should I do? [duplicate]

This question already has answers here:
recyclerview No adapter attached; skipping layout
(38 answers)
Closed 4 years ago.
Today I'm having this issue with my application; I want to say that I read almost ALL the other questions here on StackOverflow but nothing, always the same problem.
homepage.java (main class)
public class homepage extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_homepage);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_homepage, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = null;
switch(getArguments().getInt(ARG_SECTION_NUMBER))
{
case 1:
rootView = inflater.inflate(R.layout.fragment_homepage, container, false);
break;
case 2:
rootView = inflater.inflate(R.layout.fragment_fibra, container, false);
break;
case 3:
rootView = inflater.inflate(R.layout.fragment_adsl, container, false);
break;
case 4:
rootView = inflater.inflate(R.layout.fragment_aiuto, container, false);
break;
}
return rootView;
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return PlaceholderFragment.newInstance(position + 1);
}
#Override
public int getCount() {
// Show 4 total pages.
return 4;
}
}
}
MyAdapter.java (default code from Android Studio Guide)
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>
{
private String[] mDataset;
public static class MyViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public MyViewHolder(TextView v) {
super(v);
mTextView = v;
}
}
public MyAdapter(String[] myDataset) {
mDataset = myDataset;
}
#Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
TextView v = (TextView) LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
MyViewHolder vh = new MyViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.mTextView.setText(mDataset[position]);
}
#Override
public int getItemCount() {
return mDataset.length;
}
}
And finally, one of the fragment that I'm using:
fibra.java
public class fibra extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
private RecyclerView recyclerView;
private MyAdapter adapter;
private LinearLayoutManager llm;
String [] prova={"prova1","prova2","prova2","prova2","prova2","prova2","prova2"};
public fibra() {
}
public static fibra newInstance(String param1, String param2) {
fibra fragment = new fibra();
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);
//setContentView(R.layout.my_activity); (doesn't work "cannot resolve method")
recyclerView.setHasFixedSize(true); //per migliorare performance
llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(llm);
adapter = new MyAdapter(prova);
recyclerView.setAdapter(adapter);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_fibra, container, false);
return rootView;
}
// 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;
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
}
Here:
//setContentView(R.layout.my_activity); (doesn't work "cannot resolve method")
It's like inflating a layout inside an Activity which you already have onCreateView and in a Fragment.
Change the first codes to :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
Then, initialize RecyclerView inside onCreateView().
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_fibra, container, false);
// Here you should initialize RecyclerView and other widgets by rootView.findViewById and etc
recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true); //per migliorare performance
llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(llm);
adapter = new MyAdapter(prova);
recyclerView.setAdapter(adapter);
return rootView;
}
You forget to add tablaylout.setupWithViewPager(mViewPager); in your homepage.java

Handling onClicks of RecyclerView inside a Fragment

I am trying to implement RecyclerView inside a Fragment. I have designed a CustomAdapter(StateAdapter) class to load the contents of RecyclerView and an interface StateAdapterOnClickHandler to handle onClicks of the items inside RecyclerView but clicking on item doesn't work.
Here's my HomeFragment.java which extends Fragment. I am unable to figure out how to initialize the StateAdapterOnClickHandler and where to call its method onItemClick()
public class HomeFragment extends Fragment implements StateAdapter.StateAdapterOnClickHandler {
private View homeFragment;
private RecyclerView recyclerView;
private StateAdapter stateAdapter;
private List<String> states;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public static HomeFragment newInstance() {
HomeFragment homeFragment = new HomeFragment();
return homeFragment;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
homeFragment = inflater.inflate(R.layout.fragment_home,container,false);
recyclerView = homeFragment.findViewById(R.id.recyclerview_states);
states = Arrays.asList(getResources().getStringArray(R.array.india_states));
LinearLayoutManager layoutManager = new LinearLayoutManager(container.getContext(),LinearLayoutManager.VERTICAL,false);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
stateAdapter = new StateAdapter();
stateAdapter.setStateNames(states);
recyclerView.setAdapter(stateAdapter);
return homeFragment;
}
#Override
public void onItemClick(String state) {
Intent intent = new Intent(getActivity(),DetailActivity.class);
intent.putExtra("State",state);
startActivity(intent);
}
}
Here's my StateAdapter class
public class StateAdapter extends RecyclerView.Adapter<StateAdapter.StateViewHolder> {
private List<String> stateNames;
private final StateAdapterOnClickHandler stateAdapterOnClickHandler;
StateAdapter(StateAdapterOnClickHandler stateAdapterOnClickHandler){
this.stateAdapterOnClickHandler = stateAdapterOnClickHandler;
}
public interface StateAdapterOnClickHandler {
void onItemClick(String state);
}
#Override
public StateAdapter.StateViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
int idForListItem = R.layout.state_layout;
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(idForListItem,parent,false);
return new StateViewHolder(view);
}
#Override
public void onBindViewHolder(StateAdapter.StateViewHolder holder, int position) {
String stateName = stateNames.get(position);
holder.mTextView.setText(stateName);
}
#Override
public int getItemCount() {
if(stateNames == null) return 0;
return stateNames.size();
}
public class StateViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public final TextView mTextView;
public StateViewHolder(View itemView) {
super(itemView);
mTextView = (TextView) itemView.findViewById(R.id.tv_state_name);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
int clickedPostion = getAdapterPosition();
String state = stateNames.get(clickedPostion);
stateAdapterOnClickHandler.onItemClick(state);
}
}
public void setStateNames(List<String> states) {
stateNames = states;
notifyDataSetChanged();
}
}
Please, tell me what's wrong with the code while handling the clicks. I am new to Android Programming and this is first time I am working with Fragments. Thanks for helping.

How to notify an Adapter in a Fragment from Activity?

I have a TabLayout in mainActivity with some tabs, tabs are Fragments.
And on each Fragment I have a ReclerView.
From the main Activity I want to call notifyDatasetChange() on my Adapter in my Fragment for updating the UI.
But how to notify it?
myMainActivity:
final ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
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) {
}
});
One of myTabFragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
RecyclerView v = (RecyclerView) inflater.inflate(R.layout.tab_fragment_1, container, false);
SetupRecycleView(v);
return v;
}
void SetupRecycleView(RecyclerView recList) {
recList.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(G.context);
llm.setOrientation(LinearLayoutManager.VERTICAL);
//recList.setLayoutManager(new GridLayoutManager(G.context, 2));
recList.setLayoutManager(llm);
List<StructGhaza> ghazas = StructGhaza.getAllGhaza("2");
CardAdapter adapter = new CardAdapter(ghazas);
recList.setAdapter(adapter);
}
The CardAdapter:
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.GhazaViewHolder> {
private List<StructGhaza> ghazas;
public CardAdapter(List<StructGhaza> ghazaList) {
this.ghazas = ghazaList;
}
#Override
public int getItemCount() {
return ghazas.size();
}
#Override
public void onBindViewHolder(GhazaViewHolder ghazaViewHolder, int position) {
StructGhaza ghaza = ghazas.get(position);
ghazaViewHolder.vTitle.setText(ghaza.Name);
//----Load Image
Glide.with(G.context)
.load(ghaza.Aks)
.placeholder(R.drawable.loading_spinner)
.crossFade()
.into(ghazaViewHolder.itemImg);
//---------------
ghazaViewHolder.Rate.setRating(ghaza.Star);
ghazaViewHolder.sampleLayout.setHoverView(ghazaViewHolder.hover);
ghazaViewHolder.sampleLayout.addChildAppearAnimator(ghazaViewHolder.hover, R.id.tvHover, Techniques.FlipInX);
ghazaViewHolder.sampleLayout.addChildDisappearAnimator(ghazaViewHolder.hover, R.id.tvHover, Techniques.FlipOutX);
}
#Override
public GhazaViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.fragment_carditem, viewGroup, false);
return new GhazaViewHolder(itemView);
}
public static class GhazaViewHolder extends RecyclerView.ViewHolder {
protected TextView vTitle;
protected BlurLayout sampleLayout;
protected View hover;
protected ImageView itemImg;
protected RatingBar Rate;
public GhazaViewHolder(View v) {
super(v);
sampleLayout = (BlurLayout) v.findViewById(R.id.sample);
itemImg = (ImageView) v.findViewById(R.id.item_img);
hover = LayoutInflater.from(G.context).inflate(R.layout.fragment_hover, null);
vTitle = (TextView) v.findViewById(R.id.titleTv);
Rate = (RatingBar) v.findViewById(R.id.rating);
}
}
}
I would recommend putting a public method in your Fragment and then calling from the Activity. See "Deliver a Message to a Fragment" here:
http://developer.android.com/training/basics/fragments/communicating.html
In your Activity, just do the following to make sure the Fragment method can be called:
TabFragment tabFragment = (TabFragment) getSupportFragmentManager().findFragmentByTag(TAB_FRAGMENT_TAG);
// Check if the tab fragment is available
if (tabFragment != null) {
// Call your method in the TabFragment
articleFrag.yourMethod();
}
And then in the Fragment, have the following method:
public void yourMethod() {
recyclerViewAdapter.notifyDataSetChanged();
}
you create updateListview() in your fragment like
public void updateListView() {
yourAdapter.notifyDataSetChanged();
}
and updateListview() in your Activity like
updateListView();

Animating shared element transitions using android fragments seems to be a nightmare

Animating a shared element transition between two fragments seems to be a pain. I have it working elsewhere in my app with the exact same code.
here is my on click code inside a recyclerview adapter:
private void click(int position, ItemViewHolder holder){
final ItemDescription itemDescription = itemListFiltered.get(position);
FragmentManager fm = mContext.getSupportFragmentManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && itemDescription != null) {
//animate transition
TransitionSet transitionSet = new TransitionSet();
transitionSet.addTransition(new ChangeTransform());
transitionSet.addTransition(new ChangeBounds());
transitionSet.addTransition(new ChangeImageTransform());
transitionSet.setDuration(300);
//set up fragment
ScanDetailsFragment frag = ScanDetailsFragment.newInstance(itemDescription, holder.productImage.getTransitionName());
frag.setEnterTransition(transitionSet);
frag.setExitTransition(transitionSet);
fm.beginTransaction().replace(R.id.fragment_container, frag)
.addSharedElement(holder.productImage, "product_image")
.addToBackStack("item details")
.commit();
}else {
ScanDetailsFragment itemDetails = ScanDetailsFragment.newInstance(itemDescription);
fm.beginTransaction().replace(R.id.fragment_container, itemDetails).addToBackStack("item details").commit();
}
I just can't seem to get this animation to work.
here is how I did it
Fragment with the RecyclerView
public class DogFragment extends Fragment implements DogAdapter.OnItemCLickListener {
public static final String TAG = "DogFragment";
#InjectView(R.id.recyclerview_dog)
RecyclerView mRecyclerView;
DogAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
final View v = inflater.inflate(R.layout.fragment_dog, container, false);
ButterKnife.inject(this, v);
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final List<Integer> images = new ArrayList<>();
images.add(R.drawable.dog1);
images.add(R.drawable.dog2);
images.add(R.drawable.dog3);
final RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getActivity(), 1);
mRecyclerView.setLayoutManager(layoutManager);
adapter = new DogAdapter(images);
adapter.setOnItemClickListener(this);
mRecyclerView.setAdapter(adapter);
}
public String getTAG() {
return TAG;
}
#Override
public void onItemClick(int position, int imageRefId, ImageView imageView) {
SingleDogFragment fragment = SingleDogFragment.getInstance(getActivity(), adapter.getImageTransitionName(getActivity(), position), imageRefId);
getFragmentManager().beginTransaction()
.addSharedElement(imageView, adapter.getImageTransitionName(getActivity(), position))
.replace(R.id.container, fragment, fragment.getTag())
.addToBackStack(null)
.commit();
}
}
The adapter
public class DogAdapter extends RecyclerView.Adapter<DogAdapter.ViewHolder> {
private OnItemCLickListener mItemClickListener;
private List<Integer> mImages;
public DogAdapter(List<Integer> images) {
mImages = images;
}
public int getItem(int position) {
return mImages.get(position);
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.gridview_element_dog, viewGroup, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
viewHolder.mImageView.setImageResource(getItem(i));
viewHolder.mImageView.setTransitionName(
getImageTransitionName(viewHolder.mImageView.getContext(), i)
);
}
public String getImageTransitionName(Context context, int position) {
return context.getString(R.string.dog_transition_name) + position;
}
#Override
public int getItemCount() {
return mImages.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
#InjectView(R.id.dog_imageview)
ImageView mImageView;
public ViewHolder(final View view) {
super(view);
ButterKnife.inject(this, view);
view.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (mItemClickListener != null) {
mItemClickListener.onItemClick(getPosition(), getItem(getPosition()), mImageView);
}
}
}
public interface OnItemCLickListener {
public void onItemClick(int position, int refId, ImageView imageView);
}
public void setOnItemClickListener(final OnItemCLickListener mItemClickListener) {
this.mItemClickListener = mItemClickListener;
}
}
Target fragment
public class SingleDogFragment extends Fragment {
private static final String ARG_TRANSITION_NAME = "ARG_TRANSITION_NAME";
private static final String ARG_IMAGE_REF_ID = "ARG_IMAGE_REF_ID";
#InjectView(R.id.single_dog_imageview)
ImageView mDogImage;
public static SingleDogFragment getInstance(Context context, String transitionName, int imageRefId) {
SingleDogFragment fragment = new SingleDogFragment();
Bundle bundle = new Bundle();
bundle.putString(ARG_TRANSITION_NAME, transitionName);
bundle.putInt(ARG_IMAGE_REF_ID, imageRefId);
fragment.setSharedElementEnterTransition(TransitionInflater.from(context).inflateTransition(R.transition.change_transform));
fragment.setEnterTransition(TransitionInflater.from(context).inflateTransition(R.transition.slide_left));
fragment.setExitTransition(TransitionInflater.from(context).inflateTransition(R.transition.slide_left));
fragment.setArguments(bundle);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View v = inflater.inflate(R.layout.fragment_single_dog, container, false);
ButterKnife.inject(this, v);
mDogImage.setTransitionName(getArguments().getString(ARG_TRANSITION_NAME));
mDogImage.setImageResource(getArguments().getInt(ARG_IMAGE_REF_ID));
return v;
}
}
change_transform.xml
<?xml version="1.0" encoding="utf-8"?>
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
<targets>
<target android:excludeId="#android:id/statusBarBackground"/>
<target android:excludeId="#android:id/navigationBarBackground"/> <!-- exclude the status bar and the navigation bar of the animation -->
</targets>
<changeBounds/>
<changeTransform/>
</transitionSet>
And here is what it look like
Hope I could help !

Categories