我的RecyclerView里面的tablayout只显示一个项目

时间:2017-10-09 04:41:15

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

我的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);
        }

    }
}

我调试了但数据列表大小超过了一个!!

2 个答案:

答案 0 :(得分:0)

这是我的错。我使layoutManger方向水平而不是垂直。我知道那个项目不是一个。但我把它与标签项误认了。对不起你的投票。

所以我改变了

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

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

答案 1 :(得分:0)

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

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