Switch fragment within designated space - android

I have a layout, MainScreen.java which has following design.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/toolbar"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/tab_and_view" />
</LinearLayout>
toolbar will be the title of the app. tab_and_view will consist of a tabview and framelayout
Here's the layout file of tab_and_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.android.luftschlafer.engdictionary.Tab.SlidingTabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorAccent"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1" />
</LinearLayout>
I have made some tabs: Home,Search, Settings and About app. All of them will display their content in the ViewPager.
In Home page, there is only a ListView item.
<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=".Corr_Pages.HomePage">
<!-- TODO: Update blank fragment layout -->
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/home_function_list"/>
</RelativeLayout>
And codes of HomePage.java:
public class HomePage extends BaseFragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "Home";
private static final String ind_color="IndicatorColor";
private static final String div_color="DividerColor";
// TODO: Rename and change types of parameters
private String mParam1;
private int mParam2,mParam3;
private OnFragmentInteractionListener mListener;
public HomePage() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* #param tab_name Parameter 1.
* #return A new instance of fragment HomePage.
*/
// TODO: Rename and change types and number of parameters
public static HomePage newInstance(String tab_name, int indicatorColor,int
dividerColor,int iconResId) {
HomePage fragment = new HomePage();
fragment.setTitle(tab_name);
fragment.setIndicatorColor(indicatorColor);
fragment.setDividerColor(dividerColor);
fragment.setIconResId(iconResId);
//Pass out data
Bundle args = new Bundle();
args.putString(ARG_PARAM1, tab_name);
args.putInt(ind_color,indicatorColor);
args.putInt(div_color,dividerColor);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2=getArguments().getInt(ind_color);
mParam3=getArguments().getInt(div_color);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home_page, container, false);
}
//This method will be done by
#Override
public void onViewCreated(View v, final Bundle savedInstanceState){
//Initialize all visual elements here
final LayoutInflater inflater= LayoutInflater.from(getContext());//Test
ListView lv=(ListView) v.findViewById(R.id.home_function_list);
//Initialize the list items and their corresponding adapter
String[] SampleSelections=new String[] {"New things","Most common words","Recommendation from Wilson"};
ArrayAdapter<String> adapter= new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1, SampleSelections);
lv.setAdapter(adapter);
//Declare the events
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position){
//Toasts will be replaced by appropriate methods soon
case 0:
Toast.makeText(getContext(),"New Contents are coming",Toast.LENGTH_SHORT).show();
declare_new_content_page();
break;
case 1:
Toast.makeText(getContext(),"There are some most common words",Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(getContext(),"Wilson's recommendation",Toast.LENGTH_SHORT).show();
break;
default:
//Nothing is selected
break;
}
}
});
}
// 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;
}
#Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
//Handles the operation of new content page.
private void declare_new_content_page(){
//Declare widgets here
FragmentManager fm=getFragmentManager();
FragmentTransaction ftran=fm.beginTransaction();
ftran.replace(R.id.tab_and_view,NewContent.newInstance("NewContent"));
ftran.commit();
}
}
In the ListView of MainScreen.java, it has three choices: What's new, most common words and recommendation. Now if I press What's new, a new fragment should be displayed in the ViewPager with a name NewContent.java. I tried to do this by using following codes:
FragmentManager fm=getFragmentManager();
FragmentTransaction ftran=fm.beginTransaction();
ftran.replace(R.id.tab_and_view,NewContent.newInstance("NewContent"));
ftran.commit();
However, after the transaction is completed, the new fragment does not remain in viewPager anymore, it also used the spaces for tabview too.
Therefore I want to know how to switch fragment within this designated ViewPager space.
In the new fragment, NewContent.java, it also has a button for users to go back to home page. I would also like to ask how to finish this operation?
I'm new to stackoverflow, so I hope you know what problem i'm facing.... Thank you.
Another fragment file, TabFragment.java, i used it to build the tabs.
package com.android.luftschlafer.engdictionary;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.android.luftschlafer.engdictionary.Corr_Pages.AboutPage;
import com.android.luftschlafer.engdictionary.Corr_Pages.SearchPage;
import com.android.luftschlafer.engdictionary.Corr_Pages.SettingPage;
import com.android.luftschlafer.engdictionary.Tab.BaseFragment;
import com.android.luftschlafer.engdictionary.Tab.SlidingTabLayout;
import com.android.luftschlafer.engdictionary.Corr_Pages.HomePage;
import java.util.LinkedList;
/**
* Created by LuftSchlafer on 6/17/2016.
*/
//Configurations of all fragments
public class TabFragment extends Fragment {
private SlidingTabLayout tabs;
private ViewPager pager;
private FragmentPagerAdapter adapter;
public static Fragment newInstance(){
TabFragment f = new TabFragment();
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab_and_view, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
//adapter
final LinkedList<BaseFragment> fragments = getFragments();
adapter = new TabFragmentPagerAdapter(getFragmentManager(), fragments);
//pager
pager = (ViewPager) view.findViewById(R.id.pager);
pager.setAdapter(adapter);
//tabs
tabs = (SlidingTabLayout) view.findViewById(R.id.tabs);
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
#Override
public int getIndicatorColor(int position) {
return fragments.get(position).getIndicatorColor();
}
#Override
public int getDividerColor(int position) {
return fragments.get(position).getDividerColor();
}
});
tabs.setBackgroundResource(R.color.colorAccent);
tabs.setCustomTabView(R.layout.tab_title, R.id.txtTabTitle,
R.id.imgTabIcon);//REMEMBER THE SEQUENCE!
tabs.setViewPager(pager);
}
//Produce the fragments and corresponding tabs.
private LinkedList<BaseFragment> getFragments(){
int indicatorColor = Color.parseColor("RED");
int dividerColor = Color.TRANSPARENT;
LinkedList<BaseFragment> fragments = new LinkedList<>();
//Add fragments here
fragments.add(HomePage.newInstance("Home", indicatorColor,
dividerColor,R.drawable.ic_home_black_24dp));
fragments.add(SearchPage.newInstance("Search", indicatorColor,
dividerColor,R.drawable.ic_book_black_24dp));
fragments.add(SettingPage.newInstance("Settings", indicatorColor,
dividerColor,R.drawable.ic_build_black_24dp));
fragments.add(AboutPage.newInstance("About
app",indicatorColor,dividerColor,R.drawable.ic_info_outline_black_24dp));
return fragments;
}
}

Related

Can't set adapter of ListView because the ListView has a null object reference

I am trying to make a TabLayout with a ViewPager, but whenever I leave the app (Don't close it, just switch to another app and back) the fragments kind of get killed. I don't know that for sure, but the fragment's need to reCreate, and then fail.
The code for creating the TabLayout, TabLayoutAdapter and the ViewPager:
TabLayout tabLayout = (TabLayout) findViewById(R.id.tablayout);
MyTabLayoutAdapter tabLayoutAdapter;
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
tabLayout.addTab(tabLayout.newTab().setText("-2"));
tabLayout.addTab(tabLayout.newTab().setText("-1"));
tabLayout.addTab(tabLayout.newTab().setText("0"));
tabLayout.addTab(tabLayout.newTab().setText("1"));
tabLayout.addTab(tabLayout.newTab().setText("2"));
tabLayoutAdapter = new MyTabLayoutAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(tabLayoutAdapter);
viewPager.setOffscreenPageLimit(3);
tabLayout.setupWithViewPager(viewPager);
viewPager.setCurrentItem(2);
The code of my TabLayoutAdapter:
public class MyTabLayoutAdapter extends FragmentStatePagerAdapter {
private MyFragment fragmentOne;
private MyFragment fragmentTwo;
private MyFragment fragmentDefault;
private MyFragment fragmentFour;
private MyFragment fragmentFive;
private int numberOfTabs;
public MyTabLayoutAdapter(FragmentManager fragmentManager, int numberOfTabs) {
super(fragmentManager);
fragmentOne = new MyFragment();
fragmentTwo = new MyFragment();
fragmentDefault = new MyFragment();
fragmentFour = new MyFragment();
fragmentFive = new MyFragment();
this.numberOfTabs = numberOfTabs;
}
#Override
public MyFragment getItem(int position) {
switch (position) {
case 0:
return fragmentOne;
case 1:
return fragmentTwo;
case 2:
return fragmentDefault;
case 3:
return fragmentFour;
case 4:
return fragmentFive;
default:
return fragmentDefault;
}
}
#Override
public int getCount() {
return numberOfTabs;
}
}
The MyFragment fragment:
public class MyFragment extends Fragment {
private View currentView;
private ListView listView;
public MyFragment() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
currentView = LayoutInflater.from(getContext()).inflate(R.layout.my_fragment_layout, container, false);
listView = (ListView) currentView.findViewById(R.id.listview);
return currentView;
}
//This line throws a NullPointer when I resume the app
public void populateListView(MyListAdapter myListAdapter) {
listView.setAdapter(myListAdapter);
}
}
Whenever I call
MyListAdapter myListAdapter = new MyListAdapter(this, R.layout.my_item, justSomeStringArray);
tabLayoutAdapter.getItem(0).populateListView(myListAdapter);
I get a NullPointerException saying that it can't set a adapter on a null object reference.
Anyone any idea how I can fix this?
Here's an approach which lets the Fragment instance drive the data retrieval by using an AsyncTask implementation (GetDataForListViewTask).
But, to follow Commonsware's advice, GetDataForListViewTask merely obtains the data. It does not set up the adapter for use on the ListViews. Instead, setting up the ListAdapter is done in the Fragment itself.
GetDataForListViewTask passes the data back to the Fragment instance using EventBus. This keeps GetDataForListViewTask from having to maintain a reference to the ListView, Activity, Context or any of that risky stuff.
And, by not "reaching in" to a Fragment from the outside to change its widgets or even hand it data, this code also tries (very broadly) to follow another piece of CommonsWare's guidance:
In general, having code outside of a fragment attempt to manipulate
the widgets inside that fragment — as you are doing here — is a bad
idea.
In any case, if you use this approach, which is a bit different from your original approach, you'll want to modify the GetDataForListViewTask so that it obtains the data from elsewhere in your app or the network, etc.
GetDataForListViewTask.java
import android.os.AsyncTask;
import org.greenrobot.eventbus.EventBus;
import java.util.ArrayList;
class GetDataForListViewTask extends AsyncTask<Void, Void, ArrayList<String>> {
#Override
protected ArrayList<String> doInBackground(Void... ignored) {
// since this method runs on a worker thread, you may
// replace this code with a network call or a background computation
// from another part of the app is ready
ArrayList<String> listItems = new ArrayList<>();
for (int i=0; i<10; i++) {
listItems.add("item " + i);
}
return listItems;
}
#Override
protected void onPostExecute(ArrayList<String> result) {
super.onPostExecute(result);
// post event so that Fragment can use the data to populate its ListView
EventBus.getDefault().post(new MyFragment.ListViewDataReadyEvent(result));
}
}
MainActivity.java
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tablayout);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
MyAdapter tabLayoutAdapter = new MyAdapter(getSupportFragmentManager(), 5);
viewPager.setAdapter(tabLayoutAdapter);
viewPager.setOffscreenPageLimit(3);
tabLayout.setupWithViewPager(viewPager);
viewPager.setCurrentItem(2);
}
class MyAdapter extends FragmentStatePagerAdapter {
private final int count;
public MyAdapter(FragmentManager fm, int count) {
super(fm);
this.count = count;
}
#Override
public MyFragment getItem(int position) {
return MyFragment.newInstance();
}
#Override
public int getCount() {
return this.count;
}
#Override
public CharSequence getPageTitle(int position) {
return "Tab " + position;
}
}
}
MyFragment.java
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.ArrayList;
public class MyFragment extends Fragment {
private ListView listView;
private GetDataForListViewTask getDataForListViewTask;
public static MyFragment newInstance() {
return new MyFragment();
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true); // to support use of GetDataForListViewTask
EventBus.getDefault().register(this);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
listView = (ListView) LayoutInflater.from(getContext()).inflate(
R.layout.my_fragment_layout, container, false);
getDataForListViewTask = new GetDataForListViewTask();
getDataForListViewTask.execute();
return listView;
}
#Override
public void onDestroy() {
if (getDataForListViewTask != null) {
getDataForListViewTask.cancel(false);
}
EventBus.getDefault().unregister(this);
super.onDestroy();
}
static class ListViewDataReadyEvent {
ArrayList<String> data;
ListViewDataReadyEvent(ArrayList<String> data) {
this.data = data;
}
}
#SuppressWarnings("unused")
#Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(ListViewDataReadyEvent event) {
if (listView != null) {
ArrayAdapter<String> listAdapter = new ArrayAdapter<>(listView.getContext(),
android.R.layout.simple_list_item_1, event.data.toArray(new String[]{}));
listView.setAdapter(listAdapter);
}
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:padding="4dip"
android:gravity="center_horizontal"
android:layout_width="match_parent" android:layout_height="match_parent">
<android.support.design.widget.TabLayout
android:id="#+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill" />
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</LinearLayout>
my_fragment_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView android:id="#+id/listview"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
</ListView>
Selected compile statements for build.gradle
compile 'org.greenrobot:eventbus:3.0.0'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:appcompat-v7:25.3.1'
onCreateView() is not called on a fragment until the fragment is being added to a FragmentManager. Your final code snippet will only work, at best, if it is called sometime after fragmentOne is added to a FragmentManager. Otherwise, the fragment's ListView will not exist yet.
In general, having code outside of a fragment attempt to manipulate the widgets inside that fragment — as you are doing here — is a bad idea.

