以编程方式提供相等的列空间

时间:2017-05-23 05:35:22

标签: android android-recyclerview



 //Java code
 
 public class PassengerHomeActivity extends AppCompatActivity implements OnMapReadyCallback {

RecyclerView my_recycler_view;
    ArrayList<carmodel> array_list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

CarCategory();
        array_list = new ArrayList<>();
        my_recycler_view.setHasFixedSize(true);
        adapter = new CarAllAdapter(PassengerHomeActivity.this, array_list);
        my_recycler_view.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        my_recycler_view.setAdapter(adapter);
}

private void CarCategory() {
        if (array_list != null) array_list.clear();
        StringRequest stringRequest = new StringRequest(Request.Method.GET, URLUtils.APP_BASE_URL + "carcategorylistApi", new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {


                try {
                    JSONObject jObj = new JSONObject(response);
                    Log.w("CarCategory_customer", "<><><>" + response);

                    if (jObj.getString("ack").equals("1")) {
                        JSONArray jarr = jObj.getJSONArray("cardetails");
                        for (int i = 0; i < jarr.length(); i++) {
                            JSONObject jsonObject = jarr.getJSONObject(i);
                            String id = jsonObject.getString("id");
                            String name = jsonObject.getString("name");
                            String image = jsonObject.getString("image");
                            String hover_image = jsonObject.getString("hover_image");

                            carmodel objmodel = new carmodel();
                            objmodel.setId(id);
                            objmodel.setName(name);
                            objmodel.setImage(image);
                            objmodel.setHover_image(hover_image);

                            array_list.add(objmodel);


                        }
                        adapter.notifyDataSetChanged();

                    } else if (jObj.getString("ack").equals("2")) {
                        Toast.makeText(PassengerHomeActivity.this, "" + jObj.getString("msg"), Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(PassengerHomeActivity.this, "" + jObj.getString("msg"), Toast.LENGTH_SHORT).show();
                    }

                } catch (JSONException e) {
                    // JSON error
                    System.out.println("mono_JSONError " + e);
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (error instanceof TimeoutError) {
                    Toast.makeText(PassengerHomeActivity.this, getString(R.string.tooslow), Toast.LENGTH_LONG).show();
                } else if (error instanceof NoConnectionError) {
                    Toast.makeText(PassengerHomeActivity.this, getString(R.string.nointernet), Toast.LENGTH_LONG).show();
                } else if (error instanceof AuthFailureError) {
                    System.out.println("mono_AuthFailureError " + error);
                } else if (error instanceof ServerError) {
                    System.out.println("mono_ServerError " + error);
                } else if (error instanceof NetworkError) {
                    System.out.println("mono_NetworkError " + error);
                } else if (error instanceof ParseError) {
                    System.out.println("mono_ParseError " + error);
                }
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                return params;
            }

        };

        RequestQueue requestQueue = Volley.newRequestQueue(PassengerHomeActivity.this);
        requestQueue.add(stringRequest);
        stringRequest.setRetryPolicy(new DefaultRetryPolicy(
                10000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));


    }
}
&#13;
&#13;
&#13;

我正在显示带有图片和文字的水平滚动回收视图,如屏幕截图所示。

我想要的是对于recyclerview的每个行项目具有相等的宽度,如果行数小于4并且如果大于4则它将适合屏幕,那么它将具有正常滚动。我可以计算行项目的数量。

我只想知道如果计数为4或更小,如何以相等的宽度拉伸行。

此图片来自当前代码。

current image

这就是我想要的。

desired image

&#13;
&#13;
//Main xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/top_parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".PassengerHomeActivity">

    <android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/projectColor">


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

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <android.support.design.widget.CoordinatorLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_above="@+id/ll_buttomAction">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <RelativeLayout
                        android:id="@+id/rl_mapcontainer"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                        <fragment
                            android:id="@+id/maps_home"
                            class="com.google.android.gms.maps.SupportMapFragment"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent" />

                        <ImageView
                            android:id="@+id/pinLocation"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerInParent="true"
                            android:src="@drawable/pin_pickup" />


                    </RelativeLayout>

                    <LinearLayout
                        android:id="@+id/ll_pick"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp"
                        android:layout_marginTop="10dp"
                        android:background="@drawable/pickup_text"
                        android:orientation="vertical"
                        android:padding="5dp">

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="horizontal">

                            <com.example.nits_34.zeomobile.CustomView.IonicFont
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="@string/ion_circle"
                                android:textColor="#19F029" />

                            <com.example.nits_34.zeomobile.CustomView.ExoticFontRegular
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginLeft="10dp"
                                android:text="Pickup from" />
                        </LinearLayout>

                        <TextView
                            android:id="@+id/txt_pickup"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="5dp"
                            android:ellipsize="end"
                            android:maxLines="1"
                            android:minLines="1"
                            android:textColor="#2B2B2B" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/ll_pick"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp"
                        android:layout_marginTop="5dp"
                        android:background="@drawable/pickup_text"
                        android:orientation="vertical"
                        android:padding="5dp">

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="horizontal">

                            <com.example.nits_34.zeomobile.CustomView.IonicFont
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="@string/ion_circle"
                                android:textColor="#ff4d4d" />

                            <com.example.nits_34.zeomobile.CustomView.ExoticFontRegular
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginLeft="10dp"
                                android:text="Drop at" />
                        </LinearLayout>

                        <TextView
                            android:id="@+id/txt_drop"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="5dp"
                            android:ellipsize="end"
                            android:maxLines="1"
                            android:minLines="1"
                            android:textColor="#2B2B2B" />
                    </LinearLayout>

                </RelativeLayout>


                <android.support.design.widget.FloatingActionButton
                    android:id="@+id/fab"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="16dp"
                    android:theme="@style/MyFloatingButton"
                    app:fabSize="mini"
                    app:layout_anchor="@+id/rl_mapcontainer"
                    app:layout_anchorGravity="bottom|right|end"
                    app:srcCompat="@drawable/my_locationblack" />

            </android.support.design.widget.CoordinatorLayout>

            <LinearLayout
                android:id="@+id/ll_buttomAction"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:orientation="vertical">

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/my_recycler_view"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:scrollbars="none" />

                <LinearLayout
                    android:id="@+id/ll_rideoption"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <com.example.nits_34.zeomobile.CustomView.ExoticFontRegular
                        android:id="@+id/txt_RideNow"
                        android:layout_width="match_parent"
                        android:layout_height="45dp"
                        android:layout_weight="1"
                        android:background="#1A222D"
                        android:gravity="center"
                        android:text="RIDE NOW"
                        android:textColor="@color/projectColor" />

                    <View
                        android:layout_width="1dp"
                        android:layout_height="45dp"
                        android:background="#58789E" />

                    <com.example.nits_34.zeomobile.CustomView.ExoticFontRegular
                        android:id="@+id/RideLeter_Txt"
                        android:layout_width="match_parent"
                        android:layout_height="45dp"
                        android:layout_weight="1"
                        android:background="#1A222D"
                        android:gravity="center"
                        android:text="RIDE LATER"
                        android:textColor="@color/projectColor" />

                </LinearLayout>
            </LinearLayout>


        </RelativeLayout>

        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:headerLayout="@layout/navigation_header"
            app:itemIconTint="#ff1b6bae"
            app:itemTextColor="#333" />


    </android.support.v4.widget.DrawerLayout>


</RelativeLayout>
&#13;
//Row layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#324457"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/ll_singleCar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="5dp">

        <ImageView
            android:id="@+id/itemImage"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center_horizontal"
            android:scaleType="fitCenter"
            android:src="@drawable/no_image"  />


        <TextView
            android:id="@+id/tvTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/itemImage"
            android:gravity="center"
            android:padding="2dp"
            android:text="Sample title"
            android:textColor="@color/projectColor"
            android:textSize="12sp" />


    </LinearLayout>

</LinearLayout>
&#13;
{
  "ack": 1,
  "cardetails": [
    {
      "id": "3",
      "name": "Luxury Cards",
      "image": "http:\/\/111.333.88.222\/team6\/zoemobile\/car_images\/suv.png",
      "hover_image": "http:\/\/111.333.88.222\/team6\/zoemobile\/car_images\/Luxury_hover.png"
    },
    {
      "id": "4",
      "name": "SUV's",
      "image": "http:\/\/111.333.88.222\/team6\/zoemobile\/car_images\/limousine.png",
      "hover_image": "http:\/\/111.333.88.222\/team6\/zoemobile\/car_images\/SUV_hover.png"
    },
    {
      "id": "6",
      "name": "Regular Cars ",
      "image": "http:\/\/111.333.88.222\/team6\/zoemobile\/car_images\/taxi.png",
      "hover_image": "http:\/\/111.333.88.222\/team6\/zoemobile\/car_images\/reguler_hover.png"
    }
  ]
}
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

activity_main.xml:将layout_width =“match_parent”更改为“wrap_content”

<android.support.v7.widget.RecyclerView
                    android:id="@+id/my_recycler_view"
                    android:layout_width="wrap_content" // change match_parent to wrap_content
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:scrollbars="none" />

row.xml:请查看以下来源。我在两个地方将layout_width =“match_parent”更改为“wrap_content”

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#324457"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/ll_singleCar"
        android:layout_width="wrap_content" // change match_parent to wrap_content 
        android:layout_height="wrap_content" 
        android:orientation="vertical"
        android:padding="5dp">

        <ImageView
            android:id="@+id/itemImage"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center_horizontal"
            android:scaleType="fitCenter"
            android:src="@android:drawable/btn_default"  />


        <TextView
            android:id="@+id/tvTitle"
            android:layout_width="wrap_content" // change match_parent to wrap_content 
            android:layout_height="wrap_content" 
            android:layout_below="@id/itemImage"
            android:gravity="center"
            android:padding="2dp"
            android:text="Sample title"
            android:textColor="#ffffff"
            android:textSize="12sp" />


    </LinearLayout>

</LinearLayout>

如果您希望在适配器少于4个项目时每个行项目具有相同的宽度大小,则可以在适配器文件中的onBindViewHolder方法中以编程方式更改 ll_singleCar 的宽度。请检查此链接 https://stackoverflow.com/a/11344000/7973751

答案 1 :(得分:0)

我相信您可以使用ItemDecoration类来解决问题。在方法getItemOffsets中,您可以计算每个项目的大小,并在需要基本屏幕大小时向左或向右添加空间,这应该很容易通过RecyclerView.getWidth或测量宽度获得。

确保您的RecyclerView宽度与父级匹配。