MrYanDao
MrYanDao

Reputation: 1263

Setting Data in ViewPagerIndicatorin different layouts

I have 3 different layouts on my ViewPagerIndicator - https://github.com/JakeWharton/Android-ViewPagerIndicator

My project is to load data from the website and set the data accordingly into the 3 different layout of the ViewPager.

How do I go about achieving it?

In my TestFragment, I have

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        Log.i("mContent", mContent);
        View view = null;
        if (mContent.equalsIgnoreCase("title1")) {
            view = inflater.inflate(R.layout.one, container, false);

        } else if (mContent.equalsIgnoreCase("title2")) {
            view = inflater.inflate(R.layout.two, container, false);
        } else if (mContent.equalsIgnoreCase("title3")) {
            view = inflater.inflate(R.layout.three, container, false);
            //Textview data = (TextView)view.findViewById(id)
        }
        return view;
    }

I know this is for setting layout according to the fling'd titles. I know how to findViewById to get the id of the widgets and set data. However, the data is set statically in the code itself.

For me, I have to grab data from the internet and put the data in accordingly. How do I achieved that? Thanks in advance.

Upvotes: 2

Views: 378

Answers (4)

Joe
Joe

Reputation: 141

I almost know what you want.Here is my answers,and it works well. You can according to the following steps.

1.Create three different Fragment class ,such as LoadImageFragment1,LoadImageFragment2,LoadImageFragment3, and all of them extends Fragment. Follow is my example.

public class LoadImageFragment1 extends Fragment {
ListView listView;
LayoutInflater mInflater;
LoadImageAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    mInflater = inflater;
    View view = inflater.inflate(R.layout.loadimage_layout, null);
    listView = (ListView) view.findViewById(R.id.listview);
    adapter = new LoadImageAdapter();
    adapter.initList();
    listView.setAdapter(adapter);
    return view;
}

private class LoadImageAdapter extends BaseAdapter{
    ArrayList<String> list = new ArrayList<String>();

    public void initList(){
        for(int i = 0 ; i < 30;i ++){
            list.add("" + i);
        }
    }

    @Override
    public int getCount() {

        return list.size();
    }

    @Override
    public Object getItem(int position) {

        return position;
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null){
            convertView = mInflater.inflate(R.layout.loadimage_item, null);
        }
        return convertView;
    }

}

2.Then modify your SampleTabsDefault class.

    class GoogleMusicAdapter extends FragmentPagerAdapter {
    public GoogleMusicAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
      //  return TestFragment.newInstance(CONTENT[position % CONTENT.length]);
        switch(position){
        case 0:
            return new LoadImageFragment1();
        case 1:
            return new LoadImageFragment2();
        case 2:
            return new LoadImageFragment3();
        }

    }

    @Override
    public CharSequence getPageTitle(int position) {
        return CONTENT[position % CONTENT.length].toUpperCase();
    }

    @Override
    public int getCount() {
      return CONTENT.length;
    }
}

And I just use the class of SampleTabsDefault as a sample.You can do this in other classes. Here I just use a ListView as a sample , you can load your images and texts from internt, and you also can do other things in your LoadImageAdapter 's getView() method. Hope this can help you.

Upvotes: 1

Streets Of Boston
Streets Of Boston

Reputation: 12596

Create and implement a Loader (AsyncTaskLoader) to load data from the internet.

Have your TestFragment implement LoaderCallbacks, so that it gets notified when the Loader (AsyncTaskLoader) has finished loading data from the internet.

In your implementation of your TestFragment's onActivityCreated, initialize or restart the loader:

getLoaderManager().initLoader(0, null, this);

In your TestFragment's implementation of onLoadFinished, assign the data to the widgets.

https://developer.android.com/reference/android/content/AsyncTaskLoader.html

https://developer.android.com/reference/android/app/LoaderManager.html

https://developer.android.com/reference/android/app/LoaderManager.LoaderCallbacks.html

Upvotes: 0

Siva Natarajan
Siva Natarajan

Reputation: 1429

Before starting, copy the samples from ViewPagerIndicator https://github.com/JakeWharton/Android-ViewPagerIndicator/tree/master/sample/src/com/viewpagerindicator/sample

The steps to be done are,

  1. Create an custom FragmentPagerAdapter (ex: https://github.com/JakeWharton/Android-ViewPagerIndicator/blob/master/sample/src/com/viewpagerindicator/sample/TestFragmentAdapter.java)

  2. Create custom Fragment for each each layout (in your case 3) https://github.com/JakeWharton/Android-ViewPagerIndicator/blob/master/sample/src/com/viewpagerindicator/sample/TestFragment.java

  3. Override onActivityCreated in the fragment class

In the onActivityCreated method, u can access the UI components as in onCreate method

So that u can make a web service call, bind it to ur UI components in this segment.

Cheers

Upvotes: 0

twocity
twocity

Reputation: 666

I have 3 different layouts on my ViewPagerIndicator

ViewPagerIndicator is just an indicator

I thought your meaning is your viewadapter have three views to show.

Use FragmentPagerAdapter to handle different view. you should read the ViewPagerIndicator samples

Upvotes: 0

Related Questions