简体   繁体   English

如何使用约束布局创建水平回收视图?

[英]How to create horizontal recyclerview using constraintlayout?

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="wrap_content">

        <ImageView
            android:id="@+id/titleImageViewID"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:contentDescription="@string/todo"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/mac" />

        <View
            android:id="@+id/view8"
            android:layout_width="0dp"
            android:layout_height="86dp"
            android:layout_marginTop="168dp"
            android:background="#8A474141"
            app:layout_constraintBottom_toBottomOf="@id/titleImageViewID"
            app:layout_constraintEnd_toEndOf="@+id/titleImageViewID"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="@+id/titleImageViewID"
            app:layout_constraintTop_toTopOf="@+id/titleImageViewID" />

        <TextView
            android:id="@+id/titleTextViewID"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:text="@string/textview"
            android:textColor="@android:color/white"
            android:textSize="24sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toTopOf="@+id/view8"
            app:layout_constraintEnd_toEndOf="@+id/view8"
            app:layout_constraintStart_toStartOf="@+id/view8"
            app:layout_constraintTop_toBottomOf="@+id/titleImageViewID"/>
    </androidx.constraintlayout.widget.ConstraintLayout>

Above code is sample_layout of a RecyclerView - please suggest for only ConstraintLayout , I use this layout because this is responsive to any device, so I don't need to create another layout for different screen sizes上面的代码是 RecyclerView 的sample_layout - 请只建议ConstraintLayout ,我使用这个布局是因为它可以响应任何设备,所以我不需要为不同的屏幕尺寸创建另一个布局

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private RecyclerView recyclerView;
    private RecycleAdapter recycleAdapter;
    private RecyclerView.LayoutManager manager;
    private ConstraintLayout constraintLayout;

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

        recyclerView = findViewById(R.id.recycleViewID);
        manager = new ConstraintLayout(this,ConstraintLayout,View.SCROLL_AXIS_HORIZONTAL,false);

       RecycleModel [] myThumbData = new RecycleModel[]{
       new RecycleModel("Goru banaisi", R.drawable.mac)};}

Can't figure out the main java code how do I access all of my XML in java code, anyone give me a proper code and explanation, I couldn't find any recycler view using constraint layout online.无法弄清楚主要的 java 代码如何访问我在 java 代码中的所有 XML 中的所有 XML,任何人都可以使用约束查看器在线找到正确的代码和解释,我无法在线查找。 So problem solver help me to solve my problem**所以问题解决者帮我解决我的问题**

Set your recyclerview manager as shown below:如下所示设置您的recyclerview manager

manager = new LinearLayoutManager(this, RecyclerView.Horizontal, false)
reyclerview.manager = manager
//Then set the adapter
recyclerview.adapter = your adapter.

This should enable horizontal scroll for your recyclerview .这应该为您的recyclerview启用horizontal scroll

Hope this helps希望这可以帮助

LayoutManager布局管理器

You have to set proper LayoutManager .您必须设置正确的LayoutManager There are few basic implementations eg LinearLayoutManager , GridLayoutManager or StaggeredGridLayoutManager .基本的实现很少,例如LinearLayoutManagerGridLayoutManagerStaggeredGridLayoutManager

In Android Documentation you can find information that:Android 文档中,您可以找到以下信息:

A LayoutManager is responsible for measuring and positioning item views within a RecyclerView as well as determining the policy for when to recycle item views that are no longer visible to the user. LayoutManager 负责测量和定位 RecyclerView 中的项目视图,以及确定何时回收用户不再可见的项目视图的策略。 By changing the LayoutManager a RecyclerView can be used to implement a standard vertically scrolling list, a uniform grid, staggered grids, horizontally scrolling collections and more.通过更改 LayoutManager,RecyclerView 可用于实现标准垂直滚动列表、统一网格、交错网格、水平滚动 collections 等。 Several stock layout managers are provided for general use.提供了几个库存布局管理器供一般使用。

Set from Code从代码设置

You can set them from code:您可以从代码中设置它们:

layoutManager = new LinearLayoutManager(
        this,
        LinearLayoutManager.HORIZONTAL,   // Here, you have orientation of items
        false
);

// Remember to set your LayoutManager in recycler view, not in adapter!
recyclerView.setLayoutManager(layoutManager);

Set from XML设置从 XML

In your XML layout file, where you have your adapter, you have to set:在您拥有适配器的 XML 布局文件中,您必须设置:

  • orientation and orientation
  • layoutManager . layoutManager
<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

回收商视图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM