Below is an screenshot from my app.
This screen is a fragment that has sliding tabs layout. It will hold another fragment that will show data in listview. The problem is, in order to load data the value selected from the spinner need to pass within the fragment in tab. I am not getting idea how to do this. One approach would be the tab fragment would implement a callback and within that callback data should be loaded. But I am not getting how to register that callback in onItemSelected of spinner.
Note: All fragments within the tab will show data in listview only, so I have created a common fragment.
This is my code so far:
Fragment for the screenshot
public class BuyListingFragment2 extends BaseFragment {
private Context ctx;
private Spinner vehicle_type;
private ArrayList<ListingTabModel> mListingTabs = new ArrayList<ListingTabModel>();
private ArrayAdapter<String> spinnerAdapter;
private ArrayList<String> vehicleTypeSpinnerlist;
private int spinnerPosition;
private SlidingTabLayout sliding_tabs;
private BuyListingPagerAdapter buyListingPagerAdapter;
public static BuyListingFragment2 newInstance(String category,
int position, String preselectedFilters) {
BuyListingFragment2 fragment = new BuyListingFragment2();
Bundle args = new Bundle();
args.putString("vehicle_type", category);
args.putInt("spinner_position", position);
fragment.setArguments(args);
return fragment;
}
public BuyListingFragment2() {
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.vehicleType = getArguments().getString("vehicle_type");
this.selectedVehicle = this.vehicleType;
this.spinnerPosition = getArguments().getInt("spinner_position");
ArrayList<CategoryType> vehicleTypeList = RegistrationResponse
.getInstance().getVehicleTypeList();
spinnerAdapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, vehicleTypeList);
buyListingPagerAdapter = new BuyListingPagerAdapter(
getChildFragmentManager(), mListingTabs);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ctx = getActivity();
vehicle_type = (Spinner) view.findViewById(R.id.vehicle_type);
vehicle_type.setAdapter(spinnerAdapter);
vehicle_type.setSelection(spinnerPosition, false);
if (mListingTabs.isEmpty()) {
String[] tabNames = getResources().getStringArray(
R.array.listing_tab_names);
for (int i = 0; i < tabNames.length; i++) {
String tabName = tabNames[i];
ListingTabModel mListingTabModel = new ListingTabModel();
mListingTabModel.setTagName(tabName);
mListingTabs.add(mListingTabModel);
}
}
buyListingPagerAdapter.notifyDataSetChanged();
listing_layout_viewpager = (ViewPager) view
.findViewById(R.id.listing_layout_viewpager);
listing_layout_viewpager.setAdapter(buyListingPagerAdapter);
sliding_tabs = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
sliding_tabs.setDistributeEvenly(true);
sliding_tabs.setViewPager(listing_layout_viewpager);
vehicle_type.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
spinnerPosition = position;
//How to register listener here
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
Common Fragment inside Tab
public class ListingFragment extends BaseFragment implements
OnSpinnerDataSelected {
private InfiniteListView mListView;
private BuyListingListAdapter buyListingAadapter;
private RobotoLightTextView emptyMessage;
private int currentPageNumber = 1;
private int totalPages;
private HashMap<String, String> params = new HashMap<String, String>();
private int apiCallCount = 0;
private Context ctx;
private String vehicleType;
private ProgressBar progressBar;
public ListingFragment() {
}
public static ListingFragment newInstance(ListingTabModel mListingTabModel) {
ListingFragment mFragment = new ListingFragment();
Bundle bundle = new Bundle();
// bundle.putBoolean("is_grid_view", mListingTabModel.isShowGridView());
// bundle.putString("vehicle_type", mListingTabModel.getVehicleType());
mFragment.setArguments(bundle);
return mFragment;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ctx = getActivity();
emptyMessage = (RobotoLightTextView) view
.findViewById(R.id.empty_message);
mListView = (InfiniteListView) view.findViewById(R.id.lstVw_buy);
boolean isGrid = getArguments().getBoolean("is_grid_view");
vehicleType = getArguments().getString("vehicle_type");
buyListingAadapter = new BuyListingListAdapter(ctx,
mVehicleListingList, isGrid);
mListView.setAdapter(buyListingAadapter);
progressBar = new ProgressBar(ctx);
}
#Override
public int getLayoutId() {
return R.layout.layout_messages;
}
#Override
public void onSpinnerDataSelected(String vehicleCategory) {
// TODO: fetch listing data
}
}
Callback implemented by the ListingFragment
public interface OnSpinnerDataSelected {
void onSpinnerDataSelected(String vehicleCategory);
}
FragmentStatePagerAdapter
public class BuyListingPagerAdapter extends FragmentStatePagerAdapter {
ArrayList<ListingTabModel> mFragmentsList;
public BuyListingPagerAdapter(FragmentManager fm,
ArrayList<ListingTabModel> mFragmentsList) {
super(fm);
this.mFragmentsList = mFragmentsList;
}
#Override
public Fragment getItem(int index) {
ListingFragment listingFragment = ListingFragment
.newInstance(mFragmentsList.get(index));
return listingFragment;
}
#Override
public int getCount() {
return mFragmentsList.size();
}
#Override
public CharSequence getPageTitle(int position) {
String tagName = mFragmentsList.get(position).getTagName();
tagName = tagName.replace("_", " ");
return tagName;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return object == view;
}
}
When using one activity and multiple fragments, I suggest to let the Fragment manage the UI and use the Activity has a controller/model.
Workflow for a spinner to communicate with other fragments :
Register the spinner listener in Frag1
Register a data listener from Frag2 in Activity
OnItemSelected from Frag1 prevent Activity from the Spinner value change
Activity received the spinner change value
Activity call Frag2 listener to prevent Frag2 of the spinner change
Frag2 receive spinner change, do your stuff
Here is a litle schema
I would base everything on an event bus like Otto. IMHO, Fragments were meant to be decoupled from hosting activities and such, but all the interfaces and callbacks end up creating spaghetti code. Otto lets you post event on a common bus -- the receiver doesn't need to be tied to the sender via some listener/callback mechanism. Plus, it works great in conjunction with dependency injection, see Dagger.
Related
I'm creating an app with vertical page adapter using FragmentStatePagerAdapter. The big issue i'm facing is, data is not displayed on the textview on first app launch but is displayed on scrolling the page. I believe the fragment view is delaying to create textview because, on my LoadAlbumDataCompleted() function inside Fragmentone.class, i'm able to print the data returned or also output via toast but is not getting populated to the textview.
Kindly help.
MainActivity.class
public class MainActivity extends FragmentActivity implements LoadAalbumsTotalListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAdapter = new MainActivityVSlideAdapter(this, getSupportFragmentManager(), NUMBER_OF_PAGES);
mPager.setAdapter(mAdapter);
LoadTotalAlbumsNum.BindAlbumsTotalListener(this);
}
#Override
public void OnLoadAlbumsCompleted(String total) {
if(total.trim().equalsIgnoreCase("")){
NUMBER_OF_PAGES=0;
}else{
NUMBER_OF_PAGES=Integer.parseInt(total.trim());
}
mAdapter = new MainActivityVSlideAdapter(this, getSupportFragmentManager(), NUMBER_OF_PAGES);
mPager.setAdapter(mAdapter);
}
}
MainActivityVSlideAdapter.class Adapter
public class MainActivityVSlideAdapter extends FragmentStatePagerAdapter {
static int NUMBER_OF_PAGES;
private Context con;
public MainActivityVSlideAdapter(Context con, FragmentManager fm, int NUMBER_OF_PAGES) {
super(fm);
this.con=con;
this.NUMBER_OF_PAGES=NUMBER_OF_PAGES;
}
#Override
public int getCount() {
return NUMBER_OF_PAGES;
}
#Override
public Fragment getItem(int position) {
return FragmentOne.newInstance(position);
}
}
Fragmentone.class
public class FragmentOne extends Fragment implements LoadAlbumDataListener {
private static final String MY_NUM_KEY = "num";
private int mNum;
private TextView SaloonName;
private TextView location;
// You can modify the parameters to pass in whatever you want
public static FragmentOne newInstance(int num) {
FragmentOne f = new FragmentOne();
Bundle args = new Bundle();
args.putInt(MY_NUM_KEY, num);
f.setArguments(args);
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//get argument from
mNum = getArguments() != null ? getArguments().getInt(MY_NUM_KEY) : 0;
session=new Session(getActivity());
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_one, container, false);
Methods methods=new Methods(getActivity());
// v.setBackgroundColor(mColor);
SaloonName = v.findViewById(R.id.SaloonName);
location=v.findViewById(R.id.location);
new LoadAlbumData(getActivity()).execute(getString(R.string.urlAddress)+"load-album-data.php", String.valueOf(mNum));
LoadAlbumData.BindLoadAlbumDataListener(this);
return v;
}
#Override
public void LoadAlbumDataCompleted(String s) {
JSONArray jsonPicsArray = null;
JSONObject jsonObj;
String BusinessLocation=null;
try {
jsonPicsArray = new JSONArray(s);
businessName = jsonObj.getString("businessName");
BusinessLocation = jsonObj.getString("location");;
}
Toast.makeText(getActivity(), businessName, Toast.LENGTH_LONG).show();
SaloonName.setText(businessName);
location.setText(BusinessLocation);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
From your code, it looks like, you are not setting the LoadAlbumDataListener correctly. Instead of statically setting the listener. Pass reference in constructor of your LoadAlbumData Asynctask
remove this line
LoadAlbumData.BindLoadAlbumDataListener(this);
replace
new LoadAlbumData(getActivity()).execute(getString(R.string.urlAddress)+"load-album-data.php", String.valueOf(mNum));
with
new LoadAlbumData(getActivity(), this).execute(getString(R.string.urlAddress)+"load-album-data.php", String.valueOf(mNum));
Modify your asynctask to have reference of LoadAlbumDataListener
Also as a good practice, never store strong reference of activity or fragment in your asynctask. Use WeakReference.
I'm building a simple stock display app that allows you to fetch stocks of interest (frag A), store them in a TinyDB, and display them in a recyclerView (frag B).
The framework used to work fine - until I decided to incorporate a viewpager and Tablayout host. I cannot get the RecyclerView in Frag B to display new data live. This is because the activity viewpager initializes both fragments at launch, meaning you can't call the onCreateView code again, I believe.
Communicating between two fragments through an Activity has been touched before on this site, but I found the best example to be this one:
(https://github.com/kylejablonski/InterfaceDemo),
which uses two interfaces, one to communicate from Frag A to Activity, and another one to communicate from Activity to Frag B. But I have a serious problem -
Currently, clicking both the "clear portfolio" and "add stock" to portfolio buttons in Frag A result in an empty screen in Frag B, which means something is being called yet new data is not being displayed/associated with the Adapter
Activity (https://github.com/EXJUSTICE/Investr/blob/master/app/src/main/java/com/xu/investo/ViewHolderActivity.java)
public class ViewHolderActivity extends AppCompatActivity implements CommunicateToActivity,CommunicateToFragment{
//Job of ViewHolderActivity is to allow swiping between list and MainFragment/Fragment
TabLayout tablayout;
ViewPager viewPager;
List<HistoricalQuote> historicaldata;
Bundle bundle;
Adapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_holder);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager =(ViewPager)findViewById(R.id.viewpager);
tablayout= (TabLayout)findViewById(R.id.tabs);
tablayout.setupWithViewPager(viewPager);
setupViewPager(viewPager);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
}
//----------------------- Interface code-----------
#Override
public void communicateup(){
communicatedown();
//call communicate down STRAIGHT, since we call it from mainfrag
}
#Override
public void communicatedown(){
//This line works
ListFragment currentFragment =(ListFragment)adapter.instantiateItem(viewPager,1);
currentFragment.refreshUI();
}
private void setupViewPager(ViewPager viewPager) {
adapter = new Adapter(getSupportFragmentManager());
adapter.addFragment(new MainFragment(), "Add Stock");
adapter.addFragment(new ListFragment(), "Portfolio");
viewPager.setAdapter(adapter);
}
static class Adapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public Adapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
Frag A (MainFragment)
(https://github.com/EXJUSTICE/Investr/blob/master/app/src/main/java/com/xu/investo/MainFragment.java)
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view =inflater.inflate(R.layout.content_main,container,false);
stocknames = new ArrayList<String>();
stocktickers = new ArrayList<String>();
tinyDB = new TinyDB(getContext());
/*
menu.setDisplayShowHomeEnabled(true);
//menu.setLogo("INSERT LOGO HERE");
menu.setDisplayUseLogoEnabled(true);
menu.setTitle(" Stock Selector");
*/
fetch =(Button) view.findViewById(R.id.fetchBtn);
enterID =(EditText)view.findViewById(R.id.enterID);
display =(TextView)view.findViewById(R.id.display);
mCalendar = Calendar.getInstance();
clear = (Button)view.findViewById(R.id.clearportfolio);
fetch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//TODO all the main page should do is add stocktickers and names to portfolio
//Fetch id and the dates
id =enterID.getText().toString();
/*to = Calendar.getInstance();
from = Calendar.getInstance();
to.setTime(dateto);
from.setTime(datefrom);
*/
FetchXDayData getData = new FetchXDayData();
getData.execute(id);
}
});
clear.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
tinyDB.clear();
recyclerinterface.communicateup();
}
});
return view;
}
//----------------------------INTERFACE CODE
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
recyclerinterface = (CommunicateToActivity) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement RecyclerUpdateInterface");
}
}
///-------End of Oncreate---------------------------------------------------------------
//Called by AsyncTask, moving result to main thread
public void moveResultToUI(Stock result){
this.stock = result;
Toast.makeText(getActivity(),"Stock "+stock.getName()+" successfully added to portofolio",Toast.LENGTH_LONG).show();
//reverse the list of course, stock names and tickrs to portfolio
stocknames.add(stock.getName());
stocktickers.add(stock.getSymbol());
/*DEBUG Test code, Test. 30012017 WORKS
for (int i =0;i<historicaldata.size();i++){
HistoricalQuote current = historicaldata.get(i);
Toast toast = Toast.makeText(this,current.getClose().toString(),Toast.LENGTH_SHORT);
toast.show();
}
*/
//
if (stock != null){
display.setText("Name: "+stock.getName() +"\n"+"Price: "+ stock.getQuote().getPrice()+"\n"+ "Change(%)"+stock.getQuote().getChangeInPercent());
/*SMA = getSMA(10);
decision=checkSimpleCrossover(SMA,stock.getQuote().getPrice().longValue());
decisionView.setText("SMA: " + SMA + "\n"+decision);
*/
tinyDB.putListString("names",stocknames);
tinyDB.putListString("tickers",stocktickers);
//call interface activity comming up to Activity, then down to next fragment
recyclerinterface.communicateup();
}else{
Toast error = Toast.makeText(getActivity(),"Network Problem",Toast.LENGTH_SHORT);
error.show();
}
}
Frag B (ListFragment)
(https://github.com/EXJUSTICE/Investr/blob/master/app/src/main/java/com/xu/investo/ListFragment.java)
public class ListFragment extends Fragment {
private HistoricalQuote[] hisstocks;
private Stock[] stocks;
private RecyclerView recyclerView;
private StockAdapter mAdapter;
public ArrayList<String> stocknames;
public ArrayList<String>stocktickers;
TinyDB tinyDB;
#Override
public void onCreate(Bundle savedInstanceState){
//exists only to set the options menu
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
//fetching arraylists
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.content_list, container, false);
//Convert the arraylist into an array for arrayadapter
stocknames = new ArrayList<String>();
stocktickers = new ArrayList<String>();
tinyDB = new TinyDB(getContext());
stocknames = tinyDB.getListString("names");
stocktickers= tinyDB.getListString("tickers");
if (!stocknames.isEmpty()){
for (int i =0;i<stocknames.size();i++){
Toast toast= Toast.makeText(getActivity(),stocknames.get(i),Toast.LENGTH_SHORT);
toast.show();
}
}
recyclerView = (RecyclerView)view.findViewById(R.id.recylerView);
recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.HORIZONTAL));
//http://stackoverflow.com/questions/24618829/how-to-add-dividers-and-spaces-between-items-in-recyclerview
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setItemAnimator(new DefaultItemAnimator());
//layoutManager necessary because it positions views on screen, in this case linearly
if (stocknames.isEmpty() ||stocknames ==null){
recyclerView.setVisibility(View.GONE);
}else{
updateUI();
}
return view;
}
public void refreshUI(){
stocknames.clear();
stocktickers.clear();
stocknames = tinyDB.getListString("names");
stocktickers= tinyDB.getListString("tickers");
if (mAdapter == null) {
mAdapter = new StockAdapter(stocknames,stocktickers);
recyclerView.setAdapter(mAdapter);
} else {
recyclerView.invalidate();
mAdapter.notifyDataSetChanged();
}
}
public void updateUI() {
//updateUI must be called EXPLICITLY!
stocknames = tinyDB.getListString("names");
stocktickers= tinyDB.getListString("tickers");
if (mAdapter == null) {
mAdapter = new StockAdapter(stocknames,stocktickers);
recyclerView.setAdapter(mAdapter);
} else {
mAdapter.notifyDataSetChanged();
}
}
private class StockAdapter extends RecyclerView.Adapter<StockHolder>{
private ArrayList<String>stocknames;
private ArrayList<String>stocktickers;
public StockAdapter(ArrayList<String>names,ArrayList<String> tickers){
this.stocknames=names;
this.stocktickers=tickers;
}
#Override
public StockHolder onCreateViewHolder(ViewGroup parent, int viewType){
LayoutInflater layoutinflater = LayoutInflater.from(getActivity());
View view= layoutinflater.inflate(R.layout.row,parent,false);
return new StockHolder (view);
}
//Bind datato stockholder depending on position in arraylist
public void onBindViewHolder(StockHolder holder, int position){
String stockname = stocknames.get(position);
String stockticker =stocktickers.get(position);
holder.bindStock(stockname,stockticker);
}
#Override
public int getItemCount (){
return stocknames.size();
}
}
private class StockHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private String stockname;
private String stockticker;
private TextView nametextView;
private TextView tickertextView;
public StockHolder(View itemView){
super(itemView);
itemView.setOnClickListener(this);
nametextView =(TextView)itemView.findViewById(R.id.name);
tickertextView= (TextView)itemView.findViewById(R.id.ticker);
}
#Override
public void onClick(View v){
Intent launchGraph= new Intent(v.getContext(),GraphActivity.class);
launchGraph.putExtra("stockticker",stockticker);
launchGraph.putExtra("stockname",stockname);
startActivity(launchGraph);
//Animations?
}
//Actual binder method, maybe add a current
public void bindStock(String stocknom, String stocktick){
this.stockname=stocknom;
this.stockticker = stocktick;
nametextView.setText(stockname);
tickertextView.setText(stockticker);
}
}
Thanks in advance.
EDIT: Solved issue by creating a new adapter and linking it to new arraylists pulled from the TinyDB, thereby effectively swapping adapters.
Solved the issue by creating a whole new RecyclerView adapter, to which new arraylist data was binded to, and the whole recyclerview was then set to use this new adapter. All of this was done in a single step from FragA, using interfaces shown in the code in the solution.
Method shown below:
public void refreshUI(){
tinyDB = null;
tinyDB = new TinyDB(getContext());
newnames = tinyDB.getListString("names");
newtickers= tinyDB.getListString("tickers");
mAdapter = new StockAdapter(newnames,newtickers);
recyclerView.setAdapter(mAdapter);
}
I have a ListView which contains product details. On Item click of listview I open new activity with particular product detail. I wanted to convert activity to ViewPager so that I can swipe to load next and previous records in same fragment. Fragment structure will be same for all records. I don't know from where should I start. Can you give me overview idea how to achieve this. Here is my model class.
Product.java
public class Product implements Serializable{
public int id;
public String Name;
public String description;
public String longDescription;
public String amount;
public String image;
}
Here is my FragmentPagerAdapter class
ProductPagerAdapter.java
public class ProductPagerAdapter extends FragmentPagerAdapter {
private Context context;
private ArrayList<Product> list;
public ProductPagerAdapter(FragmentManager fm, Context context, ArrayList<Product> list) {
super(fm);
this.context = context;
this.list = list;
}
#Override
public Fragment getItem(int position) {
return ProductFragment.newInstance(position);
}
#Override
public int getCount() {
return list.size();
}
}
And this is my Fragment
ProductFragment.java
public class ProductFragment extends Fragment {
public ProductFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_product, container, false);
//findViewById...
return v;
}
public static Fragment newInstance(int id) {
Bundle args = new Bundle();
args.putInt("Id", id);
ProductFragment fragment = new ProductFragment();
fragment.setArguments(args);
return fragment;
}
}
And now on list item Click I am opening new activity. And I am sending Product object to it.
lv_itemRateList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getActivity(), DetailsActivity.class);
Product r = new Product();
r = rateListArrayList.get(i);
intent.putExtra("product",r);
startActivity(intent);
}
});
My DetailsActivity contains my viewpager. Now can someone tell me how to do this?
1-) Need a custom adapter for ViewPager.
I assumed that all the pages will have the same content, so all of them have the same layout so ProductPagerAdapter extends the PagerAdapter.
public class ProductPagerAdapter extends PagerAdapter
{
//variables
private Context context;
private ArrayList<Product> list;
private Product product
//views
TextView txtName ;
public ProductPagerAdapter(Context context, List<Product> list)
{
this.context = context;
this.list = list;
}
#Override
public int getCount()
{
return list.size();
}
#Override
public boolean isViewFromObject(View view, Object object)
{
return view == ( object);
}
#Override
public Object instantiateItem(ViewGroup container, int position)
{
product = list.get(position);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.fragment_product, container,false);
txtName = (TextView) itemView.findViewById(R.id.txtName);
txtName.setText(product.Name)
((ViewPager) container).addView(itemView);
return itemView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object)
{
((ViewPager) container).removeView((LinearLayout) object);
}
}
2-) Adapter is ready. Now let's prepare the activity that will show the details. First initialize ViewPager and ProductPagerAdapter. Then show the details of the item which is selected by clicking on the previous activity.
public class DetailsActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
...
//getting product object
Intent intent = getIntent();
Product product = (Product) getIntent().getSerializableExtra("product");
//get selected item id
int selectedItemID = 0;
for(int i = 0 ; i < list.size() ; i++)
{
if(list.get(i).id == product.id)
{
selectedItemID = i;
break;
}
}
//Init ViewPager and adapter
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
ProductPagerAdapter detailAdapter = new ProductPagerAdapter(DetailActivity.this, list);
// Binds the Adapter to the ViewPager
viewPager.setAdapter(detailAdapter);
viewPager.setCurrentItem(selectedItemID); // set selection
viewPager.setPageTransformer(true, new ForegroundToBackgroundTransformer()); //custom transformer
...
}
}
3-) We've done what we have to do so far. So it will work like this. But what I would like to point out is that there are a few things to make the ViewPager better.
Performance : Use Parcelable instead of Serializable. Parcelable takes more time to implement but it will perform 10 times faster and use less resources. Please check this SO answer
Animation : You may want to need animations for transforming ViewPager. I suggest you to use ViewPagerTransforms
Indicators : If you want to use the paging indicator, I recommend ViewPagerIndicator.
For more on ViewPager
I am designing a constitution app and I want to use a tabbed layout with swipe view. The tabs get data from the database using a custom adapter. Since the data size (no of fragment) is unknown, I want every swipe to generate a new view which are the different chapter content from the Constitution.
I want something that looks like the dictionary app below, with those swipe labels on both sides. I am familiar with tabs but I would love to get a resource to help me achieve this, since most documentation I have seen doesn't explain this. Thanks
Modify this with your desired OutPut
onCreate
ArrayList<McqQuestionBean> mcqQuestionBeans= new ArrayList<McqQuestionBean>();
adapter = new NewsFragmentPagerAdapter(getSupportFragmentManager(),
mcqQuestionBeans, MCQTestActivity.this);
pager.setAdapter(adapter);
Base Adapter
public class NewsFragmentPagerAdapter extends FragmentStatePagerAdapter {
private ArrayList<McqQuestionBean> mcqQuestionBeans;
private McqQuestionFragment fragment;
private Activity context;
public NewsFragmentPagerAdapter(FragmentManager fm, ArrayList<McqQuestionBean> mcqQuestionBeans, Activity context) {
super(fm);
this.mcqQuestionBeans = mcqQuestionBeans;
this.context = context;
}
public void update(ArrayList<McqQuestionBean> mcqQuestionBeans) {
this.mcqQuestionBeans = mcqQuestionBeans;
notifyDataSetChanged();
}
#Override
public int getCount() {
return mcqQuestionBeans.size();
}
#Override
public int getItemPosition(Object object) {
// TODO Auto-generated method stub
return super.getItemPosition(object);
}
#Override
public Fragment getItem(int position) {
fragment = McqQuestionFragment.newInstance(mcqQuestionBeans.get(position), position, context);
return fragment;
}
}
Your Fragment McqQuestionFragment
public class McqQuestionFragment extends Fragment {
private int position, porrefid;
private String question;
private ArrayList<McqQuestionChoiceBean> choices;
#SuppressWarnings("unchecked")
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
position = getArguments().getInt("position");
porrefid = getArguments().getInt("porrefid");
userMarkedOn = getArguments().getInt("userMarkedOn");
question = getArguments().getString("question");
choices = (ArrayList<McqQuestionChoiceBean>) getArguments()
.getSerializable("choices");
}
public static McqQuestionFragment newInstance(
McqQuestionBean mcqQuestionBean, int position, Activity activity) {
final McqQuestionFragment f = new McqQuestionFragment();
final Bundle args = new Bundle();
args.putString("question", mcqQuestionBean.getQuestion());
args.putInt("position", position);
args.putInt("userMarkedOn", mcqQuestionBean.getUserCorrectedOn());
args.putSerializable("choices", mcqQuestionBean.getChoices());
args.putInt("porrefid", mcqQuestionBean.getPorrefid());
f.setArguments(args);
return f;
}
}
I'm building an Android application using a FragmentStatePagerAdapter for tabbed navigation and dynamic content in each tab. Each tab has Fragment with content which is to be replaced upon user input (for example, the first tab has a Fragment containing a list of books, and upon clicking, you can access detailed information of the book, which is displayed using another Fragment
Problem: I haven't find a way of correctly handling the onBack events nor the BackStack, so when I'm reviewing any book's details, I can easily go back pressing the back button - I mean, popping the last state from the BackStack.
What I suspect: The way I'm switching Fragment objects may not the the best one, but except for the back button issue, it is working just as I want. I suspect some problem between the FragmentStatePagerAdapter's adapter, and the FragmentManager's own collection of Fragments; probably this is something with an easy solution I didn't see.
Unaswered question (not very detailed though): Adding Fragment to BackStack using FragmentStatePagerAdapter
The code:
// MAIN ACTIVITY, Just this simple.
public class MainActivity extends FragmentActivity {
public static final String TAG = "MainActivity";
// Whether the Log Fragment is currently shown
private boolean mLogShown;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
MainTabSliderFragment fragment = new MainTabSliderFragment();
transaction.replace(R.id.sample_content_fragment, fragment);
transaction.commit();
}
}
}
.
// THE SLIDE TAB FRAGMENT, which becomes the parent view of the tabs.
public class MainTabSliderFragment extends Fragment {
static final String LOG_TAG = MainTabSliderFragment.class.getSimpleName();
private SlidingTabLayout mSlidingTabLayout;
private ViewPager mViewPager;
private CustomFragmentStatePageAdapter cfspAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_sample, container, false);
return root;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
cfspAdapter = new CustomFragmentStatePageAdapter(getFragmentManager());
List<String> pageTitles = new ArrayList<>();
pageTitles.add(getString(R.string.page_one));
pageTitles.add(getString(R.string.page_two));
pageTitles.add(getString(R.string.page_three));
List<Fragment> pageFragments = new ArrayList<>();
final BookListPageFragment pageOne = BookListPageFragment.newInstance(new CustomFragmentStatePageAdapter.SwitchFragmentListener() {
#Override
public void onSwitchFragments(Class<? extends Fragment> clazz, Map<String, String> ... args) {
cfspAdapter.switchFragment(CustomFragmentStatePageAdapter.PagePosition.POSITION_PAGE_ONE, clazz, this, args);
}
});
CustomerPageFragment pageTwo = CustomerPageFragment.newInstance(...);
ForumPageFragment pageThree = ForumPageFragment.newInstance(...);
pageFragments.add(pageOne);
pageFragments.add(pageTwo);
pageFragments.add(pageThree);
cfspAdapter.addFragments(pageFragments, pageTitles);
mViewPager.setAdapter(cfspAdapter);
mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
mSlidingTabLayout.setViewPager(mViewPager);
}
}
.
// THE FIRST TAB, In its initial state (the initial fragment).
public class BookListPageFragment extends Fragment {
private static final String TAG = BookListPageFragment.class.getSimpleName();
private BookListAdapter bAdapter;
private static CustomFragmentStatePageAdapter.SwitchFragmentListener switchFragmentListener;
public static BookListPageFragment newInstance(CustomFragmentStatePageAdapter.SwitchFragmentListener _switchFragmentListener) {
switchFragmentListener = _switchFragmentListener;
BookListPageFragment f = new BookListPageFragment();
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.page_one_booklist, container, false);
final ListView lv = (ListView) v.findViewById(R.id.list);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
BookRowData bRow = (BookRowData) lv.getItemAtPosition(position);
Log.i(TAG, "Clicked on book " + bRow.getBookId());
Map<String, String> param = new HashMap<>();
param.put("book_id", Long.toString(bRow.getBookId()));
switchFragmentListener.onSwitchFragments(ReviewBookPageFragment.class, new Map[]{param});
}
});
initializeTestList(v, lv); // Just add some books to the list.
return v;
}
.
// THE PAGE ADAPTER, used for handling tab's Fragment switching.
public class CustomFragmentStatePageAdapter extends FragmentStatePagerAdapter {
private final static String TAG = FragmentStatePagerAdapter.class.getSimpleName();
private FragmentManager fragmentManager;
private List<Fragment> fragmentList = new ArrayList<>();
private List<String> tabTitleList = new ArrayList<>();
public CustomFragmentStatePageAdapter(FragmentManager fm) {
super(fm);
fragmentManager = fm;
}
public void addFragments(List<Fragment> fragments, List<String> titles) {
fragmentList.clear();
tabTitleList.clear();
fragmentList.addAll(fragments);
tabTitleList.addAll(titles);
notifyDataSetChanged();
}
#Override
public int getItemPosition(Object object) {
if (fragmentList.contains(object)) {
return POSITION_UNCHANGED;
}
return POSITION_NONE;
}
#Override
public Fragment getItem(int item) {
if (item >= fragmentList.size()) {
return null;
}
return fragmentList.get(item);
}
#Override
public int getCount() {
return fragmentList.size();
}
#Override
public CharSequence getPageTitle(int position) {
return tabTitleList.get(position);
}
/**
* Switching pages
*
* #param newFragment
*/
public void switchFragment(final PagePosition position, Class<? extends Fragment> newFragment, SwitchFragmentListener sfListener, Map<String, String> ... args) {
final Fragment old = fragmentList.get(position.getPagePosition());
fragmentManager.beginTransaction().remove(old).commit(); //FIRST VERSION: IF HITTING BACK, IT EXITS APP AT ONCE.
//fragmentManager.beginTransaction().addToBackStack("page_one").remove(old).commit(); //SECOND VERSION: NOW I NEED TO HIT BACK TWICE TO EXIT, BUT THE VIEW DOESN'T CHANGE AFTER HITTING THE FIRST TIME.
try {
Fragment f = (Fragment) newFragment.asSubclass(Fragment.class).getMethod("newInstance", SwitchFragmentListener.class, Map[].class).invoke(newFragment, new Object[]{sfListener, args});
fragmentList.set(position.getPagePosition(), f);
} catch (IllegalAccessException iae) {
Log.e(TAG, "Fragment class access exception");
} catch (NoSuchMethodException e) {
Log.e(TAG, "Fragment instantiation exception (reflection)");
} catch (InvocationTargetException e) {
Log.e(TAG, "Fragment instantiation exception (reflection: no public constructor)");
}
notifyDataSetChanged();
}
public interface SwitchFragmentListener {
void onSwitchFragments(Class<? extends Fragment> clazz, Map<String, String> ... args);
}
public enum PagePosition {
POSITION_PAGE_ONE (0),
POSITION_PAGE_TWO (1),
POSITION_PAGE_THREE (2);
private final int position;
PagePosition(int position) {
this.position = position;
}
public int getPagePosition() {
return this.position;
}
}
}
.
// AND FINALLY THE FRAGMENT I WANT TO GO BACK FROM; this is the book review Fragment, which is displayed also in the first tab when clicking on a book from the list. Second and third tabs are ommitted.
public class ReviewBookPageFragment extends Fragment {
private static final String TAG = ReviewBookPageFragment.class.getSimpleName();
private CommentsListAdapter cAdapter;
private Long bookId;
private static CustomFragmentStatePageAdapter.SwitchFragmentListener switchFragmentListener;
public static ReviewBookPageFragment newInstance() {
ReviewBookPageFragment f = new ReviewBookPageFragment();
return f;
}
public static ReviewBookPageFragment newInstance(CustomFragmentStatePageAdapter.SwitchFragmentListener _sfListener, Map<String, String> ... args) {
switchFragmentListener = _sfListener;
Bundle b = BundlePacker.packMaps(args); // Custom util class for packing the params into a bundle.
ReviewBookPageFragment f = new ReviewBookPageFragment();
f.setArguments(b);
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.page_review_book, container, false);
Bundle bookIdBundle = this.getArguments();
Long bId = Long.parseLong(bookIdBundle.getString("book_id"));
Log.i(TAG, "Book ID: " + bId);
initializeTestList(v); // Just fill the book's reviews with test data.
return v;
}
}
So, that's the bunch of code. The idea, as a summary, is to switch from the books list view (shown on tab one), to the book's reviews when tapping on any book from the list; the reviews are also shown on the first tab, and I want to go back to the books list when pressing back. Currently, it closes the application hitting back ONCE, and if I add the transaction to the backstack (see my CustomFragmentStatePageAdapter), TWICE (but the view doesn't change after hitting back the first time.
Any help with the issue will be greatly appreciated.
For fixing the popback issue you can use this code in your activity class,
#Override
public void onBackPressed() {
// if there is a fragment and the back stack of this fragment is not empty,
// then emulate 'onBackPressed' behaviour, because in default, it is not working
FragmentManager fm = getSupportFragmentManager();
for (Fragment frag : fm.getFragments()) {
if (frag.isVisible()) {
FragmentManager childFm = frag.getChildFragmentManager();
if (childFm.getBackStackEntryCount() > 0) {
childFm.popBackStack();
return;
}
}
}
super.onBackPressed();
}
I did somethink like this:
private View _view;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if(_view==null){
_view = inflater.inflate(R.layout.page_review_book, container, false);
// your_code
}
return _view;
}