How to communicate from Mainactivity to Fragment using callback interface?

I'm having navigation drawer with four menu's and each menu has own fragments
, Inside first fragment has view pager sliding tab with 2 fragments,
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.CoordinatorLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="#color/white"
app:tabIndicatorHeight="4dp" />
</android.support.design.widget.AppBarLayout>
////////////// Here I'm replacing each fragments ////////////////
<FrameLayout
android:id="#+id/main_content_framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="#dimen/fab_margin"
android:src="#drawable/ic_done"
android:visibility="gone"/>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header"
app:menu="#menu/drawer_view"/>
</android.support.v4.widget.DrawerLayout>
ManinActivity- DrawerSelection
public void selectDrawerItem(MenuItem menuItem) {
// Create a new fragment and specify the fragment to show based on nav item clicked
Fragment fragment = null;
Class fragmentClass = null;
switch (menuItem.getItemId()) {
case R.id.nav_home:
fragmentClass = Home_Tab_Fragment.class;
break;
case R.id.nav_myorders:
fragmentClass = SecondFragment.class;
break;
case R.id.nav_myoffers:
fragmentClass = ThirdFragment.class;
break;
case R.id.nav_notification:
fragmentClass = FourthFragment.class;
break;
}
fragment = (Fragment) fragmentClass.newInstance();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.main_content_framelayout, fragment).commit();
mDrawerLayout.closeDrawers();
}
HomeTabFragment
public class Home_Tab_Fragment extends Fragment {
private FragmentActivity myContext;
ViewPager viewPager;
TabLayout tabLayout;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.layout_viewpager, container, false);
viewPager = (ViewPager) rootView.findViewById(R.id.view_pager);
return rootView;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
SlidingTabAdapter adapter = new SlidingTabAdapter(getChildFragmentManager());
adapter.addFragment(new FirstTabFragment(), "First TAB");
adapter.addFragment(new SecondTabFragment(), "Second Tab");
viewPager.setAdapter(adapter);
tabLayout = (TabLayout) getActivity().findViewById(R.id.tabs);
tabLayout.setVisibility(View.VISIBLE);
tabLayout.setupWithViewPager(viewPager);
}
}
SlidingTabAdapter
public class SlidingTabAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragments = new ArrayList<>();
private final List<String> mFragmentTitles = new ArrayList<>();
public SlidingTabAdapter(FragmentManager fm) {
super(fm);
}
public void addFragment(Fragment fragment, String title) {
mFragments.add(fragment);
mFragmentTitles.add(title);
}
#Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
#Override
public int getCount() {
return mFragments.size();
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitles.get(position);
}
}
layout_viewpager.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
FirstFragment
public class FirstTabFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_first, container, false);
getActivity().setTitle("Foodies");
return rootView ;
}
public void updateFirstFragmentValues(){
//Doing some operations
}
}
Inside MainActivity , I want to call FirstTabFragment method [updateFirstFragmentValues()]
I tried adding TAG in FirstTabFragment ,
public static final String TAG ="FirstTabFragment.TAG";
Then I invoked in MainActivity like below , but Fragment always null.
public void invokeMethodFromFirstTabFragment() {
FragmentManager fm = this.getSupportFragmentManager();
FirstTabFragment fb=(FirstTabFragment)fm.findFragmentByTag(FirstTabFragment.TAG);
if (null != fb) {
fb.updateFirstFragmentValues();
}else{
L.m("Fragment is null===========");
}
}
Kindly advise , How to call "FirstTabFragment" method from inside "MainActivity"
Please note FirstTabFragment is not added directly to MainActivity ,
its added through "Home_Tab_Fragment".
Alright, reading all of the responses of yours, I get that you just simply need to pass data to a fragment upon creation and be able to execute some of Fragment's methods. Ok, simple enough:
IMessageFragment interface
public interface IMessageFragment {
/**
* Method to receive new message text
* #param text Message text to receive
*/
void updateMessageText(String text);
}
MessageFragment
public class MessageFragment extends Fragment implements IMessageFragment {
/**
* Bind views using ButterKnife
*/
#BindView(R.id.textView) TextView mTextView;
/**
* Bundle data
*/
private String mMessageText;
/**
* Unique id of bundle data
*/
private static final String MESSAGE_EXTRA_KEY = "f_message";
/**
* New fragment pattern. This is pretty much all the magic PLUS this
* looks by far better than `new Fragment()` upon instatiation
*/
public static MessageFragment newFragment(String message) {
// 1. Create new fragment
MessageFragment auctionFragment = new MessageFragment();
// 2. Create bundle for fragment params
Bundle args = new Bundle();
// 3. Put position
args.putString(MESSAGE_EXTRA_KEY, message);
// 4. Set arguments
auctionFragment.setArguments(args);
return auctionFragment;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tutorial_fragment, container, false);
/**
* Bind views
*/
ButterKnife.bind(this, view);
// Update UI
updateUI();
return view;
}
/**
* Method to update value of mTextView
*/
private void updateUI() {
if(!mMessageText.equals("")) {
mTextView.setText(mMessageText);
}
}
#Override
public void updateMessageText(String text) {
mMessageText = text;
updateUI();
}
}
Stuff is pretty simple and the interface exists only for good code style and to support design patterns.
Edit:
You cannot directly access FirstTabFragment method from your MainActivity. Firstly you have to call Home_Tab_Fragment method from activity. Then from that method you can call FirstTabFragment method.
Here is strategy:
Step 1: Home_Tab_Fragment to FirstTabFragment communication
define an interface like this
public interface IHomeToFirstFragmentController{
void firstFragmentMethod();
}
Now implements the IHomeToFirstFragmentController in your FirstTabFragment. then you have to implements the firstFragmentMethod().
#overrride
void firstFragmentMethod(){
}
Call this firstFragmentMethod method from Home_Tab_Fragment using this way
YOUR_fragmentInstance.firstFragmentMethod();
Step 2: Activity to Home_Tab_Fragment:
define an interface like this
public interface IActivityToHomeTabController{
void callFirstFragment();
}
Now implements the IFragmentController in your Home_Tab_Fragment. then you have to implements the callFirstFragment().
#overrride
void callFirstFragment(){
YOUR_fragmentInstance.firstFragmentMethod();
}
Edit 2:
Call fragment using this way
((IFragmentController)YOUR_fragmentInstance).firstFragmentMethod();
Hope this will give you an idea about this topics. For more info visit this answer
First for all, you create interface :
public interface CallBackListener{
void onCallBack();
}
In Fragment
public MyFragment extends Fragment{
private CallBackListener mCallBack;
#Override
public void onAttach(Context context) {
AppCompatActivity activity;
if (context instanceof AppCompatActivity) {
activity = (AppCompatActivity) context;
try {
mCallBack= (CallBackListener ) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " class must be implement" +
" OnBackFragmentListener");
}
}
super.onAttach(context);
}
}
Use :
mCallBack.onCallBack();
And in Activity, you must implements interface :
public MyActivity extends AppCompatActivity implements CallBackListener{
#Override
public void onCallBack(){
// Do somethings here...
}
}

