水平RecycleView项目单击滚动到第一项

时间:2017-09-01 12:53:23

标签: android android-recyclerview

大家好,我遇到了水平回收视图的问题,如果向上滚动到右边然后点击任何项目然后它会自动滚动到左边的第一个项目,基本上我必须保持当前点击的位置不滚动开始左,我从堆栈中使用多个东西,但没有一个像scrolltopostion一样工作,创建自定义布局管理器类!请帮忙 以下是我的代码:

  private void setAdapter() {
    recyclerView.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
    recyclerView.setLayoutManager(mLayoutManager);
    mainAdapterClass = new MainAdapterClassw(context, AuctionManager.list);
    recyclerView.setAdapter(mainAdapterClass);
}

以下是适配器类:

    public class MainAdapterClassw extends RecyclerView.Adapter<MainAdapterClassw.ViewHolder> {
    ArrayList<AuctionBean> list;
    Context context;
    public MainAdapterClassw(Context context, ArrayList<AuctionBean> list) {
        super();
        this.context = context;
        this.list = list;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.customelayout, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(v);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {

        auctionBean = list.get(i);
        viewHolder.top_sr_no.setText(auctionBean.getAuction_id());
        viewHolder.view2.setVisibility(View.GONE);
        viewHolder.itemView.setSelected(selectedPos == i);

        viewHolder.bottom_sr_no.setText(auctionBean.getReserve_price());

            text_qantity.setText(qantity1);
            Picasso.with(context)
                    .load("http://traala.com/Bidding/images/date.png")
                    .placeholder(R.mipmap.ic_launcher)
                    .into(productimage);

        }
        viewHolder.setClickListener(new ItemClickListener() {
            @Override
            public void onClick(View view, int position, boolean isLongClick) {
                AuctionBean auctionBean1 = list.get(position);
                Log.d("LogPosition",position+"");

                if (countDownTimer != null) {

                    countDownTimer.cancel();
                }

                    CSPreferences.putString(context, "auction_id", auctionBean1.getAuction_id());
                    ModelManager.getInstance().getAuctionManager().AuctionManager(context, Operations.getallAcuntion(context,
                            ActionString.getall_auctions, "101", auctionBean1.getAuction_id()));


            }
        });

        switch (auctionBean.getAuction_status()) {
            case "status_active.png":
                viewHolder.imgThumbnail.setImageResource(R.drawable.status_active);
                break;
            case "status_pending.png":
                viewHolder.imgThumbnail.setImageResource(status_pending);
                break;
            case "status_over.png":
                viewHolder.imgThumbnail.setImageResource(R.drawable.ic_icon_auction_red);
                break;


        }
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {

        public ImageView imgThumbnail;
        public TextView top_sr_no;
        public TextView bottom_sr_no;
        View view2;
        private ItemClickListener clickListener;

        public ViewHolder(View itemView) {
            super(itemView);
            imgThumbnail = (ImageView) itemView.findViewById(R.id.imageview);
            top_sr_no = (TextView) itemView.findViewById(R.id.textsrno);
            bottom_sr_no = (TextView) itemView.findViewById(R.id.textbottom_srno);
            view2 = (View) itemView.findViewById(R.id.view1);
            itemView.setOnClickListener(this);
            itemView.setOnLongClickListener(this);

        }

        public void setClickListener(ItemClickListener itemClickListener) {
            this.clickListener = itemClickListener;

        }

        @Override
        public void onClick(View view)   {
            clickListener.onClick(view, getAdapterPosition(), false);
            adapterpostion =getAdapterPosition();
            auctionstatus =auctionList.get(getAdapterPosition());

        }

        @Override
        public boolean onLongClick(View view) {
            clickListener.onClick(view, getPosition(), true);
            return true;
        }
    }

}
}

2 个答案:

答案 0 :(得分:0)

请在xml文件

中的父布局中添加此属性

android:descendantFocusability="blocksDescendants"

在RecyclerView中设置适配器,如下所示:

private void setAdapter() {
    recyclerView.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setNestedScrollingEnabled(false);
    recyclerView.setHasFixedSize(true);
    mainAdapterClass = new MainAdapterClassw(context, AuctionManager.list);
    recyclerView.setAdapter(mainAdapterClass);
}

答案 1 :(得分:0)

在我的情况下,这是由于gravity中的EditText属性引起的,就像:android:gravity="center",因此删除此属性可以解决此问题。