Android开发之RecyclerView多列横向滑动

个人项目总结,勿喷;

1、BaseQuickAdapter类:

public class PositionListAdapter1 extends BaseQuickAdapter {

    private List HScrollViews;
    private RecyclerView smart_recycle1;
    private List dataList;
    private String[] cols;
    private Fragment context;
    private List marketBeans;
    private int num;
    private RequestOptions myOptions = new RequestOptions().centerCrop().diskCacheStrategy(DiskCacheStrategy.ALL);

    public PositionListAdapter1(Fragment context, List mHScrollViews, RecyclerView smart_recycle, List dataList, String[] cols, List marketBeans, int num) {
        super(R.layout.market_list_item1);
        this.HScrollViews = mHScrollViews;
        this.smart_recycle1 = smart_recycle;
        this.dataList = dataList;
        this.cols = cols;
        this.marketBeans = marketBeans;
        this.num = num;
        this.context = context;
    }

    @Override
    protected void convert(BaseViewHolder helper, PostionMarketBean item) {
//设置控件的宽高,及其它属性
        View[] holders;
        View chsv = helper.getView(R.id.item_scroll1);
        LinearLayout healh_ll = chsv.findViewById(R.id.item_scroll_layout1);
        View[] views = new View[cols.length];
        for (int i = 0; i < cols.length; i++) {
            if (i == 0) {
                views[0] = helper.getView(R.id.item_title1);
                if (num == 1) {
                    views[0].setVisibility(View.GONE);
                } else {
                    views[0].setVisibility(View.VISIBLE);
                }
                continue;
            }
            View v = newView(R.layout.row_item_edit_view_cc1, cols[i]);
            TextView Tv_PositionMxview1 = v.findViewById(R.id.Tv_PositionMxview1);

            if (i == 1) {//设置最多行数
                Tv_PositionMxview1.setMaxLines(2);
            } else {
                Tv_PositionMxview1.setMaxLines(1);
            }
            if (i != 0) {
                ViewGroup.LayoutParams params = Tv_PositionMxview1.getLayoutParams();
                params.width = DensityUtil.dip2px(mContext, 90);
                Tv_PositionMxview1.setLayoutParams(params);
            }

            healh_ll.addView(v);
            views[i] = Tv_PositionMxview1;
        }
        helper.itemView.setTag(views);
//联动滑动需要
        addHViews((TradeDetailTableScrollView1) chsv);
        holders = (View[]) helper.itemView.getTag();

//数据赋值
      
        String hyName = s_month + "月" + zt + s_price;

        //code
        ((TextView) holders[0]).setText(hyName);
        holders[0].setTag(R.id.agentweb_webview_id, item);
        holders[0].setOnClickListener(new clickListenter());

        //最新价
        ((TextView) holders[1]).setText(item.Volume1);
        holders[1].setTag(R.id.agentweb_webview_id, item);
        holders[1].setOnClickListener(new clickListenter());

        switch (item.Direction){
            case "1":
                ((TextView) holders[2]).setText("买");
                ((TextView) holders[2]).setTextColor(context.getResources().getColor(R.color.colorZhen));
                break;
            case "2":
                ((TextView) holders[2]).setText("卖");
                ((TextView) holders[2]).setTextColor(context.getResources().getColor(R.color.colorDie));
                break;
        }
        holders[2].setTag(R.id.agentweb_webview_id, item);
        holders[2].setOnClickListener(new clickListenter());

        //浮动盈亏
        //盈亏
        holders[3].setTag(R.id.agentweb_webview_id, item);
        holders[3].setOnClickListener(new clickListenter());

        if (ykFloatMon.equals("0.0") || ykFloatMon.equals("") || ykFloatMon.equals("0")) {
            ((TextView) holders[3]).setText("---");
        } else {
            ((TextView) holders[3]).setText(DoubleParse0.formatDouble4(Double.parseDouble(ykFloatMon + "")) + "");
        }
        String sfloatMoney = DoubleParse0.formatDouble4(Double.parseDouble(item.floatMoney1)) + "";

        if (Double.parseDouble(sfloatMoney) < 0) {

            ((TextView) holders[3]).setTextColor(context.getResources().getColor(R.color.colorDie));
        } else {

           ((TextView) holders[3]).setTextColor(context.getResources().getColor(R.color.colorZhen));
        }


        String bid_price = DoubleParse0.subZeroAndDot2(item.Price);
        //卖价
        ((TextView) holders[4]).setText(bid_price);
        holders[4].setTag(R.id.agentweb_webview_id, item);
        holders[4].setOnClickListener(new clickListenter());

       
        switch (item.Direction) {
            case "1":
                ((TextView) holders[5]).setText(DoubleParse0.subZeroAndDot(item.Deposit));
                ((TextView) holders[6]).setText("--");
                break;
            case "2":
                ((TextView) holders[5]).setText("--");
                ((TextView) holders[6]).setText(DoubleParse0.subZeroAndDot(item.Deposit));
                break;
        }
        holders[5].setTag(R.id.agentweb_webview_id, item);
        holders[5].setOnClickListener(new clickListenter());
        
        holders[6].setTag(R.id.agentweb_webview_id, item);
        holders[6].setOnClickListener(new clickListenter());

    }

    private View newView(int res_id, String tag_name) {

        View itemView = LayoutInflater.from(mContext).inflate(res_id, null);
        itemView.setTag(tag_name);

        return itemView;

    }

//联动
    public void addHViews(final TradeDetailTableScrollView1 hScrollView) {
        if (!HScrollViews.isEmpty()) {
            int size = HScrollViews.size();
            TradeDetailTableScrollView1 scrollView = HScrollViews.get(size - 1);
            final int scrollX = scrollView.getScrollX();
            //第一次满屏后,向下滑动,有一条数据在开始时未加入
            if (scrollX != 0) {
                smart_recycle1.post(new Runnable() {
                    @Override
                    public void run() {
                        //当listView刷新完成之后,把该条移动到最终位置
                        hScrollView.scrollTo(scrollX, 0);
                    }
                });
            }
        }
        HScrollViews.add(hScrollView);
    }
//点击事件
    class clickListenter implements View.OnClickListener {

        private int num;

        public clickListenter() {

        }

        @Override
        public void onClick(View v) {
            click(v);
        }
    }

    private void click(View v) {
        ((PositionFragment2) context).userClick(v);
    }
}

2、TradeDetailTableScrollView1 类:

public class TradeDetailTableScrollView1 extends HorizontalScrollView {

    PositionFragment2 guideFragment1;

    public TradeDetailTableScrollView1(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

    }


    public TradeDetailTableScrollView1(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    public TradeDetailTableScrollView1(Context context) {
        super(context);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        //进行触摸赋值
        guideFragment1.mTouchView1 = this;
        return super.onTouchEvent(ev);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        //当当前的CHSCrollView被触摸时,滑动其它(关键位置在这里)
        if (guideFragment1.mTouchView1 == this) {
        EventBus.getDefault().post(new JLChangeEvent1(l, t, oldl, oldt));
        //   activity.onScrollChanged(l, t, oldl, oldt);
        } else {
        super.onScrollChanged(l, t, oldl, oldt);
        }

    }
}

3、方法:

@Subscribe
public void onEventMainThread(JLChangeEvent hqChangeEvent) {
    for (TradeDetailTableScrollView scrollView : mHScrollViews) {
        //防止重复滑动
        if (mTouchView != scrollView)
            scrollView.smoothScrollTo(hqChangeEvent.l, hqChangeEvent.t);
    }
}

以上三个步骤实现RecyclerView多列横向滑动;

有什么疑问可以直接留言

你可能感兴趣的:(Activity,Recyclerview,Android,RecyclerView,RecyclerView滑动)