Как поместить заголовок раздела в RecyclerView? - PullRequest
0 голосов
/ 31 августа 2018

У меня уже есть список переработчиков, но затем я хочу поставить ярлык для каждого типа списка продуктов.

Я хочу достичь вот так

я не знаю, как я знаю, как составить список вот так

В каждой вкладке есть две вкладки (История платежей и Ожидающий платеж). Я уже закончил со списком, но там нет заголовка, как на картинке

Итак, я хотел спросить, как мне добавить заголовок в мой список RecyclerView для каждого типа продукта ??

Вот мой код:

item_transaction.xml

<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:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="85dp"
    android:background="?android:attr/selectableItemBackground"
    android:clickable="true"
    android:focusable="true">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="4dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/item_date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Textview"
                android:gravity="center"
                android:textSize="@dimen/text_regular"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                android:textColor="@android:color/black"
                />

            <android.support.constraint.ConstraintLayout
                android:id="@+id/linearLayout4"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="55dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="8dp"
                android:baselineAligned="true"
                android:orientation="horizontal"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintLeft_toRightOf="@+id/item_marker"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <android.support.constraint.ConstraintLayout
                    android:id="@+id/linearLayout2"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintWidth_percent="0.6"
                    tools:layout_conversion_absoluteHeight="40dp"
                    tools:layout_conversion_absoluteWidth="207dp">

                    <TextView
                        android:id="@+id/item_title"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:ellipsize="end"
                        android:singleLine="true"
                        android:text="TextView"
                        android:textColor="@android:color/black"
                        android:textSize="24sp"
                        app:layout_constraintBottom_toTopOf="@+id/item_ref_no"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        app:layout_constraintVertical_chainStyle="packed"
                        tools:layout_conversion_absoluteHeight="19dp"
                        tools:layout_conversion_absoluteWidth="58dp" />

                    <TextView
                        android:id="@+id/item_ref_no"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:ellipsize="end"
                        android:singleLine="true"
                        android:text="TextView"
                        android:textSize="@dimen/text_regular"
                        android:textColor="@android:color/black"
                        app:layout_constraintBottom_toBottomOf="@id/item_trans_no"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="@+id/item_title"
                        app:layout_constraintTop_toBottomOf="@+id/item_title"
                        tools:layout_conversion_absoluteHeight="19dp"
                        tools:layout_conversion_absoluteWidth="58dp" />

                    <TextView
                        android:id="@+id/item_trans_no"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:ellipsize="end"
                        android:singleLine="true"
                        android:text="TextView"
                        android:textColor="@android:color/black"
                        android:textSize="@dimen/text_regular"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="@+id/item_ref_no"
                        app:layout_constraintTop_toBottomOf="@+id/item_ref_no"
                        tools:layout_conversion_absoluteHeight="19dp"
                        tools:layout_conversion_absoluteWidth="58dp" />

                </android.support.constraint.ConstraintLayout>

                <android.support.constraint.ConstraintLayout
                    android:id="@+id/linearLayout3"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:orientation="vertical"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toEndOf="@+id/linearLayout2"
                    app:layout_constraintTop_toTopOf="parent"
                    tools:layout_conversion_absoluteHeight="40dp"
                    tools:layout_conversion_absoluteWidth="344dp">

                    <TextView
                        android:id="@+id/item_amount"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:ellipsize="end"
                        android:gravity="center"
                        android:singleLine="true"
                        android:text="TextView"
                        android:textSize="@dimen/text_regular"
                        android:textColor="@color/colorPrimary"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        tools:layout_conversion_absoluteHeight="19dp"
                        tools:layout_conversion_absoluteWidth="58dp" />


                </android.support.constraint.ConstraintLayout>

            </android.support.constraint.ConstraintLayout>

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_marginRight="5dp"
                android:layout_weight=".4"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:srcCompat="@drawable/ic_keyboard_arrow_right" />

        </android.support.constraint.ConstraintLayout>
    </android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>

fragment_payment_history.xml

<android.support.v7.widget.RecyclerView 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:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:scrollbars="vertical"
    tools:context=".ui.fragments.PaymentHistoryFragment"
    tools:listitem="@layout/item_transactions">
</android.support.v7.widget.RecyclerView>

