如何在回收站视图中为每个项目设置不同的高度

时间:2018-09-24 06:34:55

标签: android android-recyclerview

我想为回收站视图中的每个项目设置不同的高度。通常,每一行的高度都相同。但是我需要不同的高度,例如,如果前三行的高度为70,那么我需要其余的高度为0。我的代码如下。在此代码中,其设置不正确。请建议我编辑

public class MainActivity extends AppCompatActivity {

private RecyclerView rvListProfiles;
private ListProfileAdapter listProfileAdapter;
private String[] list = new String[]{"ABC","DEF","GHI","JKL","MNO"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    rvListProfiles = findViewById(R.id.rvListProfiles);

    listProfileAdapter = new ListProfileAdapter(list, this);
    RecyclerView.LayoutManager mLayoutManager = new 
    LinearLayoutManager(getApplicationContext());
    rvListProfiles.setLayoutManager(mLayoutManager);
    rvListProfiles.setItemAnimator(new DefaultItemAnimator());
    rvListProfiles.setAdapter(listProfileAdapter);
    String listString = Arrays.toString(list);
    System.out.println("List "+listString);
}
}

项目xml

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:background="@android:color/darker_gray"
    android:padding="10dp"
    android:id="@+id/parentLayout">

<ImageView
    android:id="@+id/profileImage"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_alignParentStart="true"
    android:layout_centerVertical="true"
    android:src="@mipmap/love"
    android:transitionName="imageTransition" />

<TextView
    android:id="@+id/profileName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginStart="23dp"
    android:layout_toEndOf="@+id/profileImage"
    android:padding="2dp"
    android:text="TextView"
    android:textColor="@android:color/background_light"
    android:textSize="18sp"
    android:transitionName="nameTransition" />

<TextView
    android:id="@+id/profileDesc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/profileImage"
    android:layout_alignStart="@+id/profileName"
    android:layout_marginTop="5dp"
    android:maxLines="1"
    android:padding="2dp"
    android:text="@string/dummy_data"
    android:textColor="@android:color/background_light"
    android:textSize="12sp"
    android:transitionName="descTransition" />

   </RelativeLayout>

ViewHolder类

public class ListProfileViewHolder extends RecyclerView.ViewHolder {

public ImageView profileImage;
public TextView profileName;
public TextView profileDesc;
public RelativeLayout parentLayout;
public Context context;

public ListProfileViewHolder(View itemView, Context mContext) {
    super(itemView);
    this.context = mContext;
    profileImage = itemView.findViewById(R.id.profileImage);
    profileName = itemView.findViewById(R.id.profileName);
    profileDesc = itemView.findViewById(R.id.profileDesc);
    parentLayout = itemView.findViewById(R.id.parentLayout);
}

}

适配器类

public class ListProfileAdapter extends 
RecyclerView.Adapter<ListProfileViewHolder> {

private String[] list;
private Context mContext;
private int height[] = {70,70,70,
                        0,0,0,0};

public ListProfileAdapter(String[] list, Context mContext) {
    this.list = list;
    this.mContext = mContext;
}

@Override
public ListProfileViewHolder onCreateViewHolder(ViewGroup parent, int 
viewType) {
    View layoutView = 
LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, 
null);
    return new ListProfileViewHolder(layoutView, mContext);
}

@Override
public void onBindViewHolder(final ListProfileViewHolder holder, int 
position) {


    holder.parentLayout.setLayoutParams(new 
RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            height[position]));
    holder.profileName.setText(list[position]);
    holder.profileImage.setOnClickListener(new View.OnClickListener() 
{
     @Override
     public void onClick(View view) {
         Intent sharedIntent = new 
Intent(mContext,SharedActivity.class);
         sharedIntent.putExtra("name",holder.profileName.getText());
         sharedIntent.putExtra("desc",holder.profileDesc.getText());
         Pair[] pairs = new Pair[3];
         pairs[0] = new Pair<View,String> 
(holder.profileImage,"imageTransition");
         pairs[1] = new Pair<View,String> 
(holder.profileName,"nameTransition");
         pairs[2] = new Pair<View,String> 
(holder.profileDesc,"descTransition");

         ActivityOptions options = 
ActivityOptions.makeSceneTransitionAnimation((Activity) 
mContext,pairs);
         mContext.startActivity(sharedIntent,options.toBundle());
     }
 });
    holder.parentLayout.setOnClickListener(new View.OnClickListener() 
{
        @Override
        public void onClick(View view) {
            height = new int[]{0,0,0,
                    70,70,70,70,};
            notifyDataSetChanged();
        }
    });
}

@Override
public int getItemCount() {
    return list.length;
}
}

5 个答案:

答案 0 :(得分:2)

尝试这样。

holder.parentLayout.requestLayout();
            if (position == 0 || position==1 || position==2) {
                holder.parentLayout.getLayoutParams().height = height[position];
            } else {
                holder.parentLayout.getLayoutParams().height = height[position];
            }

答案 1 :(得分:1)

.xml中的高度以dp(70dp)为单位,而编码中的高度以像素为单位。 因此,要正确获取大小,您需要将dp转换为px,然后以编程方式应用它:

将dp转换为px:

public int DpToPx(Activity activity, int dp){
    Resources r  = activity.getResources();
    int px    = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());
    return px;

}

然后将其应用于您的布局参数:

holder.parentLayout.setLayoutParams(newRelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
        DPToPx(activity, height[position]));

答案 2 :(得分:1)

您可以尝试StaggeredGridLayoutManager处理非对称项目。

尝试更改此

RecyclerView.LayoutManager mLayoutManager = new 
    LinearLayoutManager(getApplicationContext());

对此

RecyclerView.LayoutManager mLayoutManager = new 
    StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);

其中一些示例:

1: Staggered GridView

2: StaggeredGridLayout

答案 3 :(得分:1)

就我而言,我向FlowLayout中添加了视图,但项目的高度都相同。

解决方案: 在项目布局xml中,仅将RelativeLayout用作根,并确保动态添加其子视图的布局也被RelativeLayout包围。

就是这样:

<RelativeLayout>
  ...
  <FlowLayout />

</RelativeLayout>

答案 4 :(得分:0)

由于您正在更新版式大小,因此需要刷新UI,请在版式大小更新后尝试调用此行:

holder.parentLayout.requestLayout(); //It is necesary to refresh the screen