在水平滚动中使用固定项目计数创建回收器视图

发布于 2025-02-09 19:25:54 字数 7456 浏览 0 评论 0原文

请帮助我,

我正在尝试创建一种特定类型的回收器视图,该视图在各种手机中总是可以包含3个项目。该移动设备是否具有4英寸的显示屏或6.5英寸的显示屏。

请告诉我是否可能。

我的XML代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="100dp"
    android:id="@+id/recomended"
    android:layout_margin="6dp"
    android:background="@drawable/graycurverdbtn"
    android:layout_height="170dp">

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="85dp"

        android:scaleType="fitCenter"
        android:src="@drawable/mountain">

    </ImageView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_margin="10dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="15dp"
        android:gravity="center"


        >

        <TextView

            android:id="@+id/pinkbtn"

            android:layout_width="match_parent"

            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:background="@drawable/pinkcurvedbtn"
            android:gravity="center"
        android:padding="3dp"
            android:text="Recomended"
            android:textColor="@color/white"
            android:textSize="9dp">

        </TextView>
    </LinearLayout>
    <LinearLayout

        android:layout_below="@id/image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"

        android:layout_margin="8dp"
     >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                android:id="@+id/title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textStyle="normal"
                android:layout_margin="6dp"
                android:layout_gravity="center"
                android:textSize="10sp"
                android:textColor="@color/black"
                android:text="Pest control">

            </TextView>
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@color/grey"></View>
            <TextView
                android:layout_margin="8dp"
                android:id="@+id/shopname"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="10dp"
                android:textStyle="bold"
                android:textColor="@color/black"
                android:text="K02BgbhdAf">

            </TextView>
            <TextView
                android:visibility="gone"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableLeft="@drawable/graylocation"
                android:text="Airport Road"
                android:id="@+id/address"
                android:textSize="18sp"
                android:textStyle="normal">

            </TextView>
        </LinearLayout>


    </LinearLayout>

</RelativeLayout>

这是我的适配器代码

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

    private List<HomeResponse.Recommend> ourrecomendedlist = new ArrayList<>();
    private Context context;
    public static int recomendedid=0;
    public OurrecomendedAdapter(List<HomeResponse.Recommend> list, Context context) {
        this.ourrecomendedlist = list;
        this.context = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recomemdedservices_item, parent, false);
//        itemView.setLayoutParams(new ViewGroup.LayoutParams((int) (parent.getWidth() * 0.3),ViewGroup.LayoutParams.MATCH_PARENT));
        return new MyViewHolder(itemView);

    }

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

        holder.title.setText(ourrecomendedlist.get(position).getCategory().getSlug());
        holder.shopname.setText(ourrecomendedlist.get(position).getCategory().getName());

        Picasso.get().load(ourrecomendedlist.get(position).getImage()).into(holder.image, new Callback() {
            @Override
           public void onSuccess() {

           }

            @Override
            public void onError(Exception e) {

            }
        });
        holder.recomended.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(ourrecomendedlist.get(position).getCategory().getName().toString().equalsIgnoreCase("salon for women") || ourrecomendedlist.get(position).getCategory().getName().toString().equalsIgnoreCase("salon for men")){

                    AppCompatActivity activity = (AppCompatActivity) context;
                    WomensalonlistFragment subc = new WomensalonlistFragment();
                    Bundle args = new Bundle();
                    args.putString("id", ourrecomendedlist.get(position).getProductcategoryId().toString());
                    args.putString("title", ourrecomendedlist.get(position).getName());
                    subc.setArguments(args);

                    activity.getSupportFragmentManager().beginTransaction().addToBackStack(null).
                            replace(R.id.nav_host_fragment, subc).commit();
                }
                else
                {

                    typeListid= ourrecomendedlist.get(position).getId().toString();
                    Intent intent= new Intent(context, ProductdetailsActivity.class);
                    intent.putExtra("productId",typeListid);
                    context.startActivity(intent);


                    HomeFragment.vendormainid=ourrecomendedlist.get(position).getId().toString();

                }


            }
        });


    }

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

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView name,title,shopname,address,pinkbtn;
        Button addtocart;
        ImageView image;
        RelativeLayout recomended;

        MyViewHolder(View itemView) {
            super(itemView);

            addtocart=itemView.findViewById(R.id.addtocart);

            image=itemView.findViewById(R.id.image);
            title=itemView.findViewById(R.id.title);
            shopname=itemView.findViewById(R.id.shopname);
            address=itemView.findViewById(R.id.address);
            recomended=itemView.findViewById(R.id.recomended);
            name=itemView.findViewById(R.id.name);
            pinkbtn=itemView.findViewById(R.id.pinkbtn);



        }

    }
}

,我需要在每个方面比例移动设备中将输出显示为3个项目。 但是我无法实现这一目标。如果我在大屏幕移动设备上打开,那么它将显示3.5个项目,并且有时在小型设备中显示2.75个项目。

