Listview inside a fragment class of a Viewpager - android

I have a ViewPager setup with 3 fragment Classes, and a pageadapter class.
MainActivity class
package com.example.swipeview;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity {
ViewPager viewpager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewpager = (ViewPager) findViewById(R.id.viewpagers);
com.example.swipeview.PagerAdapter padapter = new com.example.swipeview.PagerAdapter(getSupportFragmentManager());
viewpager.setAdapter(padapter);
}
}
PageAdapter class
package com.example.swipeview;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class PagerAdapter extends FragmentPagerAdapter {
public PagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
#Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
switch(arg0){
case 0:
return new FragmentOne();
case 1:
return new FragmentTwo();
case 2:
return new FragmentThree();
default:
break;
}
return null;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}
}
and my first FragmentClass as an example
package com.example.swipeview;
import com.example.swipeview.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentOne extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
return inflater.inflate(R.layout.fragment_one_layout, container, false);
}
}//end of class
I've tried my usual methods for creating a listview inside of an Activity class but that doesn't work with the fragment class. I was wondering if anyone could show me how to code a listview into a fragment class.

An example with a custom adapter (using a List as source):
public class ItemsFragment extends Fragment {
/* Must be populated later. */
private final List<Item> items = new ArrayList<>();
private ItemAdapter adapter;
private ListView listView = null;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_items_list, container, false);
adapter = new ItemAdapter(this.getActivity(), items);
listView = (ListView) layout.findViewById(R.id.listview_items);
return layout;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Item currentItem = adapter.getItem(position);
// (...)
}
});
}
/* (...) */
}

Related

ViewPager Cannot find symbol class ScreenSlidePageFragment

I'm trying to develop a swipe slide activity with ViewPager and Fragments. I'm writting this code in the ScreenSlideActivity following tutorial from the Android Documentation.
(https://developer.android.com/training/animation/screen-slide)
But in the code of the tutorial there's a method that's not defined and I have an exception that says 'cannot resolve symbol class ScreenSlidePageFragment'
It seems that this method is not defined but I don't know how to write it because I don't what this method does.
I hope somebody can help me !
#Override
public Fragment getItem(int position) {
return new ScreenSlidePageFragment();
}
This is the full code
package com.flixarts.ar.englishnow;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
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;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
public class ScreenSlideActivity extends FragmentActivity {
private static final int NUM_PAGES = 3;
private ViewPager mPager;
private PagerAdapter pagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_slide);
mPager = (ViewPager) findViewById(R.id.pager);
pagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(pagerAdapter);
}
#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);
}
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return new ScreenSlidePageFragment();
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}
If you don't have this fragment, create it:
public class ScreenSlidePageFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_screen_slide_page, container, false);
return rootView;
}
}
define public constructor in ScreenSlidePageFragment
public ScreenSlidePageFragment() {
// Required empty public constructor
}
you should create your own Fragment that you can return in your adapter or use the same as the example
public class ScreenSlidePageFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_screen_slide_page, container, false);
return rootView;
}
}

How to develop an UITableView with Section and ViewForHeaderInSection in Android?

Good night, I'm new at this, the think that i need to do is this effect using a sroll which conceal a title & displays the following. For example: what happens in the contact list, when you spend the A and B, but still keeps all contacts letter.
I have an example implemented in ios with ViewForHeaderInSection this is what they used to do this effect in instagram this is what I need to do in android.
This is the closest thing I found:
Visit How to implement a scroll view on only part of my layout
Thanks, I apologize for my English is not excellent.
after search and search, i develop my woner layour with propierties...
This class help tu implements a #SlidePage into Fragment.
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.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.ImageView;
import java.util.ArrayList;
import java.util.List;
import mijem.bitgray.us.mijem_android.R;
public class FragmentTutorial extends Fragment {
/**
* 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;
ImageView indicator0;
ImageView indicator1;
ImageView indicator2;
List<ImageView> listIndicators = new ArrayList<ImageView>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_viewpager_tutorial, container, false);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) rootView.findViewById(R.id.pager);
mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
resetIndicators();
listIndicators.get(mPager.getCurrentItem()).setImageResource(
R.drawable.indicador_slide_on);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
mPagerAdapter = new ScreenSlidePagerAdapter(getActivity()
.getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
indicator0 = (ImageView) rootView.findViewById(R.id.indicator0);
indicator1 = (ImageView) rootView.findViewById(R.id.indicator1);
indicator2 = (ImageView) rootView.findViewById(R.id.indicator2);
listIndicators.add(indicator0);
listIndicators.add(indicator1);
listIndicators.add(indicator2);
return rootView;
}
#Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
//TabListener.onResumeFragment(this);
}
public void resetIndicators() {
for (ImageView img : listIndicators)
img.setImageResource(R.drawable.indicador_slide_off);
}
/**
* 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) {
ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
fragment.setPosition(position);
return fragment;
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}
enter code here
and the adapter:
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
fragment.setPosition(position);
return fragment;
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
enter code here
Code of fragmen pattern
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.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.ImageView;
import java.util.ArrayList;
import java.util.List;
import mijem.bitgray.us.mijem_android.R;
public class FragmentTutorial extends Fragment {
/**
* 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;
ImageView indicator0;
ImageView indicator1;
ImageView indicator2;
List<ImageView> listIndicators = new ArrayList<ImageView>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_viewpager_tutorial, container, false);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) rootView.findViewById(R.id.pager);
mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
resetIndicators();
listIndicators.get(mPager.getCurrentItem()).setImageResource(
R.drawable.indicador_slide_on);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
mPagerAdapter = new ScreenSlidePagerAdapter(getActivity()
.getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
indicator0 = (ImageView) rootView.findViewById(R.id.indicator0);
indicator1 = (ImageView) rootView.findViewById(R.id.indicator1);
indicator2 = (ImageView) rootView.findViewById(R.id.indicator2);
listIndicators.add(indicator0);
listIndicators.add(indicator1);
listIndicators.add(indicator2);
return rootView;
}
#Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
//TabListener.onResumeFragment(this);
}
public void resetIndicators() {
for (ImageView img : listIndicators)
img.setImageResource(R.drawable.indicador_slide_off);
}
/**
* 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) {
ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
fragment.setPosition(position);
return fragment;
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}

ListFragment doesn't re-display data when calling it from FragmentStatePagerAdapter

Here's my UI hierarchy:
Main Fragment -> Fragment with ActionBar.TabListener -> Fragment with FragmentStatePagerAdapter -> ListFragment
The issue is that the ListFragment does display the first time I go into the tab of the ListFragment, but it does not display the second time (so going to tab 2 displays it, but then going to tab 1 and then tab 2 it doesn't display itself. The ListFragment is in tab 2).
This layout works fine if I remove the FragmentStatePagerAdapter, but now my project requires pages, so I needed to add the FragmentStatePagerAdapter to make the paging work.
However, at this point I dont really know what the problem is. I dont know if the issue is in the FragmentStatePagerAdapter, the ListFragment or if the phone just for some reason looses the data of the ListFragment.
I do know that getItem in FragmentStatePagerAdapter does not get called again when switching tabs.
Here's my code for the TrendsPagerHolder and FragmentStatePagerAdapter (as inner class):
import java.util.ArrayList;
import java.util.Collections;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Fragment;
import android.os.Bundle;
import android.app.FragmentManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.support.v13.app.FragmentPagerAdapter;
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;
public class TrendsPagerHolder extends Fragment
{
private ArrayList<String>testArray = new ArrayList<String>();
private Context context;
private SharedPreferences preferences;
private Bundle applicationStatus;
// The number of pages to display
private static final int NUM_PAGES = 3;
private ViewPager pager;
//The pager adapter, which provides the pages to the view pager widget
private PagerAdapter pagerAdapter;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
context = getActivity().getApplicationContext();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
context = inflater.getContext();
applicationStatus = savedInstanceState;
View rootView = (View) inflater.inflate(R.layout.trends_screen_slider, container, false);
System.out.println("in TrendsPagerHolder onCreateView");
pager = (ViewPager) rootView.findViewById(R.id.trends_pager);
pagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
pager.setAdapter(pagerAdapter);
pager.setCurrentItem(0);
return rootView;
}
public class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter
{
TrendsLV[] arrayOfTrendsFragments;
public ScreenSlidePagerAdapter(FragmentManager fragmentManager)
{
super(fragmentManager);
testArray.add("One");
testArray.add("Two");
testArray.add("Three");
arrayOfTrendsFragments = new TrendsLV[NUM_PAGES];
arrayOfTrendsFragments[0] = new TrendsLV(testArray, 0);
arrayOfTrendsFragments[1] = new TrendsLV(testArray, 1);
arrayOfTrendsFragments[2] = new TrendsLV(testArray, 2);
}
#Override
public Fragment getItem(int position)
{
System.out.println("in getItem. Position = " + position);
return arrayOfTrendsFragments[position];
}
#Override
public int getCount()
{
return NUM_PAGES;
}
}
}
Here's the code for my ListFragment:
import java.util.ArrayList;
import java.util.Collections;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListFragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class TrendsLV extends ListFragment
{
private Context context;
private ArrayList<String>testArray;
private Bundle applicationStatus;
private int pageIndex;
public TrendsLV(ArrayList<String> test, int index)
{
testArray = test;
pageIndex = index;
}
public TrendsLV()
{
// required
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
context = inflater.getContext();
applicationStatus = savedInstanceState;
System.out.println("in TrendsLV onCreateView");
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
{
if (testArray != null)
{
TrendsListAdapter adapter = new TrendsListAdapter(context, testArray, pageIndex);
setListAdapter(adapter);
}
}
return super.onCreateView(inflater, container, savedInstanceState);
}
}//end class TrendsLV
And here's the code for the Adapter that is called from the ListFragment:
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class TrendsListAdapter extends ArrayAdapter<String>
{
private Context context;
private ArrayList<String>testArray;
private int pageIndex;
public TrendsListAdapter (Context c, ArrayList<String> test, int index)
{
super(c, R.layout.trends_row, test);
context = c;
testArray = test;
pageIndex = index;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.trends_row, parent, false);
TextView label_totalArea = (TextView) rowView.findViewById(R.id.trends_labelTotalArea);
label_totalArea.setText(context.getResources().getString(R.string.stringTotalArea));
return rowView;
}
}
Let me know if you need additional information to solve this issue.
I am testing it on a device using Android 4.4.4.
Thank you.
If you are inflating Fragment inside Fragment you should use getChildFragmentManager() instaed of getFragmentManager().
Here is the doc : http://developer.android.com/reference/android/app/Fragment.html#getChildFragmentManager()
public class TrendsPagerHolder extends Fragment
{
private ArrayList<String>testArray = new ArrayList<String>();
private Context context;
private SharedPreferences preferences;
private Bundle applicationStatus;
// The number of pages to display
private static final int NUM_PAGES = 3;
private ViewPager pager;
//The pager adapter, which provides the pages to the view pager widget
private PagerAdapter pagerAdapter;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
context = getActivity().getApplicationContext();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
context = inflater.getContext();
applicationStatus = savedInstanceState;
View rootView = (View) inflater.inflate(R.layout.trends_screen_slider, container, false);
System.out.println("in TrendsPagerHolder onCreateView");
pager = (ViewPager) rootView.findViewById(R.id.trends_pager);
pagerAdapter = new ScreenSlidePagerAdapter(getChildFragmentManager());
pager.setAdapter(pagerAdapter);
pager.setCurrentItem(0);
return rootView;
}
I think you should create the instance of ListFragment and use that instance in FragmentStatePagerAdapter.. some thing like this..
In Your TrendsLV class , remove the constructor and add below method to create the instance of fragment..
public static TrendsLV newInstance(ArrayList<String> test, int index) {
TrendsLV fragment = new TrendsLV();
testArray = test;
pageIndex = index;
return fragment;
}
Then update your ScreenSlidePagerAdapter as follow..
public class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter
{
TrendsLV[] arrayOfTrendsFragments;
public ScreenSlidePagerAdapter(FragmentManager fragmentManager)
{
super(fragmentManager);
testArray.add("One");
testArray.add("Two");
testArray.add("Three");
arrayOfTrendsFragments = new TrendsLV[NUM_PAGES];
arrayOfTrendsFragments[0] = TrendsLV.newInstance(testArray, 0);
arrayOfTrendsFragments[1] = TrendsLV.newInstance(testArray, 1)
arrayOfTrendsFragments[2] = TrendsLV.newInstance(testArray, 2)
}
#Override
public Fragment getItem(int position)
{
System.out.println("in getItem. Position = " + position);
return arrayOfTrendsFragments[position];
}
#Override
public int getCount()
{
return NUM_PAGES;
}
}
or instead of creating the array of fragments.. you can replace the in getItem() method like this..
public class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter
{
public ScreenSlidePagerAdapter(FragmentManager fragmentManager)
{
super(fragmentManager);
testArray.add("One");
testArray.add("Two");
testArray.add("Three");
}
#Override
public Fragment getItem(int position)
{
System.out.println("in getItem. Position = " + position);
Fragment instanceFragment = null;
switch (position) {
case 0:
instanceFragment =TrendsLV.newInstance(testArray, 0);;
break;
case 1:
instanceFragment =TrendsLV.newInstance(testArray, 1);;
break;
case 2:
instanceFragment =TrendsLV.newInstance(testArray, 2);;
break;
default:
break;
}
return instanceFragment;
}
#Override
public int getCount()
{
return NUM_PAGES;
}
}
I don't know what is the use of testArray so i haven't made any changes for it.
Hope it helps..!! :)

Can't add fragments - android

I'm trying to build a simple application that displays two fragments. The first fragment is displayed by default. It contains a list of names which you can choose and when you click on one of the items, it supposes to display a second fragment with a text view, displaying the name you have chosen.
The problem is everytime I click one of the names on the list, it throws me a NullPointerException. I really don't know what could be the problem.
Here are the codes( The app contains three class - two fragments and one activity. The FriendsF fragment is the list fragment and it performs well. The second fragment is FeedFragment and onitemclick it should display the name that was clicked)
FriendsF fragment:
package com.example.fragmentsexcersize;
import android.app.Activity;
import android.app.ListFragment;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class FriendsF extends ListFragment {
private static final String[] FRIENDS = { "ladygaga", "msrebeccablack",
"taylorswift13" };
public interface SelectionListener {
public void onItemSelected(int position);
}
private SelectionListener mCallback;
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? android.R.layout.simple_list_item_activated_1
: android.R.layout.simple_list_item_1;
setListAdapter(new ArrayAdapter<String>(getActivity().getBaseContext(), layout, FRIENDS));
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallback = (SelectionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement SelectionListener");
}
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (isInTwoPaneMode()) {
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
}
#Override
public void onListItemClick(ListView l, View view, int position, long id) {
mCallback.onItemSelected(position);
}
private boolean isInTwoPaneMode() {
return getFragmentManager().findFragmentById(R.id.tweets) != null;
}
}
FeedFragment:
package com.example.fragmentsexcersize;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class FeedFragment extends Fragment{
private TextView mTextView;
private static final String[] data = { "ladygaga", "msrebeccablack",
"taylorswift13" };
public FeedFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tweet_view, container, false);
mTextView = (TextView) rootView.findViewById(R.id.tweet_view);
return rootView;
}
public void updateFeedDisplay(int position) {
mTextView.setText(data[position]);
}
}
MainActivity:
package com.example.fragmentsexcersize;
import android.app.Activity;
import android.app.FragmentManager;
import android.os.Bundle;
import android.app.FragmentTransaction;
public class MainActivity extends Activity implements FriendsF.SelectionListener{
private FriendsF mFriendsFragment;
private FeedFragment mFeedFragment;
private FragmentManager fragMana;
private FragmentTransaction transaction;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFriendsFragment = new FriendsF();
fragMana = getFragmentManager();
transaction = fragMana.beginTransaction();
transaction.add(R.id.friends, mFriendsFragment);
transaction.commit();
}
private boolean isInTwoPaneMode() {
return findViewById(R.id.tweets) == null;
}
public void onItemSelected(int position) {
if (mFeedFragment == null)
mFeedFragment = new FeedFragment();
if (!isInTwoPaneMode()) {
transaction = fragMana.beginTransaction();
transaction.add(R.id.tweets, mFeedFragment);
transaction.commit();
}
mFeedFragment.updateFeedDisplay(position);
}
}
Make the following changes to your source:
MainActivity
public void onItemSelected(int position) {
Bundle bundle = new Bundle();
if (mFeedFragment == null)
mFeedFragment = new FeedFragment();
if (!isInTwoPaneMode()) {
bundle.putInt("POSITION", position);
mFeedFragment.setArguments(bundle);
transaction = fragMana.beginTransaction();
transaction.replace(R.id.tweets, mFeedFragment);
transaction.commit();
}
}
FeedFragment
private TextView mTextView;
private static final String[] data = { "ladygaga", "msrebeccablack",
"taylorswift13" };
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
View rootView = inflater.inflate(R.layout.tweet_view, container, false);
mTextView = (TextView) rootView.findViewById(R.id.tweet_view);
int mPosition = getArguments().getInt("POSITION", 0);
mTextView.setText(data[mPosition]);
return rootView;
}

Why is ViewPager inside my Fragment not working?

I am trying to create a fragment that would have a ViewPager. When the said fragment is called and committed, it only displays the first image and it is not swipable. Any thoughts how to achieve this?
Here is the Fragment
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class HowToFragment extends Fragment {
private ViewPager mViewPager;
private MyAdapter mAdapter;
private int[] image_id = {R.drawable.image_1, R.drawable.image_2, R.drawable.image_3};
private static int image_pos = 0;
public static Fragment newInstance(int pos){
image_pos = pos;
return new HowToFragment();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_how_to_image, container, false);
v.setBackgroundResource(image_id[image_pos]);
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mViewPager = new ViewPager(getActivity());
mViewPager.setId(R.id.viewPager_howto);
FragmentManager fm = getFragmentManager();
mAdapter = new MyAdapter(fm);
new setAdapterTask().execute();
}
private class setAdapterTask extends AsyncTask<Void,Void,Void>{
protected Void doInBackground(Void... params) {
return null;
}
#Override
protected void onPostExecute(Void result) {
mViewPager.setAdapter(mAdapter);
}
}
private class MyAdapter extends FragmentStatePagerAdapter{
public MyAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
#Override
public int getCount() {
return image_id.length;
}
#Override
public Fragment getItem(int position) {
return HowToFragment.newInstance(position);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
FragmentManager manager = getFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.remove((Fragment) object);
ft.commit();
super.destroyItem(container, position, object);
}
}
}
I doubt you are trying to run this app in Android version less than Honeycomb. What you are trying to achieve is nested fragments. Nested fragment support is available only since jellybean 4.2

Categories

Resources