Code Monkey home page Code Monkey logo

Comments (16)

pakerfeldt avatar pakerfeldt commented on May 29, 2024

You should have a look at the Compatibility Library and more specifically ViewPager which is pretty much ViewFlow but with Fragments instead.

http://developer.android.com/sdk/compatibility-library.html

from android-viewflow.

franciscojunior avatar franciscojunior commented on May 29, 2024

Yeah, thats what I'm using right now. But the viewpager doesn't have the title flow indicator, or at least I couldn't find it. My idea was to be able to use something like viewpager with your title flow indicator.

from android-viewflow.

pakerfeldt avatar pakerfeldt commented on May 29, 2024

I'm afraid that hasn't been tried. You could possibly copy the title flow indicator and adjust it to the ViewPager.

from android-viewflow.

franciscojunior avatar franciscojunior commented on May 29, 2024

Ok, thanks! I'll have a look at viewflow code and see what I can get. I'll let you know if I have any progress...

from android-viewflow.

franciscojunior avatar franciscojunior commented on May 29, 2024

Ok, I think I got some real progress here....

I got the TitleFlowIndicator to work with the ViewPager, but there is only one detail giving me problem right now: When I scroll to a view in the right, the title scroll is done correctly but when the scroll finishes, the title scrolled doesn't stick. :( It is changed to the first title. As soon as I start to scroll back to the left, the title is changed and the title scrolling happens normally.

So, the only problem right now I couldn't figure out yet is why the tile is being stick to the first title.

The only change I had to make to the TitleFlowIndicator was to add:

a field:
private PagerAdapter pagerAdapter

a method:
public void setPagerAdapter(PagerAdapter pager) {
pagerAdapter = pager;
}

And I changed the 2 lines which read:

int count = (viewFlow != null && viewFlow.getAdapter() != null) ? viewFlow.getAdapter().getCount() : 1;

to

int count = (pagerAdapter != null) ? pagerAdapter.getCount() : 1;

With those changes, you only have to add the TitleFlowIndicator together with your viewpager in your layout:

<LinearLayout android:layout_width="fill_parent"
    android:gravity="center_horizontal" android:id="@+id/header_layout"
    android:orientation="vertical" android:layout_height="wrap_content">
    <org.taptwo.android.widget.TitleFlowIndicator
        android:id="@+id/viewflowindic" android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        app:footerLineHeight="2"
        app:footerTriangleHeight="10" app:textColor="#FFFFFFFF" app:selectedColor="#FFFFC445" app:footerColor="#FFFFC445" app:titlePadding="10" app:textSize="13" android:layout_marginTop="10dip" ></org.taptwo.android.widget.TitleFlowIndicator>

</LinearLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"/>

The trick comes in the form of this adapter class:

public static class TextIndicatorAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener, TitleProvider {

    public TextIndicatorAdapter(FragmentActivity activity,
            ViewPager pager, TitleFlowIndicator tfi) {
        super(activity.getSupportFragmentManager());

        mContext = activity;
        mViewPager = pager;
        mFlowIndicator = tfi;

        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);


        tfi.setTitleProvider(this);
        tfi.setPagerAdapter(this);

        // TODO Auto-generated constructor stub
    }

    private final Context mContext;

    private final ViewPager mViewPager;

    private final TitleFlowIndicator mFlowIndicator;


    @Override
    public void onPageScrolled(int position, float positionOffset,
            int positionOffsetPixels) {
        // TODO Auto-generated method stub

        mFlowIndicator.onScrolled(positionOffsetPixels, 0, 0, 0);

    }

    @Override
    public void onPageSelected(int position) {
        // TODO Auto-generated method stub

        mFlowIndicator.onSwitched(null, position);
        Log.d("viewpager", String.valueOf(position));

    }

    @Override
    public void onPageScrollStateChanged(int state) {
        // TODO Auto-generated method stub

    }

    @Override
    public Fragment getItem(int position) {
        // TODO Auto-generated method stub
        if (position == 0)
            return Fragment.instantiate(mContext, Fragment1.class.getName(),
                    null); 
        else
            return Fragment.instantiate(mContext, Fragment2.class.getName(),
                    null);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 2;
    }

    public void setCurrentView(int id) {

        mViewPager.setCurrentItem(id);
    }

    @Override
    public String getTitle(int position) {
        // TODO Auto-generated method stub
        if (position == 0)
            return "Title 1"; 
        else
            return "Title 2";
    }

}

