无法在ViewPager中为recyclerView充气

时间:2018-05-24 15:52:44

标签: android android-recyclerview android-viewpager android-tablayout recyclerview-layout

我在浏览viewPager中的回收站视图时遇到问题。 以下代码属于我的MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    viewPager = findViewById(R.id.pager);
    tabLayout = findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);//setting tab over viewpager

    viewPager.setAdapter(new VeiwPagerAdapter());
}

以下是我的ViewPagerAdapter:

public class VeiwPagerAdapter extends PagerAdapter {
private RecyclerView recyclerView;
private List<Pojo> pojoList = new ArrayList<>();

@Override
public int getCount() {
    return 2;
}

@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
    return false;
}

@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
    if (position == 0) {
        LayoutInflater inflater = LayoutInflater.from(container.getContext());
        View view = inflater.inflate(R.layout.recycler_view_holder, container, false);
        recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(container.getContext()));
        pojoList.add(new Pojo("Random field 1", "Random field 1"));
        recyclerView.setAdapter(new RecycAdapter(pojoList));
        container.addView(view);
        return view;
    } else {
        LayoutInflater inflater = LayoutInflater.from(container.getContext());
        View view = inflater.inflate(R.layout.recycler_view_holder, container, false);
        recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(container.getContext()));
        pojoList.add(new Pojo("Random field 1", "Random field 1"));
        recyclerView.setAdapter(new RecycAdapter(pojoList));
        container.addView(view);
        return view;
    }
}

@Nullable
@Override
public CharSequence getPageTitle(int position) {
    return position == 0 ? "TAB 1" : "TAB 2";
}

RecycAdapter代码:

public class RecycAdapter extends RecyclerView.Adapter<RecycViewHolder> {
    private List<Pojo> pojoArrayList;

    public RecycAdapter(List<Pojo> pojoArrayList) {
        this.pojoArrayList = pojoArrayList;
    }

    @NonNull
    @Override
    public RecycViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        LayoutInflater inflater = LayoutInflater.from(parent.getContext());

        View v = inflater.inflate(R.layout.item_layout, parent, false);

        return new RecycViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecycViewHolder holder, int position) {
        Pojo pojo = pojoArrayList.get(position);
        holder.textView1.setText(pojo.getText1());
        holder.textView2.setText(pojo.getText2());
    }

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

添加了ViewHolder代码。不确定viewHolder实现是否有问题。

public class RecycViewHolder extends RecyclerView.ViewHolder {

    public TextView textView1, textView2;

    public RecycViewHolder(View itemView) {
        super(itemView);
        this.textView1 = itemView.findViewById(R.id.textvie1);
        this.textView2 = itemView.findViewById(R.id.textvie2);
    }
}

每个标签下的输出都没有。 有人可以帮助我解决我出错的地方

3 个答案:

答案 0 :(得分:1)

为什么你的MainActivity上有代码块?

recyclerView = view.findViewById(R.id.recyclerView);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    pojoList.add(new Pojo("Random field 1", "Random field 1"));
    recyclerView.setAdapter(new RecycAdapter(pojoList));
}

我可以看到与R.id.recyclerView相同的一些冲突。

如果您希望ViewPager在每个标签上显示两个不同的RecyclerViews,那么您的MainActivity应该只包含您的观看容器。您能否就您需要达到的目标提供更多背景信息?

您是否尝试在每个标签上显示简单且不同的TextView /布局?您可能有一些XML问题。

答案 1 :(得分:0)

您需要将RecyclerView充气到ViewPager的每个页面,换句话说,将此代码放在“instantiateItem”方法中:

recyclerView = view.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
pojoList.add(new Pojo("Random field 1", "Random field 1"));
recyclerView.setAdapter(new RecycAdapter(pojoList));

答案 2 :(得分:0)

最后,找到了一个解决方案,覆盖方法isViewFromObject早先返回false,导致问题在更改为代码时如下所示工作正常。

@Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }