美文网首页
Android解决类似新闻文章列表图文混排,禁止图片列表点击效果

Android解决类似新闻文章列表图文混排,禁止图片列表点击效果

作者: 幻化万千戏红尘 | 来源:发表于2018-05-07 11:06 被阅读234次

    问题是:双重recycleview嵌套,,外层recycleview的item里面图片recycleview会获取到焦点,导致点击图片recycleview部分不会有点击效果,不能跳到文章详情页里面去。

    现在想到的解决办法有两种:

    第一种:自定义一个recycleview,重写ontouchEvent事件,返回false,没有触摸事件。再替换掉里面图片recycleview就可以了

    public class CustomRecyclerView extends RecyclerView {

        public CustomRecyclerView(Context context) {

            super(context);

        }

        public CustomRecyclerView(Context context, @Nullable AttributeSet attrs) {

            super(context, attrs);

        }

        public CustomRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {

            super(context, attrs, defStyle);

        }

        @Override

        public boolean onTouchEvent(MotionEvent e) {

    //        return super.onTo uchEvent(e);

            return false;

        }

    //    @Override

    //    public boolean onInterceptTouchEvent(MotionEvent e) {

    ////        return super.onInterceptTouchEvent(e);

    //        return false;

    //    }

    //    @Override

    //    public boolean dispatchTouchEvent(MotionEvent ev) {

    ////        return super.dispatchTouchEvent(ev);

    //        return false;

    //    }

    }

    第二种:在外层recycleview的adapter里面自定义一个点击事件,把外层recycleview的控件点击事件和图片recycleview的点击事件合在一起就行了(感觉第一种的逼格比较高,推荐第一种吧)

    public class ArticalRecycleAdapter extends BaseQuickAdapter {

        private Context context;

        private List mData;

        /*private RelativeLayout llCoverOne;

        private LinearLayout llCoverThree;*/

      private ViewStub llCoverOne;

        private ViewStub llCoverThree;

        private  TextView tv_content;

        private ArticalPictureAdapter articalPictureAdapter;

        public ArticalRecycleAdapter(@Nullable List data, Context context) {

            super(R.layout.item_artical_adapter, data);

            this.context = context;

            this.mData = data;

        }

        @Override

        protected void convert(final BaseViewHolder helper, ArticleListBean item) {

        /*  llCoverOne=helper.getView(R.id.ll_cover_one);

            llCoverThree=helper.getView(R.id.ll_cover_three);*/

          final  int position = helper.getPosition();

            llCoverOne=(ViewStub) helper.getView(R.id.vs_cover_one);

            llCoverThree=(ViewStub) helper.getView(R.id.vs_cover_three);

            TextView tv_name = helper.getView(R.id.tv_name);

            LinearLayout ll_content = helper.getView(R.id.ll_content);

            TextView tv_time = helper.getView(R.id.tv_time);

            TextView tv_read_num = helper.getView(R.id.tv_read_num);

            TextView tv_read_type = helper.getView(R.id.tv_read_type);

            if (1==item.getList().size()){

                llCoverOne.setVisibility(View.VISIBLE);

                llCoverThree.setVisibility(View.GONE);

                tv_content = helper.getView(R.id.tv_content);

                ImageView ivOneCover = helper.getView(R.id.iv_cover);

                Glide.with(context).load(item.getList().get(0)).into(ivOneCover);

            }else {

                llCoverOne.setVisibility(View.GONE);

                llCoverThree.setVisibility(View.VISIBLE);

                tv_content = helper.getView(R.id.tv_content);

                articalPictureAdapter=new ArticalPictureAdapter(null,context);

                CustomRecyclerView rvThreeCover = helper.getView(R.id.rv_img);

                rvThreeCover.setFocusableInTouchMode(false); //设置不需要焦点

                rvThreeCover.requestFocus(); //设置焦点不需要

    //            rvThreeCover.setFocusable(false);

                rvThreeCover.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false));

                rvThreeCover.setAdapter(articalPictureAdapter);

                articalPictureAdapter.setNewData(item.getList());

                articalPictureAdapter.setOnItemClickListener(new OnItemClickListener() {

                    @Override

                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                        onItemClickListener.onItemClick(position);//重点是这里,,里层的图片recycleview点击事件

                    }

                });

            }

            ll_content.setOnClickListener(new View.OnClickListener() {//重点是这里,,外层的recycleview点击事件

                @Override

                public void onClick(View v) {

                    onItemClickListener.onItemClick(position);

                }

            });

    //        helper.addOnClickListener(R.id.ll_content);

    //        int position = helper.getPosition();

    //        if (position ==0){

    //            view_delivder.setVisibility(View.GONE);

    //        }

            tv_name.setText(item.getAuthor());

            tv_time.setText(item.getCreateTime());

            SpannableString ss = new SpannableString(item.getTitle() + " ");

            int len = ss.length();

            //图片

            Drawable d = ContextCompat.getDrawable(mContext, (R.mipmap.ic_vip_typeone));

            d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());

            //构建ImageSpan

            ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);

            ss.setSpan(span, len - 1, len, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

    //        tv_content.setText(ss);

            String lookColour = item.getLookColour();

            if (lookColour!=null&&lookColour.equals("1")){

    //            tv_content.setTextColor(context.getResources().getColor(R.color.red_02));

                tv_content.setTextColor(context.getResources().getColor(R.color.color_828282));

            }else {

                tv_content.setTextColor(context.getResources().getColor(R.color.color_101010));

            }

            String vip = item.getVip();

            if (vip!=null&&vip.equals("1")){

                SpannableString spannableString = new SpannableString("0"+item.getTitle());

                Drawable drawable = context.getResources().getDrawable(R.mipmap.ic_vip_typeone);

                drawable.setBounds(0, 0, 60, 50);

                ImageSpan imageSpan = new ImageSpan(drawable);

                spannableString.setSpan(imageSpan, 0, 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

                tv_content.setMovementMethod(LinkMovementMethod.getInstance());

    //        tv_content.setHighlightColor(Color.parseColor("#36969696"));

                tv_content.setText(spannableString);

            }else {

                tv_content.setText(item.getTitle());

            }

            String typeName = item.getTypeName();

            if (typeName!=null&& !TextUtils.isEmpty(typeName)){

                tv_read_type.setText(typeName);

            }else {

                tv_read_type.setVisibility(View.GONE);

            }

    //        tv_read_type.setText(item.getTypeName());

            tv_read_num.setText("浏览"+item.getBrowseVolume()+"");

    //        tv_time_stop.setText(item.getEndTimet());

    //        tv_active_title.setText(item.getName());

        }

        public OnItemClickListenerCus onItemClickListener;

        public void setOnItemClickListenerCus(OnItemClickListenerCus onItemClickListener){

            this.onItemClickListener = onItemClickListener;

        }

        public interface OnItemClickListenerCus{

            void onItemClick(int position);

        }

    }

    在xml设置焦点属性之类的都试过了,都么有用....(我是个菜鸡..)

    相关文章

      网友评论

          本文标题:Android解决类似新闻文章列表图文混排,禁止图片列表点击效果

          本文链接:https://www.haomeiwen.com/subject/gxemrftx.html