我无法使RecyclerView显示数据,甚至无法对其进行硬编码。我已经尝试了所有发现的内容,以便通过Internet进行这项工作。我的代码有什么问题?
SeleccionarClienteActivity.java
public class SeleccionarClienteActivity extends AppCompatActivity {
ClientesAdapter adapter;
String user;
@Override protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seleccionar_cliente);
user = getIntent().getStringExtra("user");
List<Cliente> list = new ArrayList<>();
list.add(new Cliente("hola", "chau", 1, 1));
adapter = new ClientesAdapter(list);
setupUI();
}
void setupUI(){
findViewById(R.id.seleccionarClienteProgressBar).setVisibility(View.GONE);
setSupportActionBar((Toolbar) findViewById(R.id.seleccionarClienteToolbar));
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
if(adapter.getItemCount() <= 0){
findViewById(R.id.txtSeleccionarClienteEmpty).setVisibility(View.VISIBLE);
}
((RecyclerView) findViewById(R.id.seleccionarClienteRecyclerView)).setAdapter(adapter);
((RecyclerView) findViewById(R.id.seleccionarClienteRecyclerView)).addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
@Override public boolean onInterceptTouchEvent(@NonNull RecyclerView recyclerView, @NonNull MotionEvent motionEvent){
View childView = recyclerView.findChildViewUnder(motionEvent.getX(), motionEvent.getY());
if (childView == null) return false;
Cliente c = ((ClientesAdapter) recyclerView.getAdapter()).getItemAt(recyclerView.getChildAdapterPosition(childView));
clientSelected(c);
return true;
}
@Override public void onTouchEvent(@NonNull RecyclerView recyclerView, @NonNull MotionEvent motionEvent){}
@Override public void onRequestDisallowInterceptTouchEvent(boolean b){}
});
}
void clientSelected(Cliente c){
Intent i = new Intent(this, SeleccionarArticuloActivity.class);
i.putExtra("pedido", new Pedido(c));
startActivity(i);
}
private class ClientesAdapter extends RecyclerView.Adapter<ClientesAdapter.ClienteHolder>{
List<Cliente> list;
ClientesAdapter(List<Cliente> list){
this.list = list;
}
@NonNull @Override public ClienteHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i){
return new ClienteHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_cliente, viewGroup, false));
}
@Override public void onBindViewHolder(@NonNull ClienteHolder clienteHolder, int i){
clienteHolder.setClient(list.get(i));
}
@Override public int getItemCount(){
return list.size();
}
Cliente getItemAt(int index){
return list.get(index);
}
class ClienteHolder extends RecyclerView.ViewHolder {
ClienteHolder(@NonNull View itemView){
super(itemView);
}
void setClient(Cliente c){
((TextView) itemView.findViewById(R.id.txtNombreCliente)).setText(c.getNombre());
((TextView) itemView.findViewById(R.id.txtCodigoCliente)).setText(c.getCodigo());
}
}
}
}
activity_seleccionar_cliente.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".SeleccionarClienteActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/seleccionarClienteRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="8dp"
android:textAlignment="center"
android:visibility="gone"
android:id="@+id/txtSeleccionarClienteEmpty"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:text="@string/noclients"
android:textSize="14sp"/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/seleccionarClienteProgressBar"
android:layout_gravity="center"/>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/seleccionarClienteToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways|snap"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
card_cliente.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="72dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_light">
<TextView
android:id="@+id/txtNombreCliente"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="Marcelito Perez"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/txtCodigoCliente"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtCodigoCliente"
android:text="1296"
android:layout_margin="16dp"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/txtNombreCliente"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
我检查了所有内容:布局大小,适配器,数据(现在已经进行了硬编码),我什么都没发现。
答案 0 :(得分:0)
您没有布局管理器
LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
myRecycler.setLayoutManager(layoutManager);
您还将多次设置回收站的属性,因此只需定义一次即可,而不是对同一元素多次使用findByID进行投射。