【问题标题】:create a list of cardview based on items count android根据项目计数android创建一个cardview列表
【发布时间】:2018-07-11 02:35:12
【问题描述】:

您好,我想根据 InfoGenBibliotheque 类中的字符串数量来制作 cardView 列表。我真的不知道如何根据项目数创建 cardViews 列表。如果有人可以提供帮助,我将不胜感激。我读过我必须创建一个 cardview 布局文件才能从中创建一个列表。 //已编辑 所以我设法使用 listView 和 BaseAdapter 来实现它。但是当我执行应用程序时,找不到边距。

InfoGenBibliotheque 类:

   public class InfoGenBibliotheque {

    private String SalutaionsAnglais [] = {
            "Certainly!",
            "Good afternoon.",
            "Good evening sir.",
            "Good Luck.",
            "Good morning."
    };

    private String SalutaionsFrancais [] = {
            "Certainement!",
            "Bon après-midi.",
            "Bonsoir Monsieur.",
            "Bonne chance.",
            "Bonjour."
    };
}

我的 XML activity_info_generales 文件:

    <?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"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:context=".ui.InfoGeneralesActivity">

            <ListView
                android:id="@+id/listView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                android:layout_margin="10dp">

            </ListView>

    </android.support.constraint.ConstraintLayout>

我的 cardView xml 文件

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    android:id="@+id/expressionCardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:gravity="center"
    app:cardPreventCornerOverlap="false"
    app:cardCornerRadius="8dp"
    android:padding="-4dp"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    android:layout_marginRight="10dp"
    android:layout_marginLeft="10dp"

    xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:weightSum="2"
        android:orientation="vertical"
        android:gravity="center"
        android:padding="4dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/colorPrimaryDark">
            <TextView
                android:id="@+id/expressionFrancais"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/colorAccent"
                android:text="Najb Mohammed"
                android:textSize="24dp"
                android:textAlignment="center"
                />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/backgroundColor">
            <TextView
                android:id="@+id/expressionAnglais"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Najb mohammed"
                android:textSize="24dp"
                android:textAlignment="center"/>
        </LinearLayout>

    </LinearLayout>

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

适配器java文件:

  public class ExpressionAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<Expression> expressions;

    public ExpressionAdapter(Context context, ArrayList<Expression> expressions) {
        this.context = context;
        this.expressions = expressions;
    }

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

    @Override
    public Object getItem(int position) {
        return expressions.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null){
            convertView = View.inflate(context, R.layout.expressions_card_view,null);
        }

        TextView expressionFrancais = convertView.findViewById(R.id.expressionFrancais);
        TextView expressionAnglais = convertView.findViewById(R.id.expressionAnglais);
        Expression expression = expressions.get(position);

        expressionFrancais.setText(expression.getExpressionFrancais());
        expressionAnglais.setText(expression.getExpressionAnglais());


        return convertView;
    }
}

填充表达式列表:

  public class ListExpression {

    public static ArrayList<Expression> getExpressionList(){
        ArrayList<Expression> expressionList = new ArrayList<>();
        InfoGenBibliotheque infoGenBibliotheque = new InfoGenBibliotheque();
        for(int i = 0;i < infoGenBibliotheque.getSalutaionsAnglais().length;i++){
            expressionList.add(new Expression(i,infoGenBibliotheque.getSalutationFrancais(i),infoGenBibliotheque.getSalutationAnglais(i)));
        }
        return expressionList;
    }
}

表达式类

    public class Expression {

    private int idExpression;
    private String expressionFrancais;
    private String expressionAnglais;

    public Expression(int idExpression, String expressionFrancais, String expressionAnglais) {
        this.idExpression = idExpression;
        this.expressionFrancais = expressionFrancais;
        this.expressionAnglais = expressionAnglais;
    }

    public int getIdExpression() {

        return idExpression;
    }

    public void setIdExpression(int idExpression) {
        this.idExpression = idExpression;
    }

    public String getExpressionFrancais() {
        return expressionFrancais;
    }

    public void setExpressionFrancais(String expressionFrancais) {
        this.expressionFrancais = expressionFrancais;
    }

    public String getExpressionAnglais() {
        return expressionAnglais;
    }

    public void setExpressionAnglais(String expressionAnglais) {
        this.expressionAnglais = expressionAnglais;
    }
}

【问题讨论】:

  • 通常使用RecyclerView 实现一个列表。不幸的是,您的问题有点笼统,因为我必须编写 RecyclerView 教程才能为您提供答案。但也许这个guide 会有所帮助。
  • 如果您开始实施您的解决方案并在此过程中遇到困难,请随时根据您目前所做的以及您需要帮助的地方来编辑您的问题,那么我将尝试在下一个方面为您提供帮助步骤。
  • @0X0nosugar 谢谢你的关心,我已经设法使用一个简单的列表视图,但问题是当我执行应用程序时边距没有显示
  • 我可以建议跳过 CardView 周围的布局,只使用 CardView 本身作为列表行的根 ViewGroup 吗? (它确实从 FrameLayout 扩展,所以我认为这应该是可能的)
  • @0X0nosugar 你能编辑我的代码吗?我已经为您提供了所有文件

标签: android android-studio listview android-cardview


【解决方案1】:

仅使用CardView 作为根元素会使边距消失:

CardView 放在FrameLayout 中会给您留出边距:

(但我个人认为水平分隔线不适合卡片。如果您希望它们不可见,请查看this post 或使用RecyclerView 而不是ListView。 )

第二种方法的xml:

<FrameLayout
    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

    android:id="@+id/expressionCardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    app:cardCornerRadius="8dp"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    android:layout_marginRight="10dp"
    android:layout_marginLeft="10dp">
    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:weightSum="2"
        android:orientation="vertical"
        android:gravity="center"
        android:padding="4dp">
       <!-- skipped inner Views here -->

    </LinearLayout>

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

【讨论】:

  • 非常感谢它对我有用!我会检查 RecyclerView 教程
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多