Viewpager cannot change imageView background programmatically

I am trying to initialize a ViewPager. I've failed to set image of programmatically to the viewpager. This is what I've come up with:
TutorialActivity.class
package app.trial.com;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
public class TutorialActivity extends FragmentActivity {
/**
* The number of pages (wizard steps) to show in this demo.
*/
private static final int NUM_PAGES = 3;
/**
* The pager widget, which handles animation and allows swiping horizontally to access previous
* and next wizard steps.
*/
private ViewPager mPager;
/**
* The pager adapter, which provides the pages to the view pager widget.
*/
private PagerAdapter mPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tutorial);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
}
#Override
public void onBackPressed() {
if (mPager.getCurrentItem() == 0) {
// If the user is currently looking at the first step, allow the system to handle the
// Back button. This calls finish() on this activity and pops the back stack.
super.onBackPressed();
} else {
// Otherwise, select the previous step.
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
/**
* A simple pager adapter that represents 5 ScreenSlidePageFragment objects, in
* sequence.
*/
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return TutorialFragment.create(position);
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}
TutorialFragment.class
package app.trial.com;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class TutorialFragment extends Fragment {
public static final String ARG_PAGE = "page";
private int mPageNumber;
public static TutorialFragment create(int pageNumber) {
TutorialFragment fragment = new TutorialFragment();
Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
fragment.setArguments(args);
return fragment;
}
public TutorialFragment() {}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mPageNumber = getArguments().getInt(ARG_PAGE);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_tutorial, container, false);
switch(mPageNumber) {
case 1:
((ImageView) rootView.findViewById(R.id.tutorialImage)).setImageResource(R.drawable.tutorial_03);
break;
case 2:
((ImageView) rootView.findViewById(R.id.tutorialImage)).setImageResource(R.drawable.tutorial_02);
break;
case 3:
((ImageView) rootView.findViewById(R.id.tutorialImage)).setImageResource(R.drawable.tutorial_03);
break;
default:
break;
}
return rootView;
}
/**
* Returns the page number represented by this fragment object.
*/
public int getPageNumber() {
return mPageNumber;
}
}
When I try to change R.id.tutorialImage using rootView.findViewById(R.id.tutorialImage).setImageResource() it doesn't change the background image. Also, it doesn't return null.
By the way my fragmentTutorial layout is:
<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="app.trial.com.TutorialFragment">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tutorialImage" />
</ScrollView>
</FrameLayout>

