在 RecyclerView OnCreateViewHolder 中以编程方式创建或扩充视图
Create or inflate views programmatically in a RecyclerView OnCreateViewHolder
基本上我想创建一个布局或使用 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
时不知道该怎么做。我搜索了 Whosebug 并找到了一些链接,但发现其中 none 个有用,其中一些如下:
- dynamically add layout into getview method of adaptor in android
最后,在得到建议后,我得到了问题的解决方案,
public class ACShare extends RecyclerView.Adapter<ACShare.VHShare>{
private List<Integer> listDrawalbe;
private ProcessedResult listener;
public ACShare(Fragment context) {
listener=(ProcessedResult)context;
TypedArray drawables = context.getActivity().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));
drawables.recycle();
}
@Override
public VHShare onCreateViewHolder(ViewGroup parent, int viewType) {
Context context= GeneralFunction.getActivity(parent);
LinearLayout ll = new LinearLayout(context);
RecyclerView.LayoutParams layoutParams=new RecyclerView.LayoutParams(RecyclerView.LayoutParams.WRAP_CONTENT,
RecyclerView.LayoutParams.WRAP_CONTENT);
ll.setLayoutParams(layoutParams);
ll.setBackgroundColor(GeneralFunction.getColor(context,R.color.color_tranparent));
return new VHShare(ll);
}
@Override
public void onBindViewHolder(VHShare holder, int position) {
holder.imageView.setImageResource(listDrawalbe.get(position));
}
@Override
public int getItemCount() {
return listDrawalbe.size();
}
class VHShare extends RecyclerView.ViewHolder implements View.OnClickListener
{
public ImageView imageView;
VHShare(View itemView) {
super(itemView);
Context context= GeneralFunction.getActivity(itemView);
imageView = new ImageView(context);
LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
int padding= (int) context.getResources().getDimension(R.dimen.elevation_header);
layoutParams.setMargins(padding,padding,padding,padding);
imageView.setLayoutParams(layoutParams);
imageView.setPadding(padding,padding,padding,padding);
LinearLayout linearLayout=(LinearLayout)itemView;
linearLayout.addView(imageView);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
listener.processedResult(getPosition(), CallBackConstants.MESSAGE);
}
}
}
基本上我想创建一个布局或使用 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
时不知道该怎么做。我搜索了 Whosebug 并找到了一些链接,但发现其中 none 个有用,其中一些如下:
- dynamically add layout into getview method of adaptor in android
最后,在得到建议后,我得到了问题的解决方案,
public class ACShare extends RecyclerView.Adapter<ACShare.VHShare>{
private List<Integer> listDrawalbe;
private ProcessedResult listener;
public ACShare(Fragment context) {
listener=(ProcessedResult)context;
TypedArray drawables = context.getActivity().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));
drawables.recycle();
}
@Override
public VHShare onCreateViewHolder(ViewGroup parent, int viewType) {
Context context= GeneralFunction.getActivity(parent);
LinearLayout ll = new LinearLayout(context);
RecyclerView.LayoutParams layoutParams=new RecyclerView.LayoutParams(RecyclerView.LayoutParams.WRAP_CONTENT,
RecyclerView.LayoutParams.WRAP_CONTENT);
ll.setLayoutParams(layoutParams);
ll.setBackgroundColor(GeneralFunction.getColor(context,R.color.color_tranparent));
return new VHShare(ll);
}
@Override
public void onBindViewHolder(VHShare holder, int position) {
holder.imageView.setImageResource(listDrawalbe.get(position));
}
@Override
public int getItemCount() {
return listDrawalbe.size();
}
class VHShare extends RecyclerView.ViewHolder implements View.OnClickListener
{
public ImageView imageView;
VHShare(View itemView) {
super(itemView);
Context context= GeneralFunction.getActivity(itemView);
imageView = new ImageView(context);
LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
int padding= (int) context.getResources().getDimension(R.dimen.elevation_header);
layoutParams.setMargins(padding,padding,padding,padding);
imageView.setLayoutParams(layoutParams);
imageView.setPadding(padding,padding,padding,padding);
LinearLayout linearLayout=(LinearLayout)itemView;
linearLayout.addView(imageView);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
listener.processedResult(getPosition(), CallBackConstants.MESSAGE);
}
}
}