请帮助我如何实现精确的输出。

Please help Me,

I am trying to create a specific type of recycler view which can have always 3 items in all kind of mobile phone. Whether that mobile is having 4 inch display or 6.5 inch display.

Please tell me if that is possible or not.

my code of xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="100dp"
    android:id="@+id/recomended"
    android:layout_margin="6dp"
    android:background="@drawable/graycurverdbtn"
    android:layout_height="170dp">

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="85dp"

        android:scaleType="fitCenter"
        android:src="@drawable/mountain">

    </ImageView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_margin="10dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="15dp"
        android:gravity="center"


        >

        <TextView

            android:id="@+id/pinkbtn"

            android:layout_width="match_parent"

            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:background="@drawable/pinkcurvedbtn"
            android:gravity="center"
        android:padding="3dp"
            android:text="Recomended"
            android:textColor="@color/white"
            android:textSize="9dp">

        </TextView>
    </LinearLayout>
    <LinearLayout

        android:layout_below="@id/image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"

        android:layout_margin="8dp"
     >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                android:id="@+id/title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textStyle="normal"
                android:layout_margin="6dp"
                android:layout_gravity="center"
                android:textSize="10sp"
                android:textColor="@color/black"
                android:text="Pest control">

            </TextView>
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@color/grey"></View>
            <TextView
                android:layout_margin="8dp"
                android:id="@+id/shopname"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="10dp"
                android:textStyle="bold"
                android:textColor="@color/black"
                android:text="K02BgbhdAf">

            </TextView>
            <TextView
                android:visibility="gone"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableLeft="@drawable/graylocation"
                android:text="Airport Road"
                android:id="@+id/address"
                android:textSize="18sp"
                android:textStyle="normal">

            </TextView>
        </LinearLayout>


    </LinearLayout>

</RelativeLayout>

This is my Adapter code

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

    private List<HomeResponse.Recommend> ourrecomendedlist = new ArrayList<>();
    private Context context;
    public static int recomendedid=0;
    public OurrecomendedAdapter(List<HomeResponse.Recommend> list, Context context) {
        this.ourrecomendedlist = list;
        this.context = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recomemdedservices_item, parent, false);
//        itemView.setLayoutParams(new ViewGroup.LayoutParams((int) (parent.getWidth() * 0.3),ViewGroup.LayoutParams.MATCH_PARENT));
        return new MyViewHolder(itemView);

    }

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

        holder.title.setText(ourrecomendedlist.get(position).getCategory().getSlug());
        holder.shopname.setText(ourrecomendedlist.get(position).getCategory().getName());

        Picasso.get().load(ourrecomendedlist.get(position).getImage()).into(holder.image, new Callback() {
            @Override
           public void onSuccess() {

           }

            @Override
            public void onError(Exception e) {

            }
        });
        holder.recomended.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(ourrecomendedlist.get(position).getCategory().getName().toString().equalsIgnoreCase("salon for women") || ourrecomendedlist.get(position).getCategory().getName().toString().equalsIgnoreCase("salon for men")){

                    AppCompatActivity activity = (AppCompatActivity) context;
                    WomensalonlistFragment subc = new WomensalonlistFragment();
                    Bundle args = new Bundle();
                    args.putString("id", ourrecomendedlist.get(position).getProductcategoryId().toString());
                    args.putString("title", ourrecomendedlist.get(position).getName());
                    subc.setArguments(args);

                    activity.getSupportFragmentManager().beginTransaction().addToBackStack(null).
                            replace(R.id.nav_host_fragment, subc).commit();
                }
                else
                {

                    typeListid= ourrecomendedlist.get(position).getId().toString();
                    Intent intent= new Intent(context, ProductdetailsActivity.class);
                    intent.putExtra("productId",typeListid);
                    context.startActivity(intent);


                    HomeFragment.vendormainid=ourrecomendedlist.get(position).getId().toString();

                }


            }
        });


    }

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

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView name,title,shopname,address,pinkbtn;
        Button addtocart;
        ImageView image;
        RelativeLayout recomended;

        MyViewHolder(View itemView) {
            super(itemView);

            addtocart=itemView.findViewById(R.id.addtocart);

            image=itemView.findViewById(R.id.image);
            title=itemView.findViewById(R.id.title);
            shopname=itemView.findViewById(R.id.shopname);
            address=itemView.findViewById(R.id.address);
            recomended=itemView.findViewById(R.id.recomended);
            name=itemView.findViewById(R.id.name);
            pinkbtn=itemView.findViewById(R.id.pinkbtn);



        }

    }
}

in this i need to display our output as 3 items in each aspect-ratio mobile.
but I'm unable to achieve that. If I'm opening in big screen mobile then it's showing 3.5 items and in small device some times it's showing 2.75 items.

Kindly help me in that how to achieve that exact output.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文