我尝试实现RecyclerViewAdapter
,其中包含存储在CardView
中的文本和图像列表,但是当我尝试它时,它既不能在模拟器上运行也不能在一个真实的设备。
适配器类:
public class MainRecyclerAdapter extends RecyclerView.Adapter<MainRecyclerAdapter.MainRecyclerViewHolder>{
Context context;
ArrayList<MainItem> itemsList;
LayoutInflater inflater;
public MainRecyclerAdapter(Context context) {
this.context = context;
}
public MainRecyclerAdapter(Context context, ArrayList<MainItem> itemsList) {
this.context = context;
this.itemsList = itemsList;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void add(MainItem item){
itemsList.add(item);
notifyDataSetChanged();
}
public void addAll(Collection<MainItem> itemCollection){
for (MainItem item : itemCollection) itemsList.add(item);
notifyDataSetChanged();
}
public void removeItem(int position){
itemsList.remove(position);
notifyDataSetChanged();
}
public void clear(){
itemsList.clear();
notifyDataSetChanged();
}
@Override
public MainRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new MainRecyclerViewHolder(inflater.inflate(R.layout.main_menu_item, parent, false));
}
@Override
public void onBindViewHolder(MainRecyclerViewHolder holder, int position) {
YoYo.with(Techniques.ZoomIn).playOn(holder.cardView);
MainItem item = itemsList.get(position);
holder.textView.setText(item.getText());
Picasso.with(context)
.load(item.getImageResourceID())
.into(holder.imageView);
if (item.getColor() != null)
holder.cardView.setCardBackgroundColor(item.getColor());
}
@Override
public int getItemCount() {
return itemsList.size();
}
public class MainRecyclerViewHolder extends RecyclerView.ViewHolder{
CardView cardView;
TextView textView;
ImageView imageView;
public MainRecyclerViewHolder(View itemView) {
super(itemView);
cardView = itemView.findViewById(R.id.mainItemCardView);
textView = itemView.findViewById(R.id.mainCardTextView);
imageView = itemView.findViewById(R.id.mainCardImageView);
}
}
}
我的主要活动布局:
<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"
android:background="@drawable/main_wallpaper"
tools:context="com.r3tr0.weekplanner.MainActivity">
<android.support.v7.widget.CardView
android:id="@+id/headMainCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:cardCornerRadius="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="12dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="Welcome to the week planner app"
android:textAlignment="center"
android:textSize="24sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:text="Let's start by making your life a little more organized!"
android:textSize="18sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.RecyclerView
android:id="@+id/mainRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="16dp"
/>
我的OnCreate方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Palette palette = Palette.from(BitmapFactory.decodeResource(getResources(), R.drawable.main_wallpaper)).generate();
((CardView) findViewById(R.id.headMainCardView)).setCardBackgroundColor(palette.getDominantColor(Color.parseColor("#FFFFFF")));
initializeRecyclerView();
}
void initializeRecyclerView(){
RecyclerView recyclerView = findViewById(R.id.mainRecyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setItemViewCacheSize(20);
recyclerView.setDrawingCacheEnabled(true);
recyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
adapter = new MainRecyclerAdapter(this, new ArrayList<MainItem>());
adapter.add(new MainItem("Coming soon", R.drawable.soon_transparent, Color.parseColor("#91677D")));
adapter.add(new MainItem("Week's plan", R.drawable.schedule_transparent, Color.parseColor("#95A6C4")));
adapter.add(new MainItem("My vault", R.drawable.vault_transparent, Color.parseColor("#CCD9E9")));
adapter.add(new MainItem("My achievements", R.drawable.soon_transparent, Color.parseColor("#E65D6D")));
adapter.add(new MainItem("Coming soon!", R.drawable.soon_transparent, Color.parseColor("#FC9D94")));
adapter.add(new MainItem("Exit", R.drawable.x_transparent, Color.parseColor("#BDCCD3")));
recyclerView.setLayoutManager(new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false));
recyclerView.setAdapter(adapter);
}
P.S: 我见过像this one和this这样的解决方案,但没有人帮助过多。
提前致谢
答案 0 :(得分:0)
如下所示初始化您的适配器。
public void add(MainItem item){
itemsList.add(item);
notifyItemInserted(itemsList.size()-1);
}
public void removeItem(int position){
itemsList.remove(position);
notifyItemRemoved(position);
}
根据您的适配器代码。您的添加和删除方法应如下所示。
inp='sample'
n=5
print [(inp,i) for i in range(n)]
将RecyclerView.Adapter读取到更多的视图持有者模式。
答案 1 :(得分:0)
尝试将此行添加到您的代码中
recyclerView.setNestedScrollingEnabled(false);
因为您的recyclerView可能会改为NestedScrollView
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycle2"
android:nestedScrollingEnabled="false"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
答案 2 :(得分:0)
现在这很有趣......我尝试了Picasso
库而不是Glide
并将图片的分辨率降低到256X256,现在RecyclerView
运行顺利,我会更新带有新代码的适配器类,以防任何人需要它。