有 Java 编程相关的问题?

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

java使用RecyclerView显示项目,每个项目都带有一个按钮,该按钮应启动不同的RecyclerView

议题大纲

错误:未连接适配器;跳过布局

调查显示,从未调用新RecyclerView的onBindViewHolder

我已经搜索并尝试了许多替代方案,但到目前为止运气不佳。我曾尝试使用一个Recycleview来处理这两种片段类型,但最终都遇到了相同的问题

第一个RecyclerView按预期工作并显示片段项,每个片段项都有一个按钮,可以打开一个新的RecyclerView,其中列出了来自不同数据集(以及不同格式)的项

我对安卓系统还很陌生,所以如果您能提供任何帮助,我将不胜感激

onBindViewHolder中的onClick代码

holder.checklistButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Context context = view.getContext();
            final Intent intent = new Intent(context, ChecklistActivity.class);
            intent.putExtra("EVENTPOSITION",mValues.indexOf(holder.mItem));
            intent.putExtra("EVENTID", holder.mItem.getEventID().toString());
            context.startActivity(intent);
        }`

一旦创建新活动

setContentView(R.layout.activity_single_fragment);
    FragmentManager fm = this.getSupportFragmentManager();
    Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);
    if (fragment==null){
        fragment = new ChecklistFragment();        fm.beginTransaction().replace(R.id.fragmentContainer,fragment).addToBackStack(null).commit();
        fm.beginTransaction().add(R.id.fragmentContainer,fragment).commit();
    }

为新片段创建OnCreate

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    recyclerView = new RecyclerView(getContext());
    recyclerView.findViewById(R.id.list2);
    Log.i(Configuration.TAG, "Event List Fragment onCreate");
    checkFeed = TheEvents.getInstance();
    Bundle extras = getActivity().getIntent().getExtras();
    String eventID = extras.getString("EVENTID");
    int eventPosition = extras.getInt("EVENTPOSITION");
    System.out.println("this is the eventid " + eventID + " is position: " + eventPosition);
    List<Checklist> feed = checkFeed.getItemAt(eventPosition).getChecklists();
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    adapter = new ChecklistRecyclerViewAdapter(feed, mListener);
    for (int i = 0; i < feed.size(); i++) {
        System.out.println("this is the checklists " + feed.get(i).getChecklistName());
    }
    System.out.println("CREATED ADAPTER: " + adapter);
    recyclerView.setAdapter(adapter);
}

RecyclerView列表布局

<安卓.support.v7.widget.RecyclerView xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:id="@+id/list2"
安卓:name="read.checklist.ChecklistFragment"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:layout_marginLeft="16dp"
安卓:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager"
tools:context="read.checklist.ChecklistFragment"
tools:listitem="@layout/fragment_checklists_item" />

列表项布局

<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:id="@+id/list2item"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:orientation="vertical"
安卓:paddingBottom="@dimen/activity_vertical_margin"
安卓:paddingLeft="@dimen/activity_horizontal_margin"
安卓:paddingRight="@dimen/activity_horizontal_margin"
安卓:paddingTop="@dimen/activity_vertical_margin">    
<TextView
    安卓:id="@+id/checklistName"
    style="@style/seventName"
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:text="@string/checklist_name" />

单帧布局

<FrameLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
安卓:id="@+id/fragmentContainer"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent">
<安卓.support.design.widget.FloatingActionButton
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:clickable="true"
    安卓:layout_gravity="bottom|right"
    安卓:layout_margin="30dp"
    app:fabSize="normal"
    app:srcCompat="@安卓:drawable/ic_menu_add"
    安卓:id="@+id/fabAddEvent" />


共 (0) 个答案