RecyclerView在ConstraintLayout

时间:2017-09-18 14:45:44

标签: android android-recyclerview android-constraintlayout

我在RecyclerView中遇到了一个视觉错误,直到我发现它是由RecyclerView放入ConstraintLayout引起的。

这是我尝试实现的目标(RecyclerView GridLayoutManagerLinearLayout}:

enter image description here

这是我的第一次尝试(RecyclerView GridLayoutManager ConstraintLayout内的RecyclerView

enter image description here

所以我的问题是:RecyclerView生活在ConstraintLayout内时,为什么RecyclerView项目无法正常使用?

使用布局检查器查看会产生wrap_content覆盖整个屏幕宽度。

此外,在为项目的match_parentandroid:layout_width分配android:layout_heightwrap_content时,这些项目按预期展开。

例如,当两者都设置为ConstraintLayout时,这是package com.example.recyclerviewtest; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.RecyclerView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RecyclerView recyclerView = findViewById(R.id.recyclerView); recyclerView.setAdapter(new MyRecyclerViewAdapter()); } } 中的结果:

enter image description here

这是破损布局的来源(第二个截图)。

MainActivity.java

package com.example.recyclerviewtest;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MyRecyclerViewAdapter extends RecyclerView.Adapter {

    class ViewHolder extends RecyclerView.ViewHolder {
        ViewHolder(View itemView) {
            super(itemView);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View view = layoutInflater.inflate(R.layout.recycler_view_item, parent, false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public int getItemCount() {
        return 3;
    }
}

MyRecyclerViewAdapter.java

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="GridLayoutManager"
        app:spanCount="2" />

</android.support.constraint.ConstraintLayout>

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:background="@android:color/holo_green_light"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="LINE 1" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="LINE 2" />

</LinearLayout>

recycler_view_item.xml

android:layout_width

总结:使用android:layout_height时,在将固定值设置为ConstraintLayoutimport numpy as np import cv2 gray = cv2.imread('image.png') edges = cv2.Canny(gray,50,150,apertureSize = 3) cv2.imwrite('edges-50-150.jpg',edges) minLineLength=100 lines = cv2.HoughLinesP(image=edges,rho=1,theta=np.pi/180, threshold=100,lines=np.array([]), minLineLength=minLineLength,maxLineGap=80) a,b,c = lines.shape for i in range(a): x = lines[i][0][0] - lines [i][0][2] y = lines[i][0][1] - lines [i][0][3] if x!= 0: if abs(y/x) <1: cv2.line(gray, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (255, 255, 255), 1, cv2.LINE_AA) se = cv2.getStructuringElement(cv2.MORPH_ELLIPSE , (3,3)) gray = cv2.morphologyEx(gray, cv2.MORPH_CLOSE, se) cv2.imwrite('houghlines.jpg', gray) cv2.imshow('img', gray) cv2.waitKey(0) 时,布局才会中断。

1 个答案:

答案 0 :(得分:1)

尝试将您的回收者视图宽度设置为0dp,添加一个约束startToStarOf父级和一个约束endToEndOf父级。另外,将对比度horizo​​ntalWeight添加到1。

让我知道它有效。