Sending Data From Fragment to Fragment Using TabLayout - java

I’ve been having difficulties sending data from one fragment to another when utilizing a TabLayout. I’ve read copious amounts of online posts pertaining to how to do this and have had no success when implementing them myself.
I have four fragments set up in my application. I'm trying to send data gathered from an EditText element in the fourth fragment to the TextView element of the second fragment.
I don't get any errors, but the text does not show up in the second fragment. I don't know how to proceed from here. Thanks!
Fourth Fragment (sending Fragment)
public class settings extends Fragment {
SendMessage SM;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.setl, container, false);
return rootView;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button btnPassData = (Button) view.findViewById(R.id.btnPassData);
final EditText inData = (EditText) view.findViewById(R.id.inMessage);
btnPassData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SM.sendData(inData.getText().toString().trim());
}
});
}
public interface SendMessage {
void sendData(String message);
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
try {
SM = (SendMessage) getActivity();
} catch (ClassCastException e) {
throw new ClassCastException("Error in retrieving data. Please try again");
}
}
}
Second Fragment (receiving fragment)
public class tt extends Fragment {
TextView txtData;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.ttl, container, false);
return rootView;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
txtData = (TextView)view.findViewById(R.id.txtData);
}
public void displayReceivedData(String message) {
txtData.setText("Data received: "+ message);
}
}
Main Activity
public class MainActivity extends AppCompatActivity implements settings.SendMessage {
#Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_test);
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
}
#Override
public void sendData(String message) {
String tag = "android:switcher:" + R.id.view_pager + ":" + 1;
tt f = (tt) getSupportFragmentManager().findFragmentByTag(tag);
f.displayReceivedData(message);
}
}

Something might be breaking in other part of your app.
Below is code of your working example.
class MainActivity
public class MainActivity extends AppCompatActivity implements Setting.SendMessage {
#Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(),this );
ViewPager viewPager = findViewById(R.id.viewpager);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = findViewById(R.id.sliding_tabs);
tabs.setupWithViewPager(viewPager);
}
#Override
public void sendData(String message) {
String tag = "android:switcher:" + R.id.viewpager + ":" + 1;
tt f = (tt) getSupportFragmentManager().findFragmentByTag(tag);
f.displayReceivedData(message);
}
}
class Setting
public class Setting extends Fragment {
SendMessage SM;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.setl, container, false);
return rootView;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button btnPassData = (Button) view.findViewById(R.id.btnPassData);
final EditText inData = (EditText) view.findViewById(R.id.inMessage);
btnPassData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SM.sendData(inData.getText().toString().trim());
}
});
}
public interface SendMessage {
void sendData(String message);
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
try {
SM = (SendMessage) getActivity();
} catch (ClassCastException e) {
throw new ClassCastException("Error in retrieving data. Please try again");
}
}
}
class tt
public class tt extends Fragment {
TextView txtData;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.tt, container, false);
return rootView;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
txtData = (TextView)view.findViewById(R.id.txtData);
}
public void displayReceivedData(String message) {
txtData.setText("Data received: "+ message);
}
}
class SectionsPagerAdapter
public class SectionsPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 2;
private String tabTitles[] = new String[] { "Tab1", "Tab2"};
private Context context;
public SectionsPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
#Override
public int getCount() {
return PAGE_COUNT;
}
#Override
public Fragment getItem(int position) {
if(position==0){
return new Setting();
}
else
return new tt();
}
#Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
}

Related

Button click listener in a separate fragment not working?

