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

后端 未结 1 1773
有刺的猬
有刺的猬 2021-01-12 16:59

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. Alway

相关标签:
1条回答
  • 2021-01-12 17:20

    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.

    0 讨论(0)
提交回复
热议问题