Creating ListView with android.support.v4.view.ViewPager showing nothing

I'm trying to create a ListView that each cell can be shifted as ViewPager.
Similar to Google Gmail app, that can shift emails in order to delete the emails.
It is working BUT showing nothing.
I created a ListView with BaseAdapter.
The Adapter create ViewPager with PagerAdapter that implements FragmentStatePagerAdapter.
The PagerAdapter activate the Fragment that supposed to show the data at the cells in the pagers.
Can you please help?
package com.tegrity.gui;
import java.util.ArrayList;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MyFregment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
/**
* simple ListView
*/
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_view1, container, false);
ListView mMyListView = (ListView) view.findViewById(R.id.myList1);
// create the my adapter
MyAdapter mMyAdapter = new MyAdapter(getActivity());
mMyListView.setAdapter(mMyAdapter);
// This is working but without the ListView
// view = inflater.inflate(R.layout.connect_pager_view, null);
// android.support.v4.view.ViewPager myPagerUnit =
// (android.support.v4.view.ViewPager)
// view.findViewById(R.id.connect_pager);
// PagerAdapter pagerAdapter = new MyPagerAdapter(getFragmentManager(),
// 0);
// myPagerUnit.setAdapter(pagerAdapter);
return view;
}
// the data
private static ArrayList<String> mMyList0 = new ArrayList<String>();
/**
* my adapter
*/
public class MyAdapter extends BaseAdapter {
// the data
private ArrayList<String> mMyList = new ArrayList<String>();
private Context mContext;
private LayoutInflater mInflater;
public MyAdapter(Context context) {
mContext = context;
mInflater = LayoutInflater.from(mContext);
mMyList.add("First line");
mMyList.add("Second line");
mMyList.add("Third line");
mMyList0 = mMyList;
}
#Override
public int getCount() {
return mMyList.size();
}
#Override
public Object getItem(int position) {
return mMyList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// optimization
if (convertView == null) {
convertView = mInflater.inflate(R.layout.connect_pager_view,
null);
}
android.support.v4.view.ViewPager myPagerUnit = (android.support.v4.view.ViewPager) convertView
.findViewById(R.id.connect_pager);
PagerAdapter pagerAdapter = new MyPagerAdapter(
getFragmentManager(), position);
myPagerUnit.setAdapter(pagerAdapter);
myPagerUnit
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
((Activity) mContext).invalidateOptionsMenu();
}
});
return convertView;
}
}
/**
* A simple pager adapter
*/
class MyPagerAdapter extends FragmentStatePagerAdapter {
// parameters from the my adapter
private int mPosition;
public MyPagerAdapter(FragmentManager fm, int position) {
super(fm);
mPosition = position;
}
#Override
public Fragment getItem(int pagePosition) {
return MyUnitFragment.create(pagePosition, mPosition);
}
#Override
public int getCount() {
return 2; // pager of 2 cells
}
}
/**
* my basic unit
*/
public static class MyUnitFragment extends Fragment {
public static final String PAGE = "page";
public static final String POSITION = "position";
private int mPageNumber;
// parameter from the my adapter
private int mPosition;
/**
* Factory method for this fragment class. Constructs a new fragment for
* the given page number.
*/
public static MyUnitFragment create(int pageNumber, int position) {
MyUnitFragment fragment = new MyUnitFragment();
Bundle args = new Bundle();
args.putInt(PAGE, pageNumber);
args.putInt(POSITION, position);
fragment.setArguments(args);
return fragment;
}
public MyUnitFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPageNumber = getArguments().getInt(PAGE);
mPosition = getArguments().getInt(POSITION);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout containing a title and body text.
View convertView = (View) inflater.inflate(R.layout.bookmark_unit,
container, false);
// page parts
String data = mMyList0.get(mPosition);
TextView textView = (TextView) convertView
.findViewById(R.id.bookmarkText1);
switch (mPageNumber) {
case 0: {
textView.setText(data + " at the first page");
break;
}
case 1: {
textView.setText(data + " at the second page");
break;
}
}
return convertView;
}
/**
* Returns the page number represented by this fragment object.
*/
public int getPageNumber() {
return mPageNumber;
}
}
}
XML for the ListView myList1.xml:
<?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:background="#color/white" >
<ListView android:id="#+id/myList1"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:divider="#drawable/course_divider"
android:dividerHeight="2dp"
android:cacheColorHint="#00000000" >
</ListView>
</LinearLayout>
XML for the Pager connect_pager_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/connect_pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</android.support.v4.view.ViewPager>
The list unit my_unit.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="#+id/myText1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#color/black"
android:layout_marginLeft="6dp">
</TextView>
</LinearLayout>
ViewPager in ListView is a bad idea.
You can use this Swipe-to-Dismiss library for deleting https://github.com/romannurik/android-swipetodismiss
You go through following link to implement gmail like delete from list function:
https://github.com/47deg/android-swipelistview

How to write button click event in ViewPager

Here my activity_main.xml present in /res/layout
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<!--
This title strip will display the currently visible page title, as well as the page
titles for adjacent pages.
-->
<android.support.v4.view.PagerTitleStrip
android:id="#+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#fff" />
</android.support.v4.view.ViewPager>
Here is my MainActivity.java file
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity extends FragmentActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {#link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
// Show 3 total pages.
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "POS 0"; // getString(R.string.title_section1).toUpperCase();
case 1:
return "POS 1";// getString(R.string.title_section2).toUpperCase();
case 2:
return "POS 2";// getString(R.string.title_section3).toUpperCase();
}
return null;
}
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view;
String i = Integer.toString(getArguments().getInt(
ARG_SECTION_NUMBER));
if (i.toString() == Integer.toString(1)) {
view = inflater.inflate(R.layout.section2, container, false);
} else {
view = inflater.inflate(R.layout.section1, container, false);
// Button button = (Button) view.findViewById(R.id.button1);
// button.setOnClickListener(new OnClickListener() {
// if i uncomment this code, button click works but i want it to handle in section1.java file
// #Override
// public void onClick(View v) {
// Activity activity = getActivity();
//
// if (activity != null) {
// Toast.makeText(activity, "sdfsdfsdfds",
// Toast.LENGTH_LONG).show();
// }
// }
//
// });
}
return view;
}
}
}
I have an activity named "Section1" which is displayed when i swipe to right side. It has a button on it.
How do i handle the button click event in section1.java file ?
here is my section.xml
<?xml version="1.0" encoding="utf-8"?>
<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=".Section1" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendMessage"
android:text="Button" />
</RelativeLayout>
Here is the Section1.Java file. Here button click even it not fired.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class Section1 extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.section1);
}
public void sendMessage(View view) {
Toast t = Toast.makeText(getApplicationContext(), "sdfsdfsdf",
Toast.LENGTH_LONG);
t.show();
}
}
I was having the same issue as you and I finally figured it out today so I thought I would share it with you
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
switch (i) {
case 2:
// The first section of the app is the most interesting -- it offers
// a launchpad into the other demonstrations in this example application.
return new LessonFragment();
default:
// The other sections of the app are dummy placeholders.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
fragment.setArguments(args);
return fragment;
}
}
#Override
public int getCount() {
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0: return getString(R.string.title_section1).toUpperCase();
case 1: return getString(R.string.title_section2).toUpperCase();
case 2: return getString(R.string.title_section3).toUpperCase();
case 3: return getString(R.string.title_section4).toUpperCase();
}
return null;
}
}
/**
* A fragment that launches other parts of the demo application.
*/
public static class LessonFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.lesson, container, false);
// Demonstration of a collection-browsing activity.
rootView.findViewById(R.id.button1)
.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(getActivity(), Test.class);
startActivity(intent);
}
});
return rootView;
}
}
#Override
public Fragment getItem(int position) {
switch(position){
case 0 :
case 1 :
// and so on.
}
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}

Categories

Resources