I am trying to add a button clicker listener in my fragment but it doesn't seem to work.
My main fragment is created to show tabs of two other fragments. Now, this tabs fragment has a button in the toolbar to filter content. This filter button's listener and method are in my two other fragments. The issue is, when clicking on the button, it does nothing.
Tabs Fragment:
public class LibraryFragment extends Fragment implements ViewPager.OnPageChangeListener {
private View view;
private SectionsPagerAdapterLib PageAdapter;
public ViewPager viewPager;
private ImageView filterBtn;
private LibraryFragment.OnItemClickListener mOnItemClickListener;
public interface OnItemClickListener {
void onItemClick(View view);
}
public void setOnItemClickListener(LibraryFragment.OnItemClickListener mItemClickListener) {
this.mOnItemClickListener = mItemClickListener;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
this.view = inflater.inflate(R.layout.fragment_library_tabs, container, false);
TabLayout tabLayout = view.findViewById(R.id.fav_list_tabs);
filterBtn = view.findViewById(R.id.filterBtn);
viewPager = view.findViewById(R.id.fav_list_tabs_container);
filterBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(v);
}
}
});
PageAdapter = new SectionsPagerAdapterLib(getFragmentManager(), getActivity());
viewPager.setAdapter(PageAdapter);
viewPager.setOffscreenPageLimit(2);
viewPager.addOnPageChangeListener(this);
tabLayout.setupWithViewPager(viewPager);
getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
getActivity().getWindow().setStatusBarColor(getActivity().getResources().getColor(R.color.mWindowColorHeaders));
return view;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
Fragment fragment = PageAdapter.getFragment(position);
if (fragment != null) {
fragment.onResume();
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
}
Fragment which is supposed to work when the button is clicked:
public class ContentFragment extends Fragment implements LibraryFragment.OnItemClickListener {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_data, null);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// find my views, etc
}
#Override
public void onItemClick(View view) {
//populate filter list
if (Constants.genreList != null){
list.add(0, "All Filters");
for (int i = 0; i < Constants.list.size(); i++) {
list.add((i + 1), Constants.list.get(i).getName());
}
}
final String[] listArray = new String[list.size()];
for (int i = 0; i < list.size(); i++) {
listArray[i] = list.get(i);
}
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Select Filter");
builder.setItems(genreArray, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
// ((TextView) v).setText(genreArray[i]);
selected_list.setText(listArray[i]);
if (i == 0) {
getData(1);
}
dialogInterface.dismiss();
}
});
builder.show();
}
}
Any help will be appreciated.
try this in a fragment
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{ //change fragment_video to where your button is located//
final View v2 = inflater.inflate(R.layout.fragment_video,container,false);
//start stream Button
Button startButton = (Button)v2.findViewById(R.id.buttonStart);
startButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//what your button is going to do
}
});
return v2;
}
As fragments are handled by Activity ,Fragments are still not depend on each other and can be used in one or two pane mode. Activity should handle listeners of both fragments.

android fragment with recycler view communication

