RecyclerView 实现横向滑动

1、布局

2、代码

mRvFunctionView = contentView.findViewById(R.id.rv_function_view);
mRvFunctionView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
FunctionViewListAdapter listAdapter = new FunctionViewListAdapter(this);
mRvFunctionView.setAdapter(listAdapter);

其中的adapter和ViewHolder,和竖向的RecyclerView一样

public class FunctionViewListAdapter extends RecyclerView.Adapter {

    private List data;
    private SortItemSelectedListener itemSelectedListener;


    public FunctionViewListAdapter(SortItemSelectedListener galleryItemSelectedListener) {
        this.itemSelectedListener = galleryItemSelectedListener;
    }

    public void setData(List data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public FunctionViewListItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_function_view, parent, false);
        return new FunctionViewListItemHolder(view, itemSelectedListener);
    }

    @Override
    public void onBindViewHolder(FunctionViewListItemHolder holder, int position) {
        ViewItemBean item = data.get(position);
        holder.bind(item);
    }

    @Override
    public void onBindViewHolder(FunctionViewListItemHolder holder, int position, List payloads) {
        onBindViewHolder(holder, position);

    }

    @Override
    public int getItemCount() {
        if (data != null) {
            return data.size();
        }
        return 0;
    }
} 
  

其中的viewholder

public class FunctionViewListItemHolder extends RecyclerView.ViewHolder {

    private SortItemSelectedListener itemSelectedListener;
    private ViewItemBean mediaItem;
    private ImageView photosIcon;
    private TextView itemName;
    private final LinearLayout mItemContent;

    public FunctionViewListItemHolder(View itemView, SortItemSelectedListener itemSelectedListener) {
        super(itemView);
        photosIcon = (ImageView) itemView.findViewById(R.id.iv_grid_item_icon);
        mItemContent = (LinearLayout) itemView.findViewById(R.id.ll_grid_item_content);
        itemName = (TextView) itemView.findViewById(R.id.tv_item_name);
        this.itemSelectedListener = itemSelectedListener;
    }

    public void bind(final ViewItemBean mediaItem) {
        this.mediaItem = mediaItem;
        int type = mediaItem.getType();
        switch (type) {
            case 0:
                photosIcon.setImageResource(R.mipmap.personal_icon_phone);
                itemName.setText(GlobalApplication.getInstance().getResources().getString(R.string.phone_backup));
                break;
            case 1:
                photosIcon.setImageResource(R.mipmap.personal_icon_recycle);
                itemName.setText(GlobalApplication.getInstance().getResources().getString(R.string.recycle_bin));
                break;
            case 2:
                photosIcon.setImageResource(R.mipmap.system_status_icon);
                itemName.setText(GlobalApplication.getInstance().getResources().getString(R.string.system_status_check));
                break;
            case 3:
                photosIcon.setImageResource(R.mipmap.control_panal_icon);
                itemName.setText(GlobalApplication.getInstance().getResources().getString(R.string.control_panel));
                break;
        }
        mItemContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemSelectedListener != null) {
                    itemSelectedListener.clickItem(mediaItem);
                }
            }
        });
    }
}

 

你可能感兴趣的:(RecyclerView 实现横向滑动)