我基本上尝试创建一个带有水平滚动的线性布局管理器的回收器视图。当我使用具有垂直滚动的线性布局管理器时,回收器视图行的设计按照布局文件中提供的方式正确对齐,但当我使用水平滚动时,设计元素放置在完全奇怪的位置 这是我的回收者视图行的代码:
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:orientation="vertical"
card_view:cardCornerRadius="2dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="250dp">
<ImageView
android:id="@+id/shop_product_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView
android:id="@+id/favorite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="12dp"
android:padding="10dp"
android:src="@drawable/ic_favorite_selected" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="@color/black80"
android:padding="8dp"
android:text="1200 INR"
android:textColor="@color/white"
android:textSize="18sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/black80">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/shop_product_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingTop="4dp"
android:paddingBottom="2dp"
android:textColor="@color/white"
android:textSize="16sp" />
<TextView
android:id="@+id/shop_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingBottom="4dp"
android:text="The Indian Shop"
android:textColor="@color/white"
android:textSize="12sp" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="150 likes"
android:textColor="@color/white"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:paddingRight="8dp"/>
</RelativeLayout>
</RelativeLayout>
活动布局的布局文件是:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.paaltao.activity.MyShopActivity">
<include
android:id="@+id/app_bar"
layout="@layout/app_bar" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/product_view"
android:layout_below="@+id/app_bar"
>
<RelativeLayout
android:id="@+id/shop_cover_area"
android:layout_width="match_parent"
android:layout_height="150dp"
android:visibility="visible">
<ImageView
android:id="@+id/shop_cover_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/shop_cover_image"
android:scaleType="centerCrop"
android:src="@drawable/apple_small" />
</RelativeLayout>
<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:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:orientation="vertical"
android:visibility="gone"
card_view:cardCornerRadius="5dp"
card_view:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/shop_start_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="16dp"
android:text="@string/product_first_launch_text"
android:textColor="@color/black" />
</LinearLayout>
</android.support.v7.widget.CardView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/shop_cover_area"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/shop_products_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:padding="8dp"
/>
</RelativeLayout>
</RelativeLayout>
<com.paaltao.classes.AddFloatingActionButton
android:id="@+id/multiple_actions1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_gravity="end"
fab:fab_addButtonColorPressed="@color/white_pressed"
fab:fab_addButtonPlusIconColor="@color/white"
fab:fab_labelStyle="@style/menu_labels_style"
app:fab_addButtonColorNormal="@color/lightGreen"/>
</RelativeLayout>
活动代码如下:
package com.paaltao.activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.paaltao.Adapters.ShopProductAdapter;
import com.paaltao.R;
import com.paaltao.classes.AddFloatingActionButton;
import com.paaltao.classes.Product;
import java.util.ArrayList;
import java.util.List;
public class MyShopActivity extends ActionBarActivity {
private RecyclerView mRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_shop);
mRecyclerView = (RecyclerView) findViewById(R.id.shop_products_recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(),LinearLayoutManager.HORIZONTAL,false));
mRecyclerView.setAdapter(new ShopProductAdapter(getApplicationContext(), getData()));
Toolbar toolbar = (Toolbar) this.findViewById(R.id.app_bar);
toolbar.setTitleTextColor(Color.WHITE);
this.setSupportActionBar(toolbar);
this.setTitle("Shop");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
AddFloatingActionButton addProductButton = (AddFloatingActionButton)findViewById(R.id.multiple_actions1);
addProductButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Tag","Clicked");
Intent intent = new Intent(MyShopActivity.this, AddProductActivity.class);
startActivity(intent);
}
});
}
public static List<Product> getData(){
ArrayList data = new ArrayList();
int[] icons = {R.drawable.apple_small,R.drawable.ic_launcher,R.drawable.bag_icon,R.drawable.notify_icon,R.drawable.apple_small,
R.drawable.bag_icon,R.drawable.apple_small,R.drawable.notify_icon,R.drawable.apple_small,R.drawable.ic_launcher};
String[] product_name = {"Handmade","Photography","Electronics","Electronics","Electronics","Electronics","Electronics",
"Electronics","Electronics","Electronics"};
for(int i=0; i<icons.length && i< product_name.length;i++){
Product current = new Product();
current.setProduct_name(product_name[i]);
current.setProduct_id(icons[i]);
data.add(current);
}
return data;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_my_shop, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
当我在循环视图中使用垂直方向时,设计工作正常。仅当我将方向设置为LinearLayoutManager.HORIZONTAL时才会出现此问题。
请帮忙
答案 0 :(得分:0)
提出这样一个问题是非常愚蠢的:P
在包含Recycler视图行设计的布局中,宽度和高度不能设置为“匹配父级”。相反,这件事仅仅适用于“包装内容”。要启用水平滚动,必须为行指定固定的宽度和高度。