Skip to content
This repository has been archived by the owner on Sep 25, 2020. It is now read-only.

Spork on PagerAdapter #8

Closed
betomaluje opened this issue Mar 21, 2016 · 1 comment
Closed

Spork on PagerAdapter #8

betomaluje opened this issue Mar 21, 2016 · 1 comment
Assignees

Comments

@betomaluje
Copy link

I'm trying to use Spork on PagerAdapter (to display views on a ViewPager) but I'm having problems doing so. For now, I'm creating a custom view, use Spork, and then adding it to the container.

@BindLayout(R.layout.related_item_row)
public class RelatedView extends FrameLayout {

    @BindView(R.id.textView_title)
    TextView textViewTitle;
    @BindView(R.id.imageView_play)
    View imageViewPlay;

    public RelatedView(Context context) {
        super(context);
        Spork.bind(this);
    }

    public void init(final RelatedFeed feed) {
        textViewTitle.setText(feed.getTitle());
        imageViewPlay.setVisibility(!feed.getDataType().equals("news") ? View.VISIBLE : View.GONE);

        setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }
}

So in the PagerAdapter I just have this:

public class RelatedPagerAdapter extends PagerAdapter {

    private Activity context;
    private ArrayList<RelatedFeed> items;

    public RelatedPagerAdapter(Activity context, ArrayList<RelatedFeed> items) {
        Spork.bind(this);

        this.context = context;
        this.items = items;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        final RelatedFeed feed = items.get(position);

        RelatedView view = new RelatedView(context);
        view.init(feed);

        container.addView(view);

        return view;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((View) object);
    }
}

It's working great for now. It would be great if Spork works the same way than in other views.

Another thing, how would be the approach if we didn't use PagerAdapter but instead a FragmentPagerAdapter (or FragmentStatePagerAdapter)?

@KenVanHoeylandt
Copy link
Member

For RecyclerView the solution was clear because the RecyclerView.ViewHolder represents always exactly 1 View. Since this relationship is 1-on-1 and guaranteed, we can do View-binding on an object that isn't a View.

A PagerAdapter (and most other Android adapters) manage View objects directly. So creating a custom View with the bindings in it (as in the example above) seems to be the best way to go forward.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants