【问题标题】:My RecyclerView inside tablayout showing only one item我在 tablayout 中的 RecyclerView 只显示一项
【发布时间】:2023-04-04 09:09:02
【问题描述】:

我的tablayout 中有许多选项卡,但每个选项卡上只包含一项。它显示数据列表大小大于 1,我的 RecyclerView 项目高度为 wrap_content

这是我生成标签的方式

TabPagerAdapter adapter = new TabPagerAdapter(getSupportFragmentManager());
        for (Plan plan: offersPojo.getPlans()){
            if (plan.getPlan().getError() == null)
            adapter.addFragment(OffersItem.newInstance(plan), plan.getName());
        }
        viewPager.setAdapter(adapter);

还有我的TabPagerAdapter

    public class TabPagerAdapter extends FragmentPagerAdapter {

    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public TabPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

OffersItem 片段

public class OffersItem extends Fragment {
     private OnFragmentInteractionListener mListener;

    @BindView(R.id.offerRecyclerView)
    RecyclerView mRecyclerView;

    private Unbinder unbinder;

    public OffersItem() {
        // Required empty public constructor
    }


    public static OffersItem newInstance(Plan plan) {
        OffersItem fragment = new OffersItem();
        Bundle args = new Bundle();
        args.putSerializable(Constants.PLANS,plan);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_offers, container, false);
        unbinder= ButterKnife.bind(this,view);
        RecyclerView.LayoutManager mPMLayoutManger = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
        mRecyclerView.setLayoutManager(mPMLayoutManger);
        mRecyclerView.setHasFixedSize(true);
        if (getArguments().getSerializable(Constants.PLANS) != null){
            Plan plan = (Plan) getArguments().getSerializable(Constants.PLANS);
            List<Datum> dataList = plan.getPlan().getData();
            OffersItemAdapter adapter = new OffersItemAdapter(dataList,getActivity());
            mRecyclerView.setAdapter(adapter);
        }
        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
        unbinder.unbind();
    }


    public interface OnFragmentInteractionListener {
        void onFragmentInteraction(Uri uri);
    }
}

我的项目布局

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/offerTextHead"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="TextView"
        android:textColor="@color/colorSubTextView"
        android:textSize="16sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/offerContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginStart="8dp"
        android:padding="5dp"
        android:text="@string/new_message_notification_placeholder_text_template"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/offerTextHead" />

    <TextView
        android:id="@+id/offerAmount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/offer_amount_background"
        android:paddingBottom="2dp"
        android:paddingEnd="10dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingStart="10dp"
        android:paddingTop="2dp"
        android:text="0.00"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/offerContent" />

    <TextView
        android:id="@+id/offerValidity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:text="TextView"
        android:textColor="@color/colorDisabled"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/offerContent" />
</android.support.constraint.ConstraintLayout>

还有我的RecyclerViewAdapter

public class OffersItemAdapter extends RecyclerView.Adapter<OffersItemAdapter.MyViewHolder> {

    List<Datum> data = null;
    Activity activity = null;


    public OffersItemAdapter(List<Datum> data, Activity activity) {
        this.data = data;
        this.activity = activity;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.offers_item, parent, false);
        return new MyViewHolder(v);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Datum itemData = data.get(position);
        holder.heading.setText(itemData.getRechargeShortdesc());
        holder.offerContent.setText(itemData.getRechargeLongdesc());
        holder.offerAmount.setText(itemData.getRechargeAmount());
        holder.offerValidity.setText(String.format("Validity: %s", itemData.getRechargeValidity()));
    }

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

    static class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.offerTextHead)
        TextView heading;
        @BindView(R.id.offerContent)
        TextView offerContent;
        @BindView(R.id.offerAmount)
        TextView offerAmount;
        @BindView(R.id.offerValidity)
        TextView offerValidity;
        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

    }
}

我调试了,但是数据列表的大小大于一!!

【问题讨论】:

  • 在项目布局中将宽度设置为 wrap_content 到 ConstraintLayout
  • @Pavya 没有。它不工作

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


【解决方案1】:

根据我的理解,您在回收站视图中一次只能看到一个,因为您将 LayoutManger 属性设置为 Horizo​​ntal 意味着您可以水平滚动列表,因此您需要将布局管理器设置为垂直,如下所示,

RecyclerView.LayoutManager mPMLayoutManger = new LinearLayoutManager(getActivity());
 mRecyclerView.setLayoutManager(mPMLayoutManger);

【讨论】:

    【解决方案2】:

    这是我的错。我将layoutManger 设置为水平方向而不是垂直方向。我知道那是项目不是一个。但我把它误认为是标签项目。很抱歉您的投票失败。

    所以我改变了

    RecyclerView.LayoutManager mPMLayoutManger = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
    

    RecyclerView.LayoutManager mPMLayoutManger = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
    

    【讨论】:

      猜你喜欢
      • 2022-01-03
      • 2019-01-26
      • 1970-01-01
      • 2021-01-09
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多