gpt4 book ai didi

java - 如何显示水平 Recyclerview - 显示水平项目时遇到问题

转载 作者:行者123 更新时间:2023-12-01 23:29:32 27 4
gpt4 key购买 nike

我想在recyclerview中显示水平项目,但我尝试过但没有成功,只显示放大宽度的水平项目。

在Android Studio中按我想要的方式显示

enter image description here

但是在我的移动设备中运行时,显示给我

enter image description here

这些项目是宽度换行的,但其他项目在滚动后出现,与我的上一个项目分开。

这是我的代码。

LIST_ITEM

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">


<LinearLayout
android:id="@+id/linearFondo"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="@drawable/item_list_sel"
android:orientation="horizontal">

<ImageView
android:id="@+id/imageView2"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
app:srcCompat="@drawable/ruta"
tools:srcCompat="@drawable/ruta" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:orientation="vertical">

<TextView
android:id="@+id/tnombre"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:inputType="textMultiLine"
android:lines="1"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textSize="12sp"
android:textStyle="bold"></TextView>

<TextView
android:id="@+id/thorarios"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textColor="@color/colorPrimary"
android:textSize="10sp"
android:visibility="gone"></TextView>

</LinearLayout>

</LinearLayout>
</LinearLayout>

我的 Xml 来显示回收器 View

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="sitetech.NFCcheckPoint.ui.operador.CheckFragment">

<!-- TODO: Update blank fragment layout -->

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@color/SuccessColor"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="10dp"
android:paddingRight="10dp">

<TextView
android:id="@+id/tfecha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Fecha"
android:textSize="20sp" />

<Button
android:id="@+id/bpruebas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pasar Tarjeta" />

</LinearLayout>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="Ruta para el registro" />

<com.omega_r.libs.omegarecyclerview.OmegaRecyclerView
android:id="@+id/rlista"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#888"
android:dividerHeight="0dp"
android:fadeScrollbars="true"
android:orientation="horizontal"
android:overScrollMode="never"
android:scrollbarAlwaysDrawHorizontalTrack="true"
android:scrollbarAlwaysDrawVerticalTrack="false"
android:scrollbars="horizontal"
app:itemSpace="0dp"
tools:listitem="@layout/ruta_sel_template">

</com.omega_r.libs.omegarecyclerview.OmegaRecyclerView>

</LinearLayout>
</LinearLayout>

以及要加载的 fragment 代码

dataAdapter = new rutaSelAdapter(lista, new onItemClick() {
@Override
public void onClickItemList(View v, int position) {
ToastHelper.info(lista.get(position).getNombre().toString());
}
});

rlista.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
rlista.setAdapter(dataAdapter);

还有我的 dataAdapter 类

public class rutaSelAdapter extends OmegaRecyclerView.Adapter<rutaSelAdapter.ViewHolder> {
public List<Ruta> lista;
private onItemClick onItemClick;
private Ruta selectedItem;

public rutaSelAdapter(List<Ruta> l, onItemClick onclick) {
lista = l;
this.onItemClick = onclick;
if (l.size() > 0)
setSelectedItem(l.get(0));
}

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

@Override
public rutaSelAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new rutaSelAdapter.ViewHolder(parent);
}

@Override
public void onBindViewHolder(rutaSelAdapter.ViewHolder holder, int position) {
Ruta dato = lista.get(position);
holder.display(dato);
}

public void setSelectedItem(Ruta rx){
selectedItem = rx;
}

public Ruta getSelectedItem(){
return selectedItem;
}

public void updateData(Ruta bx) {
boolean nuevo = true;
for (Ruta rx : lista) {
if (rx.getId() == bx.getId()) {
lista.set(lista.indexOf(rx), bx);
nuevo = false;
}
ToastHelper.info("Se a modificado el horario.");
}

if (nuevo) {
lista.add(bx);
ToastHelper.exito("Horario asignado.");
}

notifyDataSetChanged();
}

public void deleteData(Ruta rx) {
//ToastHelper.normal("Se a eliminado el horario " + rx.getHora());
lista.remove(rx);
notifyDataSetChanged();
}

public class ViewHolder extends SwipeViewHolder implements View.OnClickListener {
private final TextView tnombre;
private LinearLayout linearFondo;
HorarioDao horarioManager = AppController.daoSession.getHorarioDao();

private Ruta currentItem;

public ViewHolder(ViewGroup itemView) {
super(itemView, R.layout.ruta_sel_template, SwipeViewHolder.NO_ID, SwipeViewHolder.NO_ID);

tnombre = findViewById(R.id.tnombre);
linearFondo = findViewById(R.id.linearFondo);
contentView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setSelectedItem(currentItem);
onItemClick.onClickItemList(v, getAdapterPosition());
}
});
}

private Ruta backItem;
private void setSelection(Ruta rx){
//if (backItem == null)

}

@Override
public void onClick(final View v) {

}

public void display(Ruta rx) {
currentItem = rx;
if (rx.getNombre() == null) tnombre.setText("");
else tnombre.setText(rx.getNombre().toString());
}
}
}

在 Android Studio 中按照我的意愿显示。

但在运行时,在我的移动设备中,项目具有全宽但包裹内容。 enter image description here

最佳答案

我认为您只需在类适配器中添加notifyDataSetChanged

public rutaSelAdapter(List<Ruta> l, onItemClick onclick) {
lista = l;
this.onItemClick = onclick;
if (l.size() > 0)
setSelectedItem(l.get(0));
notifyDataSetChanged();
}

关于java - 如何显示水平 Recyclerview - 显示水平项目时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58295941/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com