PaymentHistoryFragment.java

    public class PaymentHistoryFragment extends Fragment{

    // TODO: Customize parameter argument names
    private static final String ARG_COLUMN_COUNT = "column-count";
    // TODO: Customize parameters
    private int mColumnCount = 1;
    private PaymentHistoryFragment.OnListFragmentInteractionListener mListener;

    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public PaymentHistoryFragment() {
    }

    // TODO: Customize parameter initialization
    @SuppressWarnings("unused")
    public static PaymentHistoryFragment newInstance(int columnCount) {
        PaymentHistoryFragment fragment = new PaymentHistoryFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_COLUMN_COUNT, columnCount);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate( R.layout.fragment_payment_history, container, false);

        // Set the adapter
        if (view instanceof RecyclerView) {
            Context context = view.getContext();
            RecyclerView recyclerView = (RecyclerView) view;
            if (mColumnCount <= 1) {
                recyclerView.setLayoutManager(new LinearLayoutManager(context));
            } else {
                recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
            }
            recyclerView.setAdapter(new PaymentHistoryRecyclerViewAdapter(context, prepareListData(), mListener));
        }
        return view;
    }

    private List<Transactions> prepareListData() {
        List<Transactions> transactionsList = new ArrayList<>();

        Date date = CommonUtils.stringToDate("08/20/2018");
        date = (date == null) ? new Date() : date;
        Transactions transactions = new Transactions("LG Refrigerator", "0123456789", "0123456789", 50000, date);
        transactionsList.add(transactions);

        date = CommonUtils.stringToDate("09/25/2018");
        date = (date == null) ? new Date() : date;
        transactions = new Transactions("Samsung TV", "0123456789", "0123456789", 35000, date);
        transactionsList.add(transactions);

        return transactionsList;
    }

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

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

    public interface OnListFragmentInteractionListener {
        // TODO: Update argument type and name
        void onListFragmentInteraction(Transactions transactions);
    }
}

PaymentHistoryRecyclerViewAdapter

public class PaymentHistoryRecyclerViewAdapter extends RecyclerView.Adapter<PaymentHistoryRecyclerViewAdapter.ViewHolder> {
private List<Transactions> mTransactionList;
private LayoutInflater mInflater;
private final PaymentHistoryFragment.OnListFragmentInteractionListener mListener;

// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
    public TextView mTitle, mDate, mRefNo, mTransNo, mAmount;
    public final View mView;

    public ViewHolder(View v) {
        super(v);
        mView = v;
        mTitle = v.findViewById( R.id.item_title);
        mRefNo = v.findViewById(R.id.item_ref_no);
        mTransNo = v.findViewById(R.id.item_trans_no);
        mAmount = v.findViewById(R.id.item_amount);
        mDate = v.findViewById(R.id.item_date);
        mTransNo = v.findViewById(R.id.item_trans_no);
    }
}

// Provide a suitable constructor (depends on the kind of data set)
public PaymentHistoryRecyclerViewAdapter(Context context, List<Transactions> transactionList, PaymentHistoryFragment.OnListFragmentInteractionListener listener) {
    mInflater = LayoutInflater.from(context);
    mListener = listener;
    this.mTransactionList = transactionList;
}

// Create new views (invoked by the layout manager)
@Override
public PaymentHistoryRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    // create a new view
    View v = mInflater.inflate(R.layout.item_transactions, parent, false);
    PaymentHistoryRecyclerViewAdapter.ViewHolder vh = new PaymentHistoryRecyclerViewAdapter.ViewHolder(v);
    return vh;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(PaymentHistoryRecyclerViewAdapter.ViewHolder holder, int position) {
    // - get element from your data set at this position
    // - replace the contents of the view with that element
    Transactions transactions = mTransactionList.get(position);
    String dateFormat = "mm/dd/yyyy";
    String transactionsAmount = String.format( Locale.US, "₱ %,d", transactions.getAmount());
    String transactionsRefNo = String.format("Reference No. %S", transactions.getRefNo());
    String transactionsTransNo = String.format("Transaction No. %S", transactions.getTransNo());
    String transactionsTitle = String.format("%S", transactions.getTitle());

    holder.mTitle.setText(transactionsTitle);
    holder.mRefNo.setText(transactionsRefNo);
    holder.mTransNo.setText(transactionsTransNo);
    holder.mAmount.setText(transactionsAmount);
    holder.mDate.setText( CommonUtils.getFormattedDate(transactions.getDate(), dateFormat));
    holder.mView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("Did click", " pending card");
            if (null != mListener) {
                // Notify the active callbacks interface (the activity, if the
                // fragment is attached to one) that an item has been selected.
                mListener.onListFragmentInteraction(transactions);
            }
        }
    });
}

// Return the size of your data set (invoked by the layout manager)
@Override
public int getItemCount() {
    return mTransactionList.size();
}

}

1 Ответ

0 голосов
/ 19 сентября 2018

Вы найдете свой пример здесь Несколько типов просмотра

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...