i have a ListFragment which contain a recycler view and i want to make it when an item is clicked the DetailFragmentdisplay beside it in a multi-pane layout i know how to create a multi-pane layout but i don't know how to send data between fragments because i don't where to initialize the click to send data, where should i initialize the on click so it passes data from ListFragment to DetailFragment and display it ? * note: i am getting my data from a dialog fragment
MainActivity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_list_container);
if (fragment == null) {
fragment = new ListFragment();
fm.beginTransaction()
.add(R.id.fragment_list_container, fragment)
.commit();
}
}
}
ListFragment
public class ListFragment extends Fragment implements MyDialog.OnInputSelected {
#Override
public void sendInput(String title, Uri imageUri) {
Wallpaper wallpaper = new Wallpaper(title, imageUri);
wallpaperList.add(wallpaper);
passWallpaper.passWallpaperMethod(wallpaper);
}
private List<Wallpaper> wallpaperList;
private RecyclerViewAdapter recyclerViewAdapter;
private RecyclerView recyclerView;
private FloatingActionButton fabOpenDialog;
public ListFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_list, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
fabOpenDialog = view.findViewById(R.id.fab_open_dialog);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
fabOpenDialog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MyDialog dialog = new MyDialog();
dialog.setTargetFragment(ListFragment.this, 1);
dialog.show(getFragmentManager(), "MyDialog");
}
});
wallpaperList = new ArrayList<>();
recyclerView = getView().findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerViewAdapter = new RecyclerViewAdapter(wallpaperList, getActivity());
recyclerView.setAdapter(recyclerViewAdapter);
}
}
RecyclerViewAdapter
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private List<Wallpaper> wallpaperList;
private Context context;
public RecyclerViewAdapter(List<Wallpaper> wallpaperList, Context context) {
this.wallpaperList = wallpaperList;
this.context = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycler_row, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
Wallpaper wallpaper = wallpaperList.get(position);
holder.iv.setImageURI(wallpaper.getImageUrl());
holder.tvTitle.setText(wallpaper.getTitle());
}
#Override
public int getItemCount() {
return wallpaperList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public View view;
public ImageView iv;
public TextView tvTitle;
public ViewHolder(#NonNull View itemView) {
super(itemView);
view = itemView;
iv = itemView.findViewById(R.id.row_iv);
tvTitle = itemView.findViewById(R.id.row_tv_title);
}
}
}
DetailActivity
public class DetailsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.details_container);
if (fragment == null) {
fragment = new DetailsFragment();
fm.beginTransaction()
.add(R.id.details_container, fragment)
.commit();
}
}
}
DetailsFrgament
public class DetailsFragment extends Fragment {
private TextView tvTitle;
private ImageView ivWallpaper;
public DetailsFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_details, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
tvTitle = view.findViewById(R.id.fragment_details_tv_title);
ivWallpaper = view.findViewById(R.id.details_fragment_iv_wallpaper);
}
DialogFragment
public class MyDialog extends DialogFragment implements View.OnClickListener {
public interface OnInputSelected {
void sendInput(String title, Uri imageUri);
}
public OnInputSelected onInputSelected;
#Override
public void onAttach(#NonNull Context context) {
super.onAttach(context);
try {
// "getTargetFragment()" instead of "getActivity()"
onInputSelected = (OnInputSelected) getTargetFragment();
} catch (ClassCastException e) {
Log.d("DialogFragment", "Class Cast Exception: " + e.getMessage());
}
}
TextView yes, no;
private EditText etTitle;
private ImageView imageView;
private Uri imageUri;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
setCancelable(false);
return inflater.inflate(R.layout.dialog_add_wallpaper, null);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
yes = view.findViewById(R.id.dialog_tv_ok);
no = view.findViewById(R.id.dialog_tv_cancel);
imageView = view.findViewById(R.id.dialog_iv_add_image);
imageView.setOnClickListener(this);
yes.setOnClickListener(this);
no.setOnClickListener(this);
etTitle = view.findViewById(R.id.et_add_title);
}
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setTitle("Title testing");
return dialog;
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.dialog_tv_ok) {
onInputSelected.sendInput(etTitle.getText().toString().trim(), imageUri);
dismiss();
} else if (v.getId() == R.id.dialog_tv_cancel) {
onInputSelected.sendInput("got canceled", null);
dismiss();
} else {
Intent imageIntent = new Intent(Intent.ACTION_GET_CONTENT);
imageIntent.setType("image/*");
startActivityForResult(imageIntent, 1);
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == Activity.RESULT_OK && data != null &&
data.getData() != null) {
imageUri = data.getData();
imageView.setImageURI(imageUri);
}
}
}
You can initialize clickListener in onBindViewHolder in RecyclerViewAdapter and inside something like this:
holder.iv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((MainActivity) view.getContext()).getFragmentManager().beginTransaction()
.replace(R.id.fragment_list_container, new DetailsFrgament())
.commit();
}
});
For transfering data you can use Bundle

First Click in button does not get the text in the EditText object

