Can I set custom onClick on my timeline using fabric sdk?
Asked Answered
P

1

6

I am creating a Twitter client using Fabric but I can not create a custom onClick. I created this custom adapter and tried to create a OnClickListener but not working. Always open in browser tweet.

public class TweetAdapter extends TweetTimelineListAdapter {

    public ArrayList<Long> tweetIds=new ArrayList<Long>();
    public TweetAdapter(Context context, Timeline<Tweet> timeline) {
        super(context, timeline);
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Object rowView = convertView;
        final Tweet tweet = (Tweet)this.getItem(position);
        if(convertView == null) {
            rowView = new CompactTweetView(this.context, tweet);
        } else {
            ((BaseTweetView)convertView).setTweet(tweet);
            ((View)rowView).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    tweetIds.add(tweet.getId());
                }
            });
        }
        return (View)rowView;
    }
}

In BaseTweetView class it is the function type OnClickListener but in this case I can't think of any idea to overwrite.

private void setPermalinkLauncher() {
    BaseTweetView.PermalinkClickListener listener = new BaseTweetView.PermalinkClickListener();
    this.setOnClickListener(listener);
    this.contentView.setOnClickListener(listener);
}

class PermalinkClickListener implements OnClickListener {
    PermalinkClickListener() {
    }

    public void onClick(View v) {
        if(BaseTweetView.this.getPermalinkUri() != null) {
            BaseTweetView.this.scribePermalinkClick();
            BaseTweetView.this.launchPermalink();
        }
    }
}

Any ideas? Thanks

Paisa answered 29/4, 2015 at 3:34 Comment(0)
O
5

Finally I made this works using a custom Adapter (very similar that the one you use in the question). This adapter obtains the resulting view from super implementation and adds an onClickListener to overrides the fabric defaults one:

class CustomTweetTimelineListAdapter extends TweetTimelineListAdapter {

    public CustomTweetTimelineListAdapter(Context context, Timeline<Tweet> timeline) {
        super(context, timeline);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

        //disable subviews to avoid links are clickable
        if(view instanceof ViewGroup){
            disableViewAndSubViews((ViewGroup) view);
        }

        //enable root view and attach custom listener
        view.setEnabled(true);            
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String tweetId = "click tweetId:"+getItemId(position);
                Toast.makeText(context, tweetId, Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }

    //helper method to disable subviews
    private void disableViewAndSubViews(ViewGroup layout) { 
        layout.setEnabled(false); 
        for (int i = 0; i < layout.getChildCount(); i++) { 
            View child = layout.getChildAt(i); 
            if (child instanceof ViewGroup) { 
                disableViewAndSubViews((ViewGroup) child); 
            } else { 
                child.setEnabled(false); 
                child.setClickable(false); 
                child.setLongClickable(false); 
            } 
        } 
    }

}

Full code example here.

Hope it helps.

Orchid answered 4/5, 2015 at 14:33 Comment(2)
To disable all clicks necessary to modify the method: private void disableViewAndSubViews(ViewGroup layout) { layout.setEnabled(false); for (int i = 0; i < layout.getChildCount(); i++) { View child = layout.getChildAt(i); if (child instanceof ViewGroup) { disableViewAndSubViews((ViewGroup) child); } else { child.setEnabled(false); child.setClickable(false); child.setLongClickable(false); } } } Everything else works perfect. Thank you so muchPaisa
any ideas on how to make this work without extending TweetTimeLineListAdapter? I have tried the above code but it doesn't work for me. I am creating a Tweet object and adding it into a FrameLayout, which is the row of the list. I need to select one without invoking default tweet actions.Honeymoon

© 2022 - 2024 — McMap. All rights reserved.