这是我第一次实施RecyclerView。
我正在尝试从数据库中检索数据,然后实例化一个名为服务的类,该类将用于动态创建CardView以作为项目显示在RecyclerView中。 RecyclerView是Fragment类的一部分。
Fragment类:
package com.alsowaygh.getitdone.view.services;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.alsowaygh.getitdone.R;
import com.alsowaygh.getitdone.controller.service.QueryServices;
import com.alsowaygh.getitdone.controller.service.Service;
import java.util.ArrayList;
public class ServicesListFragment extends Fragment {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private CardView mServiceCardView;
private RecyclerView.LayoutManager mLayoutManager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.services_list_fragment, container, false);
mRecyclerView = rootView.findViewById(R.id.services_recycler_view);
mServiceCardView = rootView.findViewById(R.id.service_card_view);
mLayoutManager = new LinearLayoutManager(rootView.getContext());
mRecyclerView.setLayoutManager(mLayoutManager);
//retreive argument from CategoriesListFragment (selected)
String selectedCategory = getArguments().getString("category");
//Query services for selected category
QueryServices queryServices = new QueryServices();
ArrayList<Service> services = queryServices.getServicesForCategory(selectedCategory);
mAdapter = new ServiceListAdapter(services);
mRecyclerView.setAdapter(mAdapter);
return rootView;
}
}
片段布局包括Recycler View:
<LinearLayout android:id="@+id/categories_list_fragment"
android:name="com.alsowaygh.getitdone.view.services.ServicesListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.widget.RecyclerView
android:id="@+id/services_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
适配器类这是我卡住的地方,代码不完整:
package com.alsowaygh.getitdone.view.services;
import android.support.annotation.NonNull;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.alsowaygh.getitdone.R;
import com.alsowaygh.getitdone.controller.service.Service;
import java.util.ArrayList;
public class ServiceListAdapter extends RecyclerView.Adapter {
private ArrayList<Service> mServices;
public static class ViewHolder extends RecyclerView.ViewHolder{
public ViewHolder(View itemView) {
super(itemView);
}
}
public ServiceListAdapter(ArrayList<Service> services){
mServices = services;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
// create a new view
CardView v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_service, parent, false);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return 0;
}
}
布局,包括必须添加到RecyclerView的CardView
<?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"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/service_card_view"
android:layout_width="match_parent"
android:layout_height="140dp"
android:layout_gravity="center"
card_view:cardBackgroundColor="@color/defaultActivityBackground"
card_view:cardCornerRadius="10dp"
card_view:contentPadding="5dp">
<TextView
android:id="@+id/title"
style="@style/item_list_service_title_text_style"
android:layout_width="221dp"
android:layout_height="40dp"
android:layout_gravity="top"
android:layout_marginLeft="10dp"
android:gravity="center_vertical"
android:text="Service Title" />
<TextView
android:id="@+id/providerName"
style="@style/item_list_service_detail_text_style"
android:layout_width="221dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:gravity="center_vertical"
android:text="Provider Name" />
<TextView
android:id="@+id/price"
style="@style/item_list_service_detail_text_style"
android:layout_width="221dp"
android:layout_height="38dp"
android:layout_gravity="bottom"
android:layout_marginLeft="10dp"
android:gravity="center_vertical"
android:text="Price" />
<ImageView
android:id="@+id/imageView"
android:layout_width="141dp"
android:layout_height="140dp"
android:layout_gravity="right"
android:tint="@color/colorAccent"
android:background="@android:color/white"
app:srcCompat="@drawable/service_image_placeholder" />
</android.support.v7.widget.CardView>
</LinearLayout>
我不知道如何将各种东西放在一起,以便在RecyclerView中动态嵌入CardView
非常感谢您的帮助..
答案 0 :(得分:0)
让我先开始帮助您onCreateViewHolder()
和onBindViewHolder()
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_service, parent, false);
return new MyViewHolder(v);
}
另外,
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
MyViewHolder holder1 = (MyViewHolder) holder;
holder1.titleTextView.setText("...");
//Set the rest of UI elements the same way
}
现在,将MyViewHolder
这样的ViewHolder类替换为
public static class MyViewHolder extends RecyclerView.ViewHolder{
TextView titleTextView;
//Create the other UI elements here for use in onBindViewHolder()
public ViewHolder(View itemView) {
super(itemView);
titleTextView = itemView.findViewById(R.id.text_view_title);
//Initialise other UI elements here
}
}
随时问你是否被困在任何地方。