The problem is that when i click in the button the first time no text is in the EditText object, in the second click it works but does not replace the frame.
I Have one activity with a container for a fragment that has a editText and a button, when click the button i replace the fragment in the container with another fragment to display the text in the edit text.
I'm following this tutorial:
https://www.youtube.com/watch?v=OLbsiE42JvE&list=PLshdtb5UWjSrOJfpFOE-u55s3SnY2EO9v&index=14
i'm in video "Android tutorial (2018) - 14 - Fragment to Fragment Communication".
The diference from this tutorial from my app is that my activity is the third in the same project, and i'm working in that activity as it was my main.
public class FinalActivity extends AppCompatActivity implements HeadLineFragment.OnMessageListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_final);
if(findViewById(R.id.final_activity_fragment)!= null) {
if (savedInstanceState != null) {
return;
}
HeadLineFragment headLineFragment = new HeadLineFragment();
getSupportFragmentManager().beginTransaction().add(R.id.final_activity_fragment, headLineFragment, null).commit();
}
}
#Override
public void onMessageSend(String message) {
ArticleFragment articleFragment = new ArticleFragment();
Bundle bundle = new Bundle();
bundle.putString("message",message);
articleFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.final_activity_fragment, articleFragment,null).addToBackStack(null).commit();
}
}
public class HeadLineFragment extends Fragment {
OnMessageListener onMessageListener;
private Button button;
private EditText editText;
public interface OnMessageListener{
public void onMessageSend(String message);
}
public HeadLineFragment(){
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.headline_fragment, container, false);
button = view.findViewById(R.id.bfrag);
editText = view.findViewById(R.id.editText21);
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
String message = editText.getText().toString();
String s = editText.getText().toString();
onMessageListener.onMessageSend(message);
}
});
return view;
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
Activity activity = (Activity) context;
try {
onMessageListener = (OnMessageListener) activity;
}catch (ClassCastException e){
throw new ClassCastException(activity.toString() + " must implement OnMessageListener....");
}
}
#Override
public void onResume() {
super.onResume();
editText.setText("");
}
}
public class ArticleFragment extends Fragment {
private TextView textView;
public ArticleFragment(){}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(
R.layout.article_fragment,container, false);
textView = view.findViewById(R.id.txt_message_display);
Bundle bundle = getArguments();
String message = bundle.getString("message");
textView.setText(message);
return view;
}
}
I expect that the functionality has the same behavior as in the video of the tutorial
I eventually solved my own problem, the layout of the Activity had a fragment instead of a frame layout. By changing that the problem was solved.

viewpager using JSON

i have written code for image slider but i use drawable images..
Actually i want to know, How to use online images in slideshow instead of
drawables...please help me with complete code as i am new to android..
public class DealsFragment extends Fragment {
private PagerAdapter mPagerAdapter;
public static DealsFragment newInstance() {
DealsFragment fragment = new DealsFragment();
return fragment;
}
public DealsFragment() {
// 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_deals, container, false);
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(getActivity(), ImageOneDeals.class.getName()));
fragments.add(Fragment.instantiate(getActivity(), ImageTwoDeals.class.getName()));
fragments.add(Fragment.instantiate(getActivity(), ImageThreeDeals.class.getName()));
mPagerAdapter = new ImagePagerAdapter(getChildFragmentManager(), fragments);
pager.setAdapter(mPagerAdapter);
return view;
}
}
ImageOneDeals.java
public class ImageOneDeals extends Fragment {
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
if(container==null){
return null;
}
return (LinearLayout) inflater.inflate(R.layout.image1_deals,container,false);
}
}
ImagePagerAdapter.java
public class ImagePagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public ImagePagerAdapter(FragmentManager fragmentManager, List<Fragment> fragments){
super(fragmentManager);
this.fragments=fragments;
}
#Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
#Override
public int getCount() {
return this.fragments.size();
}
}
Your Deal Fragment:-
public class DealFragment extends Fragment {
private View rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (rootView == null) {
rootView = inflater.inflate(R.layout.slide_view_pager, container, false);
new GetUserImage().execute(); //run your webservices here
}
return rootView;
}
public class GetUserImage extends AsyncTask<String, String, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(getActivity());
pd.setCancelable(true);
pd.setMessage("Loading...");
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.show();
}
#Override
protected String doInBackground(String... params) {
postUserImageData();
return null;
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//save your data in a list
// set Adapter after you get data from webservices
mPager = (ViewPager) rootView.findViewById(R.id.slidePager);
ImagePagerAdapter viewImagePagerAdapter = new ImagePagerAdapter(getChildFragmentManager(), TotalCount, imagearr);
mPager.setAdapter(viewImagePagerAdapter);
mPager.getCurrentItem();
}
public void postUserImageData() {
}
}
}
your Adapter:-
public class ImagePagerAdapter extends FragmentPagerAdapter {
private int numOfItemSize;
List<UserImage> arr1;
public ImagePagerAdapter (FragmentManager childFragmentManager, int num_pages, List<UserImage> customMessageListView) {
super(childFragmentManager);
this.numOfItemSize = num_pages;
this.arr1 = customMessageListView;
}
#Override
public Fragment getItem(int position) {
ImageOneDeals userImageDetailFragment = new ImageOneDeals ();
UserImage sc = arr1.get(position);
Bundle bundle = new Bundle();
bundle.putString("ProfilePic", sc.ProfilePic);
bundle.putString("Work", sc.Work);
bundle.putString("BirthTime", sc.BirthTime);
bundle.putString("Message", sc.Message);
userImageDetailFragment.setArguments(bundle);
return userImageDetailFragment;
}
#Override
public int getCount() {
return numOfItemSize;
}
}
your ImageOneDeals :-
public class ImageOneDeals extends Fragment {
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
Title = getArguments().getString("ProfilePic");
BirthTime = getArguments().getString("BirthTime");
Work = getArguments().getString("Work");
Message = getArguments().getString("Message");
}
#Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
final View view = (View) inflater.inflate(R.layout.slide_user_image, container, false);
return view;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initialiseViews();
}
public void initialiseViews() {
ImageView imgDetails = (ImageView) getView().findViewById(R.id.UserimageView);
TextView usrname = (TextView) getView().findViewById(R.id.userName);
TextView usrwork = (TextView) getView().findViewById(R.id.userWork);
// imgLoader.DisplayImage(Title,imgDetails);
Picasso.with(getActivity())
.load(Title)
.placeholder( R.mipmap.add_photo)
.error( R.mipmap.add_photo)
.into(imgDetails);
usrname.setText(Message);
usrwork.setText(Work);
}
}

