Hello i have fragment and inside it viewpager with 2 tabs i need when i press button in the parent fragment refresh the child fragment inside viewpage
i used this for refresh the same fragment and it's working fine
FragmentTransaction ft1 = getFragmentManager().beginTransaction();
ft1.detach(this).attach(this).commit();
i tried the same and instead of this i put the childfragment.newInstance() put still not refreshed so any suggestion ??
Create a interface in your child fragment.
RefreshInterface
create a method in child fragment to assign a object to RefreshInterface.
intialiseRefreshInterface(RefreshInterface refreshInterface)
call the interface method inside the refresh click.
implement the child fragment Interface in Parent fragment and override the method.
before commit() the child fragment from parent fragment
1. create a object to child object
2. call the intialiseRefreshInterface(RefreshInterface refreshInterface)
3. commit() the child fragment.
your child fragment will refresh [ child fragment will be popBackStack() and re-create.
Below example code will help you in achieve your requirement
Parent Fragment
parentFragment.java
public class OneFragment extends Fragment implements TwoFragment.RefreshInterface {
TwoFragment child;
Button launch;
FrameLayout containerLayout;
// child fragment interface object
TwoFragment.RefreshInterface refreshInterface;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
launch = view.findViewById(R.id.fragment_launch);
containerLayout = view.findViewById(R.id.fragment_container);
// RefreshInterface. this is mandatory
refreshInterface = this;
launch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
launchChildFragment();
}
});
return view;
}
#Override
public void refresh_fragment() {
// as we added the child fragment to backstack. we will remove the fragment from backstack and add again
if (getActivity().getSupportFragmentManager().getBackStackEntryCount() > 0){
getActivity().getSupportFragmentManager().popBackStack();
}
launchChildFragment();
}
private void launchChildFragment(){
// creating the object for child fragment
child = new TwoFragment();
// intialising the RefreshInterface object
child.intialiseRefreshInterface(refreshInterface);
// calling the child fragment
getActivity().getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, child).addToBackStack(null).commit();
}
}
fragment_parent.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.myapplication.OneFragment">
<Button
android:layout_centerHorizontal="true"
android:id="#+id/fragment_launch"
android:text="Launch child fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#+id/fragment_container"
android:layout_below="#id/fragment_launch"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
childFragment
public class TwoFragment extends Fragment {
Button refershBtn;
RefreshInterface refreshInterface;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_two, container, false);
refershBtn = view.findViewById(R.id.btn_refersh);
refershBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
refreshInterface.refresh_fragment();
}
});
return view;
}
public void intialiseRefreshInterface(RefreshInterface refreshInterface){
this.refreshInterface = refreshInterface;
}
public interface RefreshInterface {
void refresh_fragment();
}
}
fragment_child.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.outthinking.myapplication.TwoFragment">
<!-- TODO: Update blank fragment layout -->
<EditText
android:id="#+id/refersh_tv"
android:textSize="24sp"
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="REFRESH DATA" />
<Button
android:id="#+id/btn_refersh"
android:text="refersh"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Related
I have a ViewPager with 12 fragments. I need to get data from API and update the fragments when scrolling. How do I update the contents of fragment, with only one fragment updating for 12 pages?
My fragment layout fragment_item:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/surface">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/khaire"
android:gravity="center"
android:text="Overal Working Hour"
android:textColor="#color/colorPrimary"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:gravity="center"
android:padding="15dp"
android:text="January"
android:textColor="#color/white"
android:textSize="20sp"
android:textStyle="bold" />
My main activity code:
public class MainActivity extends AppCompatActivity {
private static final int num_pages = 12;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPager = findViewById(R.id.pager);
mPagerAdapter = new ScreenAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
private class ScreenAdapter extends FragmentStatePagerAdapter {
public ScreenAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
return new SliderFragment();
}
#Override
public int getCount() {
return num_pages;
}
}
My SlideFragment class:
public class SliderFragment extends Fragment {
Toolbar toolbar;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_item,container,false);
return rootView;
}
Now I want to have same fragment while scrolling the page but with diff texts how could I do that... And if possible use RecyclerView also.
You could create a new Class (UpdatingFragment) with public abstract updateView(); that extends Fragment.
Create your SliderFragment by extending UpdatingFragment instead of Fragment
In the SliderFragment in onCreateView() at the end call updateView().
Write an overwrite method in your SliderFragment for what you need to update (anything you want to show on the UI)
Adjust your screenAdapter to return and take UpdatingFragment’s instead of Fragments.
Now whenever your fragments onCreateView gets called all their UI data gets updated, and if you create a list of SliderFragments and pass that into your adapter instead of creating new ones, you can just run a for loop over that list and call updateView() for each fragment.
I use fragments to display multiple views in one activity. I have 2 framelayouts in one linearlayout. The onCreate and onCreateView of the fragment gets called but the view of the fragment is not displayed. Is what i'm trying to do not possible? Or is there a way to fix issue?
Layout of the activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".view.activity.StandardFlowActivity">
<FrameLayout
android:id="#+id/standardFlowDownloadContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<FrameLayout
android:id="#+id/standardFlowBaseContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
Layout of the fragment
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.sennevervaecke.crossexperience.view.fragment.WedstrijdFragment">
<ListView
android:id="#+id/wedstrijdListView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
Fragment Class
public class WedstrijdFragment extends Fragment implements AdapterView.OnItemClickListener{
private ArrayList<Wedstrijd> wedstrijden;
private WedstrijdFragmentCom communication;
public WedstrijdFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
Log.e("wedstrijdFragment", "onCreate is called");
super.onCreate(savedInstanceState);
wedstrijden = LocalDB.getWedstrijden();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.e("wedstrijdFragment", "onCreateView is called");
View view = inflater.inflate(R.layout.fragment_wedstrijd, container, false);
ListView listView = view.findViewById(R.id.wedstrijdListView);
WedstrijdAdapter adapter = new WedstrijdAdapter(getContext(), wedstrijden);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
return view;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
communication = (WedstrijdFragmentCom) activity;
} catch (ClassCastException e){
e.printStackTrace();
}
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
communication.onWedstrijdItemClick(wedstrijden.get(i));
}
}
Code in the onCreate of the activity to add the fragment:
wedstrijdFragment = new WedstrijdFragment();
getSupportFragmentManager().beginTransaction().add(R.id.standardFlowBaseContainer, wedstrijdFragment, "wedstrijd").commit();
Thanks in advance!
Your first layout standardFlowDownloadContainer width and height is match_parent , so the standardFlowBaseContainer FrameLayout is out of the screen. you can change your standardFlowDownloadContainer's height to 20dp and run your project , you will see your Fragment content.
I want to change the top toolbar according to clicked fragment but it seems not working for me.
What I want is when I click the fragment, the top toolbar should change. For example, add a Add button and change the title of the toolbar for Create Fragment.
Fragment.java
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
setHasOptionsMenu(true);
View v = inflater.inflate(R.layout.fragment_event_created, container, false);
Toolbar toolbar123 = (Toolbar) v.findViewById(R.id.toolbar123);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar123);
//((MainActivity) getActivity()).getSupportActionBar().hide();
return v;
}
Fragment XML
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.rick.check_in.ui.EventCreatedFragment">
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar123"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#131313"
android:minHeight="?attr/actionBarSize">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="#00aaaaaa">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000"
android:text="Delete"
android:textColor="#fff"
android:textSize="16dp" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello_blank_fragment" />
</FrameLayout>
You can do it like this way. onAttach(Activity) called once the fragment is associated with its activity.
public class YourFragment extends Fragment {
private MainActivity mainActivity;
private Toolbar toolbar123;
public YourFragment() {
// Required empty public constructor
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mainActivity = (MainActivity)activity;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_event_created, container, false);
toolbar123 = (Toolbar)v.findViewById(R.id.toolbar123);
setupToolbar();
return v;
}
private void setupToolbar(){
toolbar123.setTitle(getString("Your Fragment Title"));
mainActivity.setSupportActionBar(toolbar123);
}
}
I'm trying to change TextView in my fragment layout, however I'm getting null value when calling findViewById no matter what.
Here's my fragment layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.kubacm.myweather.ShowMeWeather">
<TextView
android:id="#+id/city_field"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/white" />
</RelativeLayout>
Calling fragment from main activity:
#Override public void showWeather(JSONObject data){
Bundle bundle = new Bundle();
bundle.putString("data",data.toString());
ShowMeWeather wf = new ShowMeWeather();
wf.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.activity_main,wf).addToBackStack(null).commit();
}
and in fragment:
public class ShowMeWeather extends Fragment {
Typeface weatherFont;
TextView cityField;
public ShowMeWeather() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_show_me_weather, container, false);
cityField = (TextView)rootView.findViewById(R.id.city_field);
return rootView;
}
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
weatherFont = Typeface.createFromAsset(getActivity().getAssets(),"fonts/weather.ttf");
renderWeather();
}
public void renderWeather(){
try{
cityField.setText("Here should go my text");
}catch (Exception e){
Log.e("Simple Weather","there was an errror");
}
}
}
I have tried cleaning and rebuilding my project in Android Studio and still keep getting NullPointerException when I try cityField.SetText
onCreateView method is called after onCreate method, so cityField is not yet initialized
Check fragment lifecycle here here
Call your renderWeather method after onCreateView is completed
I have created a layout that contains a listView, the view is populated with multiple similar elements in the onCreateView function from a Fragment, the view is populated in the CustomAdapter extends BaseAdapter. In each element of the listView I have a button for which I want to implement a onClickListener. When the button is clicked I want to switch to another view, something similar to viewing comments on the GooglePlus mobile app.
I have tried doing so this way in the CustomAdapter:
public void setOnClickComment(View view) {
final ImageButton btn = (ImageButton)view.findViewById(R.id.addComment);
if (btn != null) {
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ONCLICK", "I just clicked this shit" + btn.getTag());
manager.beginTransaction()
.replace(R.id.feedLayout, new CommentFragment()).commit();
}
});
} else {
Log.d("CLICK", "ESTE NULL");
}
}
The manager is a FragmentManager and it's passed from the fragment to the CustomAdapter constructor. I can see the Log in LogCat but that's it nothing else happens, the tag of the button is the position of the element.
This is the layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="#+id/feedLayout"
>
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
</LinearLayout>
And in the fragment I inflate the above layout:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LinearLayout linear = (LinearLayout) inflater.inflate(R.layout.swipe_screen_left, container, false);
view = (ListView) linear.getChildAt(0);
adapter = new CustomAdapter(getActivity(), data, getFragmentManager());
view.setAdapter(adapter);
return linear;
}