片段有ViewPager
,在ViewPager
中,我们在两个fragments
之间滑动(让我们将其命名为子片段)。第一个和第二个sub-fragment
已获得RecyclerView
。
我不知道发生了什么,因为我在其他片段中以完全相同的方式创建了RecyclerView
并且工作得很好。
recycler_view_item
:(我确定此项目适用且其ID与Adapter类兼容
<?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"/>
包含ViewPager和TabLayout的FrameLayout
<?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>
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返回大于零
日志显示只有Constructor正在调用
我认为这是一个简单的错误,但我无法注意到......
答案 0 :(得分:0)
问题是由ViewPagerAdapter中的方法引起的。我不知道它是否是自动生成的,我是偶然复制它还是..我不知道但删除此方法有帮助。
@Override
public boolean isViewFromObject(View view, Object object) {
return false;
}