【发布时间】:2016-11-22 13:21:41
【问题描述】:
基本上,我想使用RecyclerView 以编程方式创建布局或扩充布局,但无法这样做。我知道如何通过膨胀来处理 xml,但出于好奇,我想以编程方式进行。
我的适配器代码如下:
public class ACShare extends RecyclerView.Adapter<ACShare.VHShare>{
private List<Integer> listDrawalbe;
public ACShare(Context context) {
TypedArray drawables = context.getResources().obtainTypedArray(R.array.s_array_contact_us_share);
listDrawalbe=new ArrayList<>();
for (int i = 0; i < drawables.length(); ++i)
listDrawalbe.add( drawables.getResourceId(i, -1));
}
@Override
public VHShare onCreateViewHolder(ViewGroup parent, int viewType) {
return new VHShare(LayoutInflater.from(parent.getContext()).inflate(-1,parent,false));
}
@Override
public void onBindViewHolder(VHShare holder, int position) {
holder.imageView.setImageResource(listDrawalbe.get(position));
}
public class VHShare extends RecyclerView.ViewHolder
{
public ImageView imageView;
public LinearLayout ll;
public VHShare(View itemView) {
super(itemView);
Context context= GeneralFunction.getActivity(itemView);
ll = new LinearLayout(context);
ll.setOrientation(android.widget.LinearLayout.VERTICAL);
ll.setLayoutParams(new ViewGroup.LayoutParams(-1,-1));
ll.setBackgroundColor(0x88ff0000);
imageView = new ImageView(context);
imageView.setLayoutParams(new ViewGroup.LayoutParams(-1,-2));
imageView.setBackgroundColor(0x5500ff00);
ll.addView(imageView);
}
}
}
我在onCreateViewHolder 不知道该怎么做。我搜索了StackOverflow,发现了一些链接,但没有一个有用,其中一些如下:
【问题讨论】:
标签: android android-recyclerview layout-inflater