有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何从片段引用适配器类中的子视图

我有一个儿童博客。xml

<TextView                            
    安卓:id="@+id/tv_fab1"
    adroid:layout_width="wrap_content"                            
    安卓:layout_height="wrap_content"
    安卓:layout_marginRight="15dp"
    安卓:text="Google+"
    安卓:textColor="@color/white"
    安卓:textSize="16dp"
     />

我有一个adapter类,它为recycleview中的每个项目扩展了上面的childblog布局

@Override
public Holder_blog onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.child_blog, parent, false);
    Holder_blog holder_blog = new Holder_blog(view);
    return holder_blog;
}

我试图在我的片段中引用childblog的文本视图,其中包含了recycleview

  @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_blog, container, false);
    System.out.println("Current Class===>>>" + getClass().getSimpleName());
    ((MainActivity) getActivity()).enable_imv_drawer_mainactivity();

    tv_fab1 = (TextView) container.findViewById(R.id.tv_fab1);

我得到了一个空的对象引用。如何在片段中获取childblog的文本视图


共 (1) 个答案

  1. # 1 楼答案

    改变

    tv_fab1 = (TextView) container.findViewById(R.id.tv_fab1);
    

    tv_fab1 = (TextView) view.findViewById(R.id.tv_fab1);