android recyclerview 加载数据过多会奔溃吗

2025-01-03 00:44:57
推荐回答(1个)
回答1:

item数量超过6条就会出现item重复,乱序
代码如下
fragment布局
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="invinciblejoe.com.lightingbuy.main.LightingFragment">

android:id="@+id/commodity_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />


recyclerview item布局
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/invinciblejoe.com.lightingbuy"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
app:cardBackgroundColor="@color/orange"
app:cardCornerRadius="20dp">

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

android:id="@+id/pic"
android:layout_width="match_parent"
android:layout_height="120dp"
android:gravity="center"
android:textSize="50sp"
/>

android:clickable="true"
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@color/orange"
android:textColor="@android:color/white" />


recycerview adapter
public class LightingRVAdapter extends RecyclerView.Adapter { private List mlist; private Context mContext; private LightingViewHolder viewHolder; public LightingRVAdapter(Context mContext, List mlist) { this.mContext = mContext; this.mlist = mlist;
} @Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_lighting, parent, false);
viewHolder = new LightingViewHolder(v); return viewHolder;
} @Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

Commodity c = mlist.get(position);
viewHolder.mImageView.setText(c.getName());
viewHolder.mTextView.setText(String.valueOf(c.getPrice_discont()));
} @Override
public int getItemCount() { return mlist == null ? 0 : mlist.size();
} private class LightingViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener
{ public TextView mTextView; public TextView mImageView; public LightingViewHolder(View v )
{ super(v);
mTextView = (TextView) v.findViewById(R.id.name);
mImageView = (TextView) v.findViewById(R.id.pic);
mImageView.setOnClickListener(this);
} @Override
public void onClick(View view) { switch (view.getId()) { case R.id.pic :
mTextView.setText("OnChlic"); break;
}
}
}

}