如何在recyclerview的Viewholder内部制作动态商品行?

时间:2018-10-26 10:30:23

标签: android recycler-adapter

所以我有一个水平滚动视图,里面有recyclerView。 我希望在onCreateViewHolder中膨胀的项目行是动态的,而不是静态的xml,因为我不知道会有多少列,而且我还需要从webservice上设置一些文本,我无法弄清楚如何从Viewholder中实现onBind方法

这是我的适配器

@NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        RecyclerView.ViewHolder holder;
      //  View v = inflater.inflate(R.layout.exec_category_sales_row,parent, false);
        CustomView itemView = new CustomView(parent.getContext());

        itemView.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        ));
        return new ViewHolder(itemView);


    }
 @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        final ViewHolder viewHolder = (ViewHolder) holder;

        ((ViewHolder) holder).customView.setUser(arylstCategoryWiseSales.get(position));

    }

private class ViewHolder extends RecyclerView.ViewHolder {

private CustomView customView;
        public ViewHolder(View v) {
            super(v);

            customView = (CustomView) v;
        }

我的customview类

public class CustomView extends RelativeLayout {
    private CategoryWiseSalesModel user;
    private TextView textView;

    // override all constructors to ensure custom logic runs in all cases
    public CustomView(Context context) {
        this(context, null);
    }
    public CustomView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public CustomView(
            Context context,
            AttributeSet attrs,
            int defStyleAttr,
            int defStyleRes
    ) {
        super(context, attrs, defStyleAttr, defStyleRes);


        inflate(getContext(), R.layout.custom_layout, this);
        textView =  findViewById(R.id.textView);
    }
//called in onBindViewHolder
    public void setUser(CategoryWiseSalesModel newUser) {
        user = newUser;
        textView.setText(user.getLights());
    }
}

不同的视图类型对我不起作用,因为项目行是完全动态的。 我还考虑过使用tablelayout,但是我在这里进行了onScroll的延迟加载逻辑。

如何处理?

1 个答案:

答案 0 :(得分:0)

动态TextViews生成

这是您的示例代码

  

用于调用和设置适配器的主类代码。

public class MainActivity extends AppCompatActivity {
        RecyclerView mRecycleview;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            List<String> mList = new ArrayList<>();
            mList.add("Hello");
            mList.add("World");

            mRecycleview = findViewById(R.id.recycleview);


            mRecycleview.setLayoutManager(new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false));
            PlannerAdapter mPlannerAdapter = new PlannerAdapter(MainActivity.this, mList, 5);//Contuctor having context, datalist and number of textviews. i.e you can get it from your data on runtime
            mRecycleview.setAdapter(mPlannerAdapter);
            mPlannerAdapter.notifyDataSetChanged();

        }
    }
  

以下是用于生成动态TextView的适配器代码

    public class PlannerAdapter extends RecyclerView.Adapter<PlannerAdapter.RecyclerViewHolder> {
    private Context context;
    LinearLayout mainlayout;
    List<String> strings;
    List<String> tvIds;
    int mTextviewCount;

    public PlannerAdapter(Context context, List s, int mTexviewQuantity) {
        this.context = context;
        this.strings = s;
        this.mTextviewCount = mTexviewQuantity;
    }


    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapterlayout, viewGroup, false);
        mainlayout = view.findViewById(R.id.mainlayout);
        tvIds = new ArrayList<>();

        for (int ind = 0; ind < mTextviewCount; ind++) {
            LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
                    130, 120);
            TextView tv = new TextView(context);
            tv.setLayoutParams(lparams);
            tv.setText("test");
            tv.setTag("mytv" + ind);
            tvIds.add("mytv" + ind);

            mainlayout.addView(tv);
        }
        RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view);
        return recyclerViewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerViewHolder recyclerViewHolder, int position) {

        for (int index = 0; index < tvIds.size(); index++) {
            recyclerViewHolder.mTextViewList.get(index).setText(strings.get(position) + "  " + index + "  ");
        }

    }

    @Override
    public int getItemCount() {

        return strings.size();
    }

    class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public android.widget.TextView tvShopName, tvDateTime;
        List<TextView> mTextViewList = new ArrayList<>();


        public RecyclerViewHolder(View itemView) {
            super(itemView);

            for (int ind = 0; ind < tvIds.size(); ind++) {
                TextView mTextView = new TextView(context);
                mTextView = itemView.findViewWithTag(tvIds.get(ind));
                mTextViewList.add(mTextView);
            }
        }

        @Override
        public void onClick(View view) {

        }
    }

}
  

这是关于完成动态textView生成和示例的问题   代码已附加。希望对您有帮助,请询问是否查询