在网格视图中选择多个卡片视图

时间:2019-02-09 07:19:06

标签: android android-cardview android-gridview

我正在尝试选择位于网格视图中的多个卡片视图。 任何帮助,将不胜感激。并预先感谢

initial look

我希望卡片高亮显示并计数

适配器类

public class sub_productadapter extends ArrayAdapter<Sublistitems> {


    private final Context context;
    private final ArrayList<Sublistitems> itemsArrayList;
    private ArrayList<Sublistitems> arraylist;
    ImageHolder holder = new ImageHolder();



public sub_productadapter(Context context, ArrayList<Sublistitems> itemlist) {
        super(context, R.layout.sub_list_item,itemlist);

        this.context = context;
        this.itemsArrayList=itemlist;
        this.arraylist=new ArrayList<Sublistitems>();
        this.arraylist.addAll(itemsArrayList);
    }

    public View getView(final int pos, View convertView, ViewGroup parent) {


        LayoutInflater inflater= (LayoutInflater) context.getSystemService
                (Context.LAYOUT_INFLATER_SERVICE);

        View row=inflater.inflate(R.layout.sub_list_item,parent,false);

        holder.sub_name= row.findViewById(R.id.sub_name);
        holder.sub_name.setText(itemsArrayList.get(pos).getName().trim());

        holder.price= row.findViewById(R.id.price);
        holder.price.setText("PKR :" + itemsArrayList.get(pos).getPrice().trim());


        holder.sub_image= row.findViewById(R.id.sub_image);
        if(itemsArrayList.get(pos).getimage() != null && itemsArrayList.get(pos).getimage().length()>0)
        {
            Picasso.get().load(itemsArrayList.get(pos).getimage()).placeholder(R.drawable.ethlon_supplies).into(holder.sub_image);
        }else {
            Toast.makeText(context, "Empty Image URL", Toast.LENGTH_LONG).show();
            Picasso.get().load(R.drawable.ethlon_supplies).into(holder.sub_image);
        }
        holder.sub_cardview = row.findViewById(R.id.sub_cardview);

        return row;
    }

    private class ImageHolder {
        TextView sub_name,price;
        ImageView sub_image;

        CardView sub_cardview;
    }
}

模型类

public class Sublistitems {

String id,img,name,price;

public Sublistitems(String id, String img, String name, String price) {
    this.id = id;
    this.img = img;
    this.name = name;
    this.price = price;
}

public void setID(String ID) {
    id = ID;
}
public String getID() { return this.id; }

public void setName(String content) {
    name = content;
}
public String getName() {
    return this.name;
}

public void setimage(String color) {
    img = color;
}
public String getimage() {
    return this.img;
}

public String getPrice() { return price;}

public void setPrice(String price) { this.price = price; }
}

我要将数据传递给适配器类的主类     gridView = findViewById(R.id.sub_grid);     gridView.setAdapter(SPD);

cardview的xml

<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/sub_cardview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:cardUseCompatPadding="true"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp">


<!--1ST Grid-->
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="8dp" >
    <!--Image Layout-->
    <LinearLayout
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center">
        <ImageView
            android:layout_margin="5dp"
            android:id="@+id/sub_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ethlon_supplies"/>
    </LinearLayout>

    <!--Text layout-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        >
        <TextView
            android:id="@+id/sub_name"
            android:gravity="center"
            android:layout_margin="3dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            android:textColor="@color/Black"
            android:fontFamily="@font/futura_book_font"
            android:textSize="10sp"
            android:textStyle="bold"
            android:maxLines="1"
            />
    </LinearLayout>

    <!--price-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">
        <TextView
            android:id="@+id/price"
            android:gravity="center"
            android:layout_margin="4dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            android:textColor="@color/Black"
            android:fontFamily="@font/futura_book_font"
            android:textSize="10sp"
            android:textStyle="bold"
            android:maxLines="1" />
    </LinearLayout>

</LinearLayout>

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

网格视图xml

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10sp">

        <GridView
            android:layout_margin="10dp"
            android:id="@+id/sub_grid"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:horizontalSpacing="10dp"
            android:verticalSpacing="10dp"
            android:numColumns="3"/>
    </LinearLayout>

0 个答案:

没有答案