简体   繁体   English

在OnbindViewHolder中膨胀视图

[英]inflating view inside OnbindViewHolder

I am trying to inflate the custom layout inside the OnBindViewHolder of recyclerview. 我试图在recyclerview的OnBindViewHolder中充气自定义布局。 I could inflate the layout and add the view with for loop successfully to the LinearLayout which is the viewgroup of recyclerview's item, but I cannot set the values to the TextViews of the inflated layout. 我可以对布局进行充气,然后将带有for循环的视图成功添加到LinearLayout(这是recyclerview的项目的视图组),但是无法将值设置为充气布局的TextViews。 Here is what I tried so far.Any small hint would be helpful. 这是我到目前为止尝试过的方法,任何小提示都会有所帮助。

@Override
public void onBindViewHolder(ParentViewHolder holder, int position) {

    holder.title.setText(dataModels.get(position).getTopic());

    listModels = dataModels.get(position).getBreakingAndLatestNewsListModels();

    View.OnClickListener myListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String tag = (String) v.getTag();
            Toast.makeText(context, listModels.get(Integer.parseInt(tag)).getTitle(), Toast.LENGTH_SHORT).show();
        }
    };

    for (int i = 0; i < listModels.size(); i++) {
        View v = inflater.inflate(R.layout.news_content_layout, holder.containerLayout, false);
        v.setTag(i + "");
        holder.containerLayout.addView(v);
        v.setOnClickListener(myListener);

        TextView titleTextView = (TextView) v.findViewById(R.id.news_title_text_view);
        titleTextView.setText("jst testing");

    }


}

The alternate way I tried this too, but again there is another ploblem in expanding and collapsing the toolbar 我也尝试过这种替代方法,但是再次出现了另一个问题,即展开和折叠工具栏

I would read up on Expand a RecyclerView . 我会在Expand RecyclerView上阅读。 There is an SO post @ Expandable list with RecyclerView? RecyclerView有一个SO post @ Expandable列表吗? .

I don't know much about it. 我不太了解它。 The idea is you want to expand/collapse from the top items in RecyclerView. 想法是你想从RecyclerView中的顶级项目扩展/折叠。 The webpage above uses animation for expanding/collapsing of items. 上面的网页使用动画来扩展/折叠项目。

If you want a vericlally scroll recycler view inside a recycler view item then it is not possible as i know, Because there will be a scrolling conflict and app going to crash in that case, You can use Horizontally scrolled recycler view inside a vertically scrolled recycler view. 如果你想要一个回收者视图项目中的vericlally滚动回收者视图,那么我不知道,因为在这种情况下会出现滚动冲突和应用程序崩溃,你可以在垂直滚动的回收器中使用水平滚动的回收器视图视图。 I suggest you to go for Linear Layout if you want to achieve the behavior which you are trying to implement. 如果你想实现你想要实现的行为,我建议你去线性布局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM