【问题标题】:Problem with horizontal recyclerview in androidandroid中水平recyclerview的问题
【发布时间】:2020-01-24 07:20:05
【问题描述】:

我正在用我的 SQLite DB 中的所有记录的数据(一条记录在另一条记录下)填充我的回收站视图。我需要水平滚动 Recycler View,因为有些字比较大,不会进入屏幕,如下图所示:

这是我的 RecyclerView 布局,但仍然无法正常工作:

<?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="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/etIngresar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Ingrese un verbo"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.08"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.023" />

    <Button
        android:id="@+id/bnMostrar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="mostrar"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.822"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.019" />
</android.support.constraint.ConstraintLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/rvListItems"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="70dp"
    android:layout_marginRight="20dp"
    android:layout_marginBottom="70dp"
    android:scrollbarSize="4dp"
    android:scrollbars="horizontal"
    android:orientation="horizontal" />

</RelativeLayout>

我无法在我的 Activity 中设置它,因为它会转换滚动水平,但它将所有记录放在一行中,我不想要它。

    LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,true);

像这样:

已编辑

我的回收站视图类:

public class RecyclerViewAdapter extends RecyclerView.Adapter {
private ArrayList<Items> listItem ;

public RecyclerViewAdapter(ArrayList<Items> listItem) {
    this.listItem = listItem;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View contentView = LayoutInflater.from(parent.getContext()).inflate(R.layout.lista, null);
    System.out.println("CREATE VIEW HOLDER: " + viewType);
    return new Holder(contentView);
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    Items item = listItem.get(position);
    Holder Holder = (Holder) holder;
    Holder.tvVerbo.setText(item.getVerbo());
    Holder.tvReferencia.setText(item.getReferencia());
    Holder.tvEu.setText(item.getEu());
    Holder.tvVoce.setText(item.getVoce());
    Holder.tvNos.setText(item.getNos());
    Holder.tvVoces.setText(item.getVoces());

    System.out.println("BIND VIEW HOLDER: " + position);
}

@Override
public int getItemCount() {
    return listItem.size();
}

public class Holder extends RecyclerView.ViewHolder{
    TextView tvVerbo;
    TextView tvReferencia;
    TextView tvEu;
    TextView tvVoce;
    TextView tvNos;
    TextView tvVoces;

    public Holder(View itemView) {
        super(itemView);
        tvVerbo = itemView.findViewById(R.id.tvLista1);
        tvReferencia = itemView.findViewById(R.id.tvLista2);
        tvEu = itemView.findViewById(R.id.tvLista3);
        tvVoce = itemView.findViewById(R.id.tvLista4);
        tvNos = itemView.findViewById(R.id.tvLista5);
        tvVoces = itemView.findViewById(R.id.tvLista6);

    }
}
}

我调用 Recycler 时的活动 Verbos2:

public class Verbos2 extends AppCompatActivity {

RecyclerView recyclerViewItem;
RecyclerViewAdapter recyclerViewVerbos;
EditText etVerbos;
Button mostrar;

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

    etVerbos = findViewById(R.id.etIngresar);
    mostrar = findViewById(R.id.bnMostrar);

    recyclerViewItem = findViewById(R.id.rvListItems);
    LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL,false);
    recyclerViewItem.setLayoutManager(manager);

    mostrar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            List<Items> completeList = new ArrayList<>();
            completeList.addAll(mostrarVerbos());
            recyclerViewVerbos = new RecyclerViewAdapter((ArrayList<Items>) completeList);
            recyclerViewItem.setAdapter(recyclerViewVerbos);
        }
    });
}

public List<Items> mostrarVerbos(){
    BaseDeDatos admin = new BaseDeDatos(getApplicationContext(), "verbos.db", getApplicationContext(), 11);
    SQLiteDatabase db = admin.getReadableDatabase();
    String[] parametros = {etVerbos.getText().toString()};
    Cursor cursor = db.rawQuery("SELECT * FROM verbos WHERE verbos =?", parametros);
    List<Items> verbos= new ArrayList<>();
    if(cursor.moveToFirst()){
        do {
            verbos.add(new Items(cursor.getString(1), " " + cursor.getString(2), " " + cursor.getString(3), " " + cursor.getString(4), " " + cursor.getString(5), " " + cursor.getString(6)));
        }while (cursor.moveToNext());
    }else{
        Toast.makeText(getApplicationContext(), "El verbo no existe", Toast.LENGTH_LONG).show();
    }
    return verbos;
}
}

列表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="match_parent"
xmlns:andoid="http://schemas.android.com/apk/res-auto">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/tvLista1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="algo1"
        android:textSize="15sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tvLista2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="algo 2"
        android:textSize="15sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tvLista3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="algo 3"
        android:textSize="15sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tvLista4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="algo 4"
        android:textSize="15sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tvLista5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="algo 5"
        android:textSize="15sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tvLista6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="algo 5"
        android:textSize="15sp"
        android:textStyle="bold" />

</LinearLayout>

</LinearLayout>

已编辑 2

如您所见,它会水平滚动到每条记录。我只需要一个同时包含所有记录的水平滚动条(在这种情况下为 3 条记录)

【问题讨论】:

  • 请粘贴xml的Recyclerview adapter类。

标签: android android-recyclerview scroll


【解决方案1】:

尝试在您的项目布局中添加水平滚动视图,并将 ma​​xLines 1 设置到您的项目 textView 中。

试试下面的代码

<?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">
    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true"
        android:overScrollMode="never">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/tvLista1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="algo1"
                android:maxLines="1"
                android:textSize="15sp"
                android:textStyle="bold" />
            <TextView
                android:id="@+id/tvLista2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="algo 2"
                android:maxLines="1"
                android:textSize="15sp"
                android:textStyle="bold" />
            <TextView
                android:id="@+id/tvLista3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="algo 3"
                android:maxLines="1"
                android:textSize="15sp"
                android:textStyle="bold" />
            <TextView
                android:id="@+id/tvLista4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="algo 4"
                android:maxLines="1"
                android:textSize="15sp"
                android:textStyle="bold" />
            <TextView
                android:id="@+id/tvLista5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="algo 5"
                android:maxLines="1"
                android:textSize="15sp"
                android:textStyle="bold" />
            <TextView
                android:id="@+id/tvLista6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="algo 5"
                android:maxLines="1"
                android:textSize="15sp"
                android:textStyle="bold" />
        </LinearLayout>
    </HorizontalScrollView>
</LinearLayout>

希望对你有帮助!

谢谢。

【讨论】:

  • 几乎成功了!因为它只将水平滚动添加到较大的记录(第二个)而不是另外两个记录。我需要相同但同时水平滚动所有记录,而不仅仅是其中一些记录
  • 你想要什么?水平滚动还是垂直滚动?
  • 如果记录很大,你想要水平滚动还是垂直滚动?
  • 我只想水平滚动,同时滚动所有记录(在本例中为 3)。使用您的方法,它只滚动一条记录,我的意思是,它会水平滚动到每条记录。我需要同时水平滚动到所有记录,而不是每条记录滚动一次
  • 你能分享一下产生了什么问题的屏幕截图吗?
【解决方案2】:

我不知道问题出在您的回收站视图上。您必须在 recyclerview 的布局项内的 TextView 上将 multiline 设置为 true,以便文本可以跨越多行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-20
    • 1970-01-01
    • 2017-08-29
    • 2019-01-20
    • 1970-01-01
    • 2016-06-11
    • 2016-10-10
    • 2016-08-15
    相关资源
    最近更新 更多