It is hardcoded, sorry for that, because I just wanted to test it fast and see how it works.

After that, you just need to glue the adapter with the viewpager in your onCreate or onCreateView method:

mViewPager = (ViewPager) v.findViewById(R.id.pager);

    TitleFlowIndicator tfi = (TitleFlowIndicator) v.findViewById(R.id.viewflowindic);




    TextIndicatorAdapter a = new TextIndicatorAdapter(getActivity(), mViewPager, tfi);
    a.setCurrentView(0);

That's it!

Please, if you could point me out what I have to do in order to get the correct title stick in the selected value after scroll it would be very much welcome!

Thanks in advance.

from android-viewflow.

franciscojunior avatar franciscojunior commented on May 29, 2024

More info: I noticed that the titleindicator always stick to the left most title when the scroll is stopped. As soon as I start scrolling, the titles are drawn correctly.

I may be missing something when the scroll stops in order to update the title indicator state.

from android-viewflow.

franciscojunior avatar franciscojunior commented on May 29, 2024

Ok, now I got it.

The problem was on my onPageScrolled implementation. After checking ViewFlow implementation, I reached the following lines:

/*
* The actual horizontal scroll origin does typically not match the
* perceived one. Therefore, we need to calculate the perceived
* horizontal scroll origin here, since we use a view buffer.
*/
int hPerceived = h + (mCurrentAdapterIndex - mCurrentBufferIndex)
* getWidth();

And obviously my initial implementation wasn't taking care of the offset correctly.
I just had to change it to:

int hPerceived = positionOffsetPixels + position
* mViewPager.getWidth();

        mFlowIndicator.onScrolled(hPerceived, 0, 0, 0);

And it worked like a charm!

I hope it helps.

Please, let me know if you have any question about it.

from android-viewflow.

JakeWharton avatar JakeWharton commented on May 29, 2024

Please provide a gist of the final working example so we can see the code in one place.

from android-viewflow.

franciscojunior avatar franciscojunior commented on May 29, 2024

Sure! I'm woking on it right now....

from android-viewflow.

franciscojunior avatar franciscojunior commented on May 29, 2024

There is it:
https://gist.github.com/1122947

I didn't know about gist. It is awesome!

When using the code, note that you have to change the code of TitleFlowIndicator. I think this may, if accepted, of course, be refactored into 2 different classes or added support for both adapters, as I noticed that the viewflow adapter in only used to get the number of views. Maybe the code could test the presence of both adapters and use which one is available.

Note that I also added a simple onclick handler to the titleflowindicator to show that the click of the tabs would work as expected and it would be a nice addition to have the tabs recognize clicks and act accordingly. The click handler I added only changes the view from the position 1 to 2, but is works as a proof of concept.

Please, let me know what do you think.

from android-viewflow.

franciscojunior avatar franciscojunior commented on May 29, 2024

pakerfeldt, what did you think about the changes?

from android-viewflow.

pakerfeldt avatar pakerfeldt commented on May 29, 2024

I have had very limited time to spend on ViewFlow lately (due to vacation) but I will look at your changes as soon as I can.

from android-viewflow.

franciscojunior avatar franciscojunior commented on May 29, 2024

Ok, no problem!

I'm improving the code a little bit more. I also think I found an issue with TitleFlowIndicator when screen orientation changes: The indicator doesn't persist the previous position. I already got a fix for that and I'll open another issue talking about it and giving you my solution. I think that now we got a 100% working titleflowindicator working with fragments. :)

from android-viewflow.

pakerfeldt avatar pakerfeldt commented on May 29, 2024

Sounds good. I've looked at your code but haven't tried it yet. In your gist you seem to be using an old version of the sources. Could you do a pull of the master repository and either send me another gist or even better, send me a pull request (if you are familiar with that, a gist is fine otherwise)?

from android-viewflow.

franciscojunior avatar franciscojunior commented on May 29, 2024

Sure! I'll get the latest code and send you another gist.

from android-viewflow.

pakerfeldt avatar pakerfeldt commented on May 29, 2024

Closing this issue since Jake already made the awesome indicator-only library (based on your work):
https://github.com/JakeWharton/Android-ViewPagerIndicator

from android-viewflow.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.