在recyclerview列表中删除左右填充

时间:2016-10-14 06:25:52

标签: android

我一直在recyclerview列表中加载图片。在图像的左侧和右侧有一些填充。

我需要删除那些填充。但是我不确定如何在recyclerview项的左侧和右侧添加填充。

下面我发布了与此相关的屏幕截图和代码:

enter image description here

如上面的屏幕截图所示,你可以看到小的左右填充。我需要删除它。

编辑:

活动代码:

recyclerView = (RecyclerView) findViewById(R.id.rv_list_tab_home_recycler);

mLayoutManager = new LinearLayoutManager(this);

recyclerView.setLayoutManager(mLayoutManager);
//  recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(homePostitemsAdapter);
recyclerView.setNestedScrollingEnabled(false);

activity_layout.xml:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.NestedScrollView 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="match_parent"
    android:layout_gravity="fill_vertical"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:id="@+id/tab_home_activity"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_list_tab_home_recycler"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clipToPadding="false"
            android:scrollbars="vertical"
            android:visibility="visible" />

    </LinearLayout>

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

adapter_layout:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp" >

    <LinearLayout
        android:id="@+id/rl_vertical_list"
        android:visibility="visible"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:id="@+id/post_items_layout_middle_home"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp" >

            <LinearLayout
                 android:id="@+id/ll_posted_image_parent"
                 android:layout_width="match_parent"
                 android:orientation="vertical"
                 android:layout_below="@+id/tv_user_posted_msg_post_items_home"
                 android:layout_height="wrap_content">

             </LinearLayout>
         </RelativeLayout>
     </LinearLayout>
 </RelativeLayout>

适配器代码:

我正在以编程方式将imageview添加到linearlayout。

DisplayMetrics dm = new DisplayMetrics();


((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(d‌​m); 

int deviceWidth = dm.widthPixels; 

int deviceHeight = deviceWidth * imageHeight / imageWidth;

holder.ivPostedImageNew = new ImageView(context);
params = new LinearLayout.LayoutParams(deviceWidth, deviceHeight);

holder.ivPostedImageNew.setLayoutParams(params);
holder.ivPostedImageNew.setPadding(0, 0, 0, 0);
holder.ll_posted_image_parent.addView(holder.ivPostedImageNew);

我不确定左右填充的位置。All views inside the adapter having left and right padding space。任何人都可以帮助我。感谢你。

3 个答案:

答案 0 :(得分:2)

检查布局是否在tabhost中提供任何填充。我认为您的家庭活动在tabhost内。这就是为什么您无法以全宽设置图像

答案 1 :(得分:1)

好的,我已经测试了您的代码。首先,我不知道什么是YOur变量deviceWidth但是当你做这样的事情时:

holder.ivPostedImageNew = new ImageView(context);
params = new LinearLayout.LayoutParams(deviceWidth, deviceHeight);
holder.ivPostedImageNew.setLayoutParams(params);
holder.ivPostedImageNew.setPadding(0, 0, 0, 0);

它类似于xml中的代码:

<ImageView
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:padding="0dp"/>

给你这个:

enter image description here

请在您的代码中尝试此操作:

holder.ivPostedImageNew = new ImageView(context);
params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
ivPostedImageNew.setLayoutParams(params);
ivPostedImageNew.setAdjustViewBounds(true);

结果就是:

enter image description here

但是你必须知道,如果图像的容器高度小于图像的原始高度,那么你还需要处理它。好的解决方案是使用

ivPostedImageNew.setScaleType(ImageView.ScaleType.CENTER_CROP);

将图像居中并剪裁以适应。

答案 2 :(得分:0)

您要加载到图像视图中的图像具有固定大小。我们需要做的是我们必须拉伸此图像,以便您的图像适合图像视图容器而不需要任何填充。它与我们在windows中设置背景墙纸相同,即我们使用Strech,Fit to Screen,Tile,Center等来根据我们的要求设置图像。

尝试在您提供的代码后使用此行

holder.ivPostedImageNew.setScaleType(ScaleType.FIT_XY);

我希望这会奏效......