片段有ViewPager
,在ViewPager
中,我們在兩個fragments
(讓我們將其命名爲子片段)之間滑動。第一個和第二個sub-fragment
已得到RecyclerView
。RecyclerView不會調用onCreateViewHolder和其他方法
我不知道發生了什麼,因爲我在其他片段中完全以相同的方式創建RecyclerView
,它的工作效果很好。
recycler_view_item
:(我敢肯定,這產品appriopriate其ID與適配器類包含ViewPager和TabLayout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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"
tools:context="com.example.android.debtors.Fragments.FragmentSingleClientInfo">
<android.support.design.widget.TabLayout
android:id="@+id/clientsinfo_tabs"
style="@style/CategoryTab"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:id="@+id/clientsinfo_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
<?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="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorHeaderText"
android:weightSum="100"
android:layout_marginBottom="4dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="20"
android:weightSum="50">
<TextView
android:text="Client"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:id="@+id/textView8"
android:layout_weight="15"
style="@style/ItemPaymentsHeaderTextView"
/>
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/clientinfo_item_client"
android:layout_weight="35"
style="@style/ItemPaymentsInfoTextView"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="25"
android:weightSum="50">
<TextView
android:text="Date"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:id="@+id/textView13"
android:layout_weight="15"
style="@style/ItemPaymentsHeaderTextView"
/>
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/clientinfo_item_date"
android:layout_weight="35"
style="@style/ItemPaymentsInfoTextView"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="27"
android:weightSum="50">
<TextView
android:text="Payment amount"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:id="@+id/textView9"
android:layout_weight="15"
style="@style/ItemPaymentsHeaderTextView"
/>
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/clientinfo_item_totalamount"
android:layout_weight="35"
style="@style/ItemPaymentsInfoTextView"/>
</LinearLayout>
<!--TODO jak bedzie po wszystkim to wyrzucic TYP (określony w TabLayout)-->
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="20"
android:weightSum="50">
<TextView
android:text="Type"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:id="@+id/textView10"
android:layout_weight="15"
style="@style/ItemPaymentsHeaderTextView"
/>
<TextView
android:text="Purchase"
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/clientinfo_item_type"
android:layout_weight="35"
style="@style/ItemPaymentsInfoTextView"/>
</LinearLayout>
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
app:srcCompat="@drawable/arrow_expand_24"
android:id="@+id/clientinfo_item_downarrow"
android:background="@color/icon_transparent"
android:layout_weight="5"
android:scaleType="fitCenter"
android:elevation="0dp"
android:layout_gravity="center_vertical|clip_horizontal"/>
的FrameLayout兼容onCreateView - 方法,我創建RecyclerView
我傳遞給AdapterClientInfo的listOfPayments不爲null。
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
listOfPayments = getPaymentsByClientId(clientsID);
for(Payment p : listOfPayments){
Log.i(TAG, "onCreateView: payment : " + p.toString(true));
}
View rootView = inflater.inflate(R.layout.recycler_view_with_viewpager,container, false);
AdapterClientInfo adapterClientInfo = new AdapterClientInfo(getContext(),listOfPayments);
RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view_with_viewpager);
setupRecyclerView(recyclerView);
recyclerView.setAdapter(adapterClientInfo);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return rootView;
}
在上面的方法我誇大layout.recycler_view_with_viewpager:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="55dp"
android:id="@+id/recycler_view_with_viewpager"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
而且在AdapterClientInfo唯一的構造在創建實例和任何其他方法不起作用呼喚,沒有什麼被稱爲。
AdapterClientInfo:
public class AdapterClientInfo extends RecyclerView.Adapter<AdapterClientInfo.MyViewHolder> {
private static final String TAG = AdapterClientInfo.class.getSimpleName();
List<Payment> listOfPayments = new ArrayList<>();
Context context;
public AdapterClientInfo(Context context, List<Payment> list) {
Log.i(TAG, "AdapterClientInfo: ");
this.context = context;
this.listOfPayments = list;
Log.i(TAG, "AdapterClientInfo: size of list : " + list.size());
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.i(TAG, "onCreateViewHolder: ");
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_clients_info_payments, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Log.i(TAG, "onBindViewHolder: START");
Payment payment = listOfPayments.get(position);
Log.i(TAG, "onBindViewHolder: payment " + payment.toString());
String clientName = getClientByID(payment.getPaymentClientID()).getClientName();
String[] dateArray = payment.getPaymentDate().split(" ");
String dateString = dateArray[0];
holder.textViewClient.setText(clientName);
holder.textViewDate.setText(dateString);
holder.textViewPaymentAmount.setText(String.valueOf(payment.getPaymentAmount()));
if(payment.isPaymentGotOrGiven())//if tru
holder.textViewType.setText("Received");
else
holder.textViewType.setText("Given");
Log.i(TAG, "onBindViewHolder: END");
}
@Override
public int getItemCount() {
Log.i(TAG, "getItemCount: " + listOfPayments.size());
return listOfPayments.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView textViewClient, textViewPaymentAmount, textViewDate, textViewType;
public MyViewHolder(View itemView) {
super(itemView);
Log.i(TAG, "MyViewHolder: ");
textViewClient = (TextView) itemView.findViewById(R.id.clientinfo_item_client);
textViewPaymentAmount = (TextView) itemView.findViewById(R.id.clientinfo_item_totalamount);
textViewDate = (TextView) itemView.findViewById(R.id.clientinfo_item_date);
textViewType = (TextView) itemView.findViewById(R.id.clientinfo_item_type);
}
}
private Client getClientByID(long ID){
DatabaseClients dbClients = new DatabaseClients(context);
Client client = dbClients.getClientByID(ID);
return client;
}
}
我敢肯定,listOfPayments的大小大於0,因此getItemCount返回大於零個
記錄顯示,僅構造方法調用
我認爲這是簡單的錯誤,但我不能注意到...
刪除listOfPayments的初始化,'List listOfPayments; // = new ArrayList <>();' –
什麼是「setupRecyclerView」? – theblitz
@mahmoudmoustafa它不會改變任何東西 @theblitz setupRecyclerView包含RecyclerView的基本配置: 'recyclerView.setHasFixedSize(true); 'RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity()。getApplicationContext()); 'recyclerView.setLayoutManager(mLayoutManager); 'recyclerView.setItemAnimator(new DefaultItemAnimator()); –