RecyclerView使用x行数

时间:2017-05-26 12:13:48

标签: java android android-recyclerview

我想为每个创建的CardView创建x行数的动态视图。为了帮助说明我的想法,这张图片展示了我本来想要做的事情。

Proposed layout

对于每个CardView,我希望能够将任意数量的TableRows放入CardView。因此,一个CardView可以有4行,而下一个CardView可以有2行,依此类推。

编辑:实施

本质上这个视图被多次复制,我想尽量减少必须为每个CardView写出单独的布局,其行数为x行,就像卡存储实现一样,它比我现在拥有的更可重用。当我在这里达到极限时,我已经发布了当前布局的pastebin,其中包含我拥有的行

https://pastebin.com/9RFDGhXv

布局:卡片存储

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        style="@style/CardStyle">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            style="@style/MainConstraints" >

            <TableRow
                android:id="@+id/row_one"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent">

                <TextView
                    android:id="@+id/card_title"
                    style="@style/Title"/>

            </TableRow>

            <TableRow
                android:id="@+id/row_two"
                android:layout_height="wrap_content"
                android:layout_width="0dp"
                app:layout_constraintTop_toBottomOf="@+id/row_one"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent">

                <android.support.constraint.ConstraintLayout>

                    <ImageView
                        android:id="@+id/drawable"
                        android:layout_height="wrap_content"
                        android:layout_width="wrap_content"
                        app:layout_constraintTop_toTopOf="parent"
                        app:layout_constraintLeft_toLeftOf="parent"
                        style="@style/RowDrawableElement"
                        />

                    <TextView
                        android:id="@+id/percentage_descriptor"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        app:layout_constraintTop_toTopOf="parent"
                        app:layout_constraintLeft_toRightOf="@id/drawable"
                        android:text="@string/percentage_descriptor"
                        style="@style/RowTextElement" />

                    <TextView
                        android:id="@+id/percentage_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        app:layout_constraintTop_toTopOf="parent"
                        app:layout_constraintRight_toRightOf="parent"
                        style="@style/RowTextElement" />


                </android.support.constraint.ConstraintLayout>

            </TableRow>

            <TableRow
                android:id="@+id/row_three"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toBottomOf="@id/row_two"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintLeft_toLeftOf="parent">

                <ProgressBar
                    android:id="@+id/progress_bar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:layout_constraintRight_toRightOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintBottom_toBottomOf="parent"
                    style="?android:attr/progressBarStyleHorizontal"
                    />

            </TableRow>

            <TableRow
                android:id="@+id/row_four"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toBottomOf="@id/row_three"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent">

                <android.support.constraint.ConstraintLayout>

                    <TextView
                        android:id="@+id/text_used"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        app:layout_constraintTop_toTopOf="parent"
                        app:layout_constraintLeft_toLeftOf="parent"
                        />

                    <TextView
                        android:id="@+id/text_total"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        app:layout_constraintTop_toTopOf="parent"
                        app:layout_constraintRight_toRightOf="parent"/>

                </android.support.constraint.ConstraintLayout>

            </TableRow>

        </android.support.constraint.ConstraintLayout>

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

</android.support.constraint.ConstraintLayout>

适配器

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

    private Context mContext;
    private List<StorageObjects> mList;

    public StorageAdapter(Context context, List<StorageObjects> list) {
        this.mContext = context;
        this.mList = list;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int i) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card_storage, parent, false);

        return new MyViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        StorageObjects storageObjects = mList.get(position);

        long used = storageObjects.getUsed();
        long total = storageObjects.getTotal();

        holder.mProgress.setMax(0);
        holder.mProgress.setProgress(0);

        holder.mTitle.setText(storageObjects.getTitle());
        MiscUtils.testDrawableIdentifier(mContext.getApplicationContext(), holder.mImageViews, storageObjects.getDrawables());
        holder.mPercentage.setText(storageObjects.getPercentage() + mContext.getString(R.string.percentage));
        holder.mProgress.setMax((int) (total/100000));
        holder.mProgress.setProgress((int) (used/100000));
        holder.mUsed.setText(MiscUtils.humanReadableByteSize(used));
        holder.mTotal.setText(MiscUtils.humanReadableByteSize(total));
    }

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

    static class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.card_title) TextView mTitle;
        @BindViews({R.id.drawable}) List<ImageView> mImageViews;
        @BindView(R.id.percentage_text) TextView mPercentage;
        @BindView(R.id.progress_bar) ProgressBar mProgress;
        @BindView(R.id.text_used) TextView mUsed;
        @BindView(R.id.text_total) TextView mTotal;

        MyViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
} 

类存储

public class Storage extends Fragment {

    private Unbinder mUnbinder;

    private List<StorageObjects> storageList;
    private StorageAdapter adapter;
    private RecyclerView.LayoutManager mLayoutManager;

    @BindArray(R.array.storage_ic_images)
    String mDrawables[];

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

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

    public static Storage newInstance() {
        return new Storage();
    }

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_storage, container, false);
        mUnbinder = ButterKnife.bind(this, view);

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
        initRecyclerView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
    }

    private void initRecyclerView() {

        storageList = new ArrayList<>();
        adapter = new StorageAdapter(getActivity(), storageList);

        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(mLayoutManager = ViewUtils.getLayoutManager(getActivity()));
        mRecyclerView.addItemDecoration(new GridSpacingItemDecoration(2, ViewUtils.dpToPx(
                getActivity(), 10), true));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setAdapter(adapter);

        prepareView();
    }

    @SuppressLint("SetTextI18n")
    private void prepareView(){

        StorageObjects storage
                = new StorageObjects(
                getActivity().getString(R.string.int_storage_title),
                StorageUtils.internalUsed(),
                StorageUtils.internalTotal(),
                StorageUtils.internalPercentage(),
                mDrawables[0]);
        storageList.add(storage);

        storage = new StorageObjects (
                getActivity().getString(R.string.ext_storage_title),
                StorageUtils.externalUsed(getActivity()),
                StorageUtils.externalTotal(getActivity()),
                StorageUtils.externalPercentage(getActivity()),
                mDrawables[1]
        );
        storageList.add(storage);

        adapter.notifyDataSetChanged();

    }
}

1 个答案:

答案 0 :(得分:0)

如果每张卡片的视图行数较少,则可以对编辑文本进行膨胀并将其添加到线性布局。

注意:在滚动视图中添加滚动视图

是一种不好的做法