2017-02-26 12 views
0

フラグメントはViewPagerで、ViewPagerにあります。これは2つのサブタイプの間でスワイプしています。そして、最初と2番目のsub-fragmentRecyclerViewを持っています。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ことを確信しているが、唯一のコンストラクタは

を呼び出している、私はそれがあることを考えることを示しています簡単な間違いですが、私はそれに気づくことができません...

+0

listOfPaymentsの初期化を削除します。リスト listOfPayments; // = new ArrayList <>(); ' –

+0

「setupRecyclerView」には何がありますか? – theblitz

+0

@mahmoudmoustafa何も変わらない @theblitz setupRecyclerViewには、RecyclerViewの基本設定が含まれています。 'recyclerView.setHasFixedSize(true); 'RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity()。getApplicationContext()); 'recyclerView.setLayoutManager(mLayoutManager); 'recyclerView.setItemAnimator(new DefaultItemAnimator()); –

答えて

0

ViewPagerAdapterのメソッドで問題が発生しました。私はそれが自動生成されたかどうかわからない、私はそれを誤ってコピーした..または私は知っているが、このメソッドを削除すると助けた。

@Override 
public boolean isViewFromObject(View view, Object object) { 
    return false; 
} 
関連する問題