ViewPager with SherlockFragment always empty

Currently I have a tab host activity that contains SherlockFragment. In one of these tabs (Fragments), I want to implement a ViewPager that will show images (ImageView). I created a FragmentPagerAdapter and Fragment to add it in the ViewPager. But when I run the application, my ViewPager appears empty with no image in it.
Here are my classes:
BonusFragment.Java
public class BonusFragment extends SherlockFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getImages(); //Here I get the images for the viewpager
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
View view = inflater.inflate(R.layout.bonus_frag, container, false);
viewPager = (ViewPager) view.findViewById(R.id.pagr);
Q.ADAPTER = new BonusPagerAdapter(getActivity()
.getSupportFragmentManager(), myImagesList);
viewPager.setAdapter(Q.ADAPTER);
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
BonusPagerAdapter.Java
private ArrayList<Bitmap> items;
public BonusPagerAdapter(FragmentManager fm, ArrayList<Bitmap> items) {
super(fm);
this.items = items;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Fragment getItem(int position) {
Bitmap bitmap = items.get(position);
BonusImageFragment fragment = BonusImageFragment.newInstance("Glass 1");
fragment.setBitmap(bitmap);
return fragment;
}
#Override
public int getItemPosition(Object object) {
return PagerAdapter.POSITION_NONE;
}
#Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
}
BonusImageFragment.Java
public class BonusImageFragment extends SherlockFragment {
private Bitmap bitmap;
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
private static String frag = "";
public BonusImageFragment() {
}
public static final BonusImageFragment newInstance(String message)
{
frag = message;
BonusImageFragment f = new BonusImageFragment();
Bundle bdl = new Bundle(1);
bdl.putString(EXTRA_MESSAGE, message);
f.setArguments(bdl);
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.glass_item, container, false);
ImageView image = (ImageView) v.findViewById(R.id.glass_view);
image.setImageBitmap(bitmap);
return v;
}
public Bitmap getBitmap() {
return bitmap;
}
public void setBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
}
}
Now why does my ViewPager always appears empty?
Fixed it... I had to change "getActivity().getSupportFragmentManager()" in "BonusFragment.Java" with "getChildFragmentManager()"

Categories