gpt4 book ai didi

java - Fragment 中的 RecyclerView

转载 作者:行者123 更新时间:2023-12-02 03:57:44 28 4
gpt4 key购买 nike

我正在遵循 Android 文档示例 hereFragment 内设置 RecyclerView

我把我的代码粘贴在下面,没有太多。 (唯一的区别是我使用的是 Fragment 而不是 Activity)。

我收到的错误是:

java.lang.IllegalStateException:指定的子级已经有父级。您必须首先在子级的父级上调用removeView()。

网上解决这个问题的所有方法都是在 fragment 的布局膨胀中添加 false 参数。但我已经这样做了,但仍然收到错误。

我的代码:

主要 Activity

...
mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.containerView,new NewsFragment()).commit();
...

新闻 fragment

public class NewsFragment extends Fragment {

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.news_layout,container, false);

mRecyclerView = (RecyclerView) v.findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);

mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);

mAdapter = new NewsAdapter(new String[]{"hello","world","qwert","test","greg","peck"});
mRecyclerView.setAdapter(mAdapter);

return v;

}
}

新闻适配器

public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {
private String[] mDataset;

public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;

public ViewHolder(TextView tv) {
super(tv);
mTextView = tv;
}
}

public NewsAdapter(String[] myDataset) {
mDataset = myDataset;
}

@Override
public NewsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.news_item, parent, false);

TextView tv = (TextView)v.findViewById(R.id.textView2);
ViewHolder vh = new ViewHolder(tv);
return vh;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.mTextView.setText(mDataset[position]);
}

@Override
public int getItemCount() {
return mDataset.length;
}
}

任何帮助将不胜感激!

最佳答案

错误在这一行:

TextView tv = (TextView)v.findViewById(R.id.textView2);
ViewHolder vh = new ViewHolder(tv);

将其更改为:

TextView tv = (TextView)v.findViewById(R.id.textView2);
ViewHolder vh = new ViewHolder(v);

TextView 已经有父 View 。这就是您收到此错误的原因。

更新的代码:

public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {
private String[] mDataset;

public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;

public ViewHolder(View view) {
super(tv);
mTextView= (TextView)view.findViewById(R.id.textView2);
}
}

public NewsAdapter(String[] myDataset) {
mDataset = myDataset;
}

@Override
public NewsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.news_item, parent, false);


ViewHolder vh = new ViewHolder(v);
return vh;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.mTextView.setText(mDataset[position]);
}

@Override
public int getItemCount() {
return mDataset.length;
}
}

关于java - Fragment 中的 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35266284/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com