I am implementing a RecyclerView in my application and it is working. But i am facing a problem with the list item alignment in Recyclerview.
我正在我的应用程序中实现RecyclerView,它正在工作。但我在Recyclerview中遇到列表项对齐问题。
actually my list item have a image view and a text view. i need some space between the each list item. like this
实际上我的列表项有一个图像视图和一个文本视图。我需要在每个列表项之间留出一些空间。像这样
With this Relative layout i am able to get my expected result. But for spacing between the items i used the RecyclerView.ItemDecoration
通过这种相对布局,我能够得到我期望的结果。但是对于我使用RecyclerView.ItemDecoration的项目之间的间距
Here is my list_view.xml
这是我的list_view.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#65FF0000"
android:padding="10dip" >
<ImageView
android:id="@+id/item_icon"
android:layout_width="70dip"
android:layout_height="70dip"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/item_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dip"
android:layout_toRightOf="@+id/item_icon"
android:gravity="left"
android:textSize="24sp" />
</RelativeLayout>
insted of using the RecyclerView.ItemDecoration i tried the in the below way but it is not worked.Here is my list_item.xml code
我使用了RecyclerView.ItemDecoration但是我尝试了以下方式,但它没有用。这是我的list_item.xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#90FF0000"
android:orientation="horizontal"
android:weightSum="1" >
<ImageView
android:id="@+id/item_icon"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/item_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.6"
android:text="TextView"
android:textColor="@android:color/darker_gray"
android:textSize="24dp" />
</LinearLayout>
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="5dp"
android:text=""
android:textColor="#90FFFFFF" />
</LinearLayout>
But i am getting the view like below
但我得到的观点如下
Where exactly i am making the mistake.. Thanks in advance
我究竟在哪里弄错了..先谢谢
3 个解决方案
#1
Well finally, I got the solution for my question.
最后,我得到了我的问题的解决方案。
In my RecyclerView adapter previously i used the below type of approach to create the list items
在我之前的RecyclerView适配器中,我使用以下类型的方法来创建列表项
public MyAdapter.ViewHolder onCreateViewHolder( ViewGroup parent,
int viewType )
{
// create a new view
itemLayoutView = LayoutInflater.from( parent.getContext() ).inflate( R.layout.list_row_serch,
null );
// create ViewHolder
ViewHolder viewHolder = new ViewHolder( itemLayoutView );
return viewHolder;
}
After changing it as like below it is working fine. even if it a Releative/Linear layout
更改后如下所示它工作正常。即使它是一个Releative / Linear布局
public MyAdapter.ViewHolder onCreateViewHolder( ViewGroup parent,
int viewType )
{
itemLayoutView = LayoutInflater.from( parent.getContext() ).inflate( R.layout.list_row_serch,
parent,
false );
// create ViewHolder
ViewHolder viewHolder = new ViewHolder( itemLayoutView );
return viewHolder;
}
#2
So as I understand from your question you want your RecyclerView
's items to look like this:
因此,我从您的问题中了解到,您希望RecyclerView的项目如下所示:
Here is a simple layout which will create this look (using RelativeLayout
):
这是一个简单的布局,它将创建这个外观(使用RelativeLayout):
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dip">
<ImageView
android:id="@+id/user_avatar"
android:layout_width="70dip"
android:layout_height="70dip"
android:layout_centerVertical="true"/>
<TextView
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dip"
android:layout_toRightOf="@+id/user_avatar"
android:gravity="left"/>
</RelativeLayout>
and here is the version using LinearLayout
:
这是使用LinearLayout的版本:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="10dip">
<ImageView
android:id="@+id/user_avatar"
android:layout_width="70dip"
android:layout_height="70dip"
android:layout_gravity="center_vertical"/>
<TextView
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_gravity="center_vertical"
android:gravity="left"/>
</LinearLayout>
If you want space between two RecyclerView
items no need to add another TextView
just include padding
to your main container in list_item.xml
如果您想在两个RecyclerView项目之间留出空间,则无需添加另一个TextView,只需在list_item.xml中包含填充到主容器中
#3
You will need to make the list item fill the whole width of its parent.
您需要使列表项填充其父级的整个宽度。
As below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#90FF0000"
android:orientation="horizontal">
<ImageView
android:layout_weight="1"
android:id="@+id/item_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:layout_weight="1"
android:id="@+id/item_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="TextView"
android:textColor="@android:color/darker_gray"
android:textSize="24dp" />
<TextView
android:layout_weight="1"
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="5dp"
android:text=""
android:textColor="#90FFFFFF" />
</LinearLayout>
</LinearLayout>
#1
Well finally, I got the solution for my question.
最后,我得到了我的问题的解决方案。
In my RecyclerView adapter previously i used the below type of approach to create the list items
在我之前的RecyclerView适配器中,我使用以下类型的方法来创建列表项
public MyAdapter.ViewHolder onCreateViewHolder( ViewGroup parent,
int viewType )
{
// create a new view
itemLayoutView = LayoutInflater.from( parent.getContext() ).inflate( R.layout.list_row_serch,
null );
// create ViewHolder
ViewHolder viewHolder = new ViewHolder( itemLayoutView );
return viewHolder;
}
After changing it as like below it is working fine. even if it a Releative/Linear layout
更改后如下所示它工作正常。即使它是一个Releative / Linear布局
public MyAdapter.ViewHolder onCreateViewHolder( ViewGroup parent,
int viewType )
{
itemLayoutView = LayoutInflater.from( parent.getContext() ).inflate( R.layout.list_row_serch,
parent,
false );
// create ViewHolder
ViewHolder viewHolder = new ViewHolder( itemLayoutView );
return viewHolder;
}
#2
So as I understand from your question you want your RecyclerView
's items to look like this:
因此,我从您的问题中了解到,您希望RecyclerView的项目如下所示:
Here is a simple layout which will create this look (using RelativeLayout
):
这是一个简单的布局,它将创建这个外观(使用RelativeLayout):
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dip">
<ImageView
android:id="@+id/user_avatar"
android:layout_width="70dip"
android:layout_height="70dip"
android:layout_centerVertical="true"/>
<TextView
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dip"
android:layout_toRightOf="@+id/user_avatar"
android:gravity="left"/>
</RelativeLayout>
and here is the version using LinearLayout
:
这是使用LinearLayout的版本:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="10dip">
<ImageView
android:id="@+id/user_avatar"
android:layout_width="70dip"
android:layout_height="70dip"
android:layout_gravity="center_vertical"/>
<TextView
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_gravity="center_vertical"
android:gravity="left"/>
</LinearLayout>
If you want space between two RecyclerView
items no need to add another TextView
just include padding
to your main container in list_item.xml
如果您想在两个RecyclerView项目之间留出空间,则无需添加另一个TextView,只需在list_item.xml中包含填充到主容器中
#3
You will need to make the list item fill the whole width of its parent.
您需要使列表项填充其父级的整个宽度。
As below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#90FF0000"
android:orientation="horizontal">
<ImageView
android:layout_weight="1"
android:id="@+id/item_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:layout_weight="1"
android:id="@+id/item_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="TextView"
android:textColor="@android:color/darker_gray"
android:textSize="24dp" />
<TextView
android:layout_weight="1"
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="5dp"
android:text=""
android:textColor="#90FFFFFF" />
</LinearLayout>
</LinearLayout>