【问题标题】:how to add fragment in recycler adapter?如何在回收器适配器中添加片段?
【发布时间】:2018-03-13 05:50:05
【问题描述】:

问题是我想在回收器视图中添加片段每一行数据 并尝试在 XML 中添加片段并在视图持有者中设置。 但发生错误,即重复 id 0x7f070075、标签 null 或父 id 0xffffffff 与 SomeFragment 的另一个片段。 并尝试了第二种方法,即在 viewholder 中动态添加框架布局,并为每个框架布局放置唯一的 id 并动态添加片段。问题是找不到视图的错误

Caused by: java.lang.IllegalArgumentException: Binary XML file line #0: Duplicate id 0x7f070075, tag null, or parent id 0xffffffff with another fragment for com.example.my.samefragmenttest.TestFragment 

尝试调试时,工作正常,而不是运行时.. 我的问题是我想回收列表行中的片段。 有任何想法吗??谢谢你。。

对不起,这是我的代码

public class MainActivity extends AppCompatActivity {
RecyclerViewPager mRecyclerView;
List<Person> peopleList = new ArrayList<Person>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initialData();

    mRecyclerView = (RecyclerViewPager) findViewById(R.id.list);
    mRecyclerView.setLayoutManager(new 
LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));
    mRecyclerView.hasFixedSize();
    final RecyclerAdapter recyclerAdapter = new 
RecyclerAdapter(this,peopleList,this.getSupportFragmentManager());
    mRecyclerView.setAdapter(recyclerAdapter);

 }
}






/**
 * Created by MY on 2018-03-11.
 */

public class RecyclerAdapter extends 
RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
List<Person> personList;
Context context;
FragmentManager fragmentManager;
int count=0;

public RecyclerAdapter(Context context, List<Person> peopleList, 
 FragmentManager supportFragmentManager) {
    this.personList = peopleList;
    this.context = context;
    this.fragmentManager = supportFragmentManager;
}


 @Override
 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    Context context = parent.getContext();
    LayoutInflater inflater = LayoutInflater.from(context);

    // Inflate the custom layout
    View view = inflater.inflate(R.layout.row_item, parent, false);

    // Return a new holder instance
    ViewHolder viewHolder = new ViewHolder(view);
    return viewHolder;
 }

 @Override
 public void onBindViewHolder(ViewHolder holder, int position) {
    Person person = personList.get(position);

    // Set item views based on your views and data model
    holder.id.setText(String.valueOf(person.getId()));
    holder.testFragment = new TestFragment();
 }

 @Override
 public int getItemCount() {
    return personList.size();
 }

 public class ViewHolder extends RecyclerView.ViewHolder{

    TextView id;
    LinearLayout linearLayout;
    FrameLayout frameLayout;
    TestFragment testFragment;

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    public ViewHolder(View itemView) {
        super(itemView);
        id = (TextView)itemView.findViewById(R.id.txt_id);
        linearLayout = (LinearLayout)itemView.findViewById(R.id.linear);
        frameLayout = (FrameLayout)itemView.findViewById(R.id.test_frame);

        if(testFragment==null){

            fragmentManager.findFragmentByTag(TestFragment.TAG);
           // TestFragment.newInstance(fragmentManager);
        }
    }
 }

【问题讨论】:

  • 这是错误原因:java.lang.IllegalArgumentException:二进制 XML 文件第 0 行:重复 id 0x7f070075、标签 null 或父 id 0xffffffff 与 com.example.my.samefragmenttest 的另一个片段。测试片段
  • 发布代码,如何设置适配器
  • 我刚刚添加了代码,但仍在为这个编码而苦苦挣扎,所以有点混乱抱歉!

标签: android android-layout android-studio android-fragments


【解决方案1】:

从 API 17 开始,您可以使用 view.generateViewId() 来获取片段的动态 id,而不会与现有 id 发生交叉错误。

public class ViewHolderInds extends RecyclerView.ViewHolder {
...
    public final int frameId; //Unique identifier for ValueFrame
...
    public ViewHolderInds(View view) {
        super(view);
...
        frameId = view.generateViewId();
        ((FrameLayout) view.findViewById(R.id.valueFrame)).setId(frameId);
...
    }
}

然后,在 RecyclerView.Adapter 中,我们使用来自 ViewHolderInds 的 frameId。

    @Override
    public void onBindViewHolder(@NonNull ViewHolderInds holder, int position) {
        getActivity().getSupportFragmentManager().
                beginTransaction().
                replace(holder.frameId, EditFragment.newInstance(...)).
                commit();
...

【讨论】:

    【解决方案2】:

    我认为您不能将片段用作 Recycler View。

    请参阅此处。 Reference

    【讨论】:

    • 请参考此Answer.@jeheechoi 可能会对您有所帮助。
    • @JaydipKalkani 谢谢,我也搜索过这个。
    • 我尝试了添加动态 id 东西的相同方法,但是当我滚动到下一个位置视图时出现错误找不到视图..:(
    【解决方案3】:

    您必须使用片段容器(视图组)创建布局,并从 onCreateViewHolder() 创建片段并将片段添加到布局中。在 R.layout.item_fragment

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
    

    在你的回收器视图适配器中,你可以这样做,

    在构造函数中,

    public RecyclerViewConstructor(Activity hostActivity) {
        super();
        this.hostActivity = hostActivity;
        lastClickTime = 0;
    }
    
    Activity host
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(mContext).inflate(R.layout.item_fragment, null);
        return new ViewHolder(rootView);
    }
    
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        YourFragment yourFragment = YourFragment.newInstance();
        FragmentManager fm = hostActivity.getSupportFragmentManager();
        fm.addOnBackStackChangedListener(this);
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.fragment_container, fragment, tag);
    }
    

    【讨论】:

      猜你喜欢
      • 2017-01-31
      • 1970-01-01
      • 2017-04-28
      • 1970-01-01
      • 1970-01-01
      • 2018-02-19
      • 1970-01-01
      • 1970-01-01
      • 2017-06-13
      相关资源
      最近更新 更多