关于RecyclerView设置Horizontal无效的情况

2017-10-21  本文已影响0人  小阿远c

自定义控件代码:
public class AppDetailImageView extends RelativeLayout {
@BindView(R.id.recycler_view)
RecyclerView recyclerView;

public AppDetailImageView(Context context) {
    this(context, null);
}

public AppDetailImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    LayoutInflater.from(getContext()).inflate(R.layout.app_detail_image, this);
    ButterKnife.bind(this, this);

}

public void bindView(DetailsBean dataList) {
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
    linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
    recyclerView.setLayoutManager(linearLayoutManager);
    recyclerView.setAdapter(new AppDetailImageAdapter(getContext(),dataList));

}

}

Adapter代码:
public class AppDetailImageAdapter extends RecyclerView.Adapter<AppDetailImageAdapter.ViewHoler> {
private Context context;
private DetailsBean dataList;

public AppDetailImageAdapter(Context context ,DetailsBean dataList){
    this.context = context;

    this.dataList = dataList;
}
@Override  //ViewHodler 赋予一个xml 界面
public AppDetailImageAdapter.ViewHoler onCreateViewHolder(ViewGroup parent, int viewType) {
    ViewHoler viewHoler = new ViewHoler(LayoutInflater.from(context).inflate(R.layout.app_datail_image, parent, false));
    return viewHoler;
}

@Override //给控件赋值
public void onBindViewHolder(AppDetailImageAdapter.ViewHoler holder, int position) {
    Glide.with(context).load(Constant.IMAGE_URL+dataList.getScreen().get(position)).into(holder.img);
}

@Override  //RecyclerView 一共有多少个子项.
public int getItemCount() {
    return dataList.getScreen().size();
}

public class ViewHoler extends RecyclerView.ViewHolder{
    ImageView img;
    public ViewHoler(View itemView) {
        super(itemView);
        img = (ImageView) itemView.findViewById(R.id.image);
    }
}
}

布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
</android.support.v7.widget.RecyclerView>
</LinearLayout>




![image.png](https://img.haomeiwen.com/i7644574/51f7b881e15b9a77.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

然后将自定义控件布局和item布局的LinearLayout删除后

image.png image.png image.png
上一篇下一篇

猜你喜欢

热点阅读