2017-01-25 112 views
0

我試圖創建recyclerView一個cardView與創建CardView編程,這裏是我的代碼,如果它是不可能的,請讓我知道使用XMLRecyclerView不顯示編程CardView

onCreate主類的

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.lesson_layout, container, false); 

    ExampleRecyclerView = (RecyclerView) rootView.findViewById(R.id.ExampleRecyclerView); 
    List<ViewExample> exampleList = new ArrayList<>(); 
    Log.i("dddddd", String.valueOf(exampleList)); 
    //alos the list just show [] in the Logs 

    adapter = new ExampleAdapter(this, exampleList); 
    LinearLayoutManager llm = new LinearLayoutManager(getActivity()); 
    llm.setSmoothScrollbarEnabled(true); 
    ExampleRecyclerView.setScrollbarFadingEnabled(true); 
    ExampleRecyclerView.setLayoutManager(llm); 

    ExampleRecyclerView.setAdapter(adapter); 
    return rootView; 
} 

ViewExample

public class ViewExample { 
    private String StepHeader, Code, Explanation; 

    public ViewExample(String stepHeader, String code, String explanation) { 
     StepHeader = stepHeader; 
     Code = code; 
     Explanation = explanation; 
    } 

    Getters & Setters are here... 
} 

而且Adapter

public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleHolder> { 

    CardView ExampleCV; 
    List<ViewExample> exampleList; 
    ViewLesson viewLesson; 


    public ExampleAdapter(ViewLesson viewLesson, List<ViewExample> exampleList) { 
     this.viewLesson = viewLesson; 
     this.exampleList = exampleList; 
    } 


    public class ExampleHolder extends RecyclerView.ViewHolder { // here is where you define what text have value 
     LinearLayout parentL; 
     public ExampleHolder(View itemView) { 
      super(itemView); 
      parentL = new LinearLayout(parentL.getContext()); 
      ExampleCV = new CardView(ExampleCV.getContext()); 
      ExampleCV.setLayoutParams(new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT)); 
      parentL.setLayoutParams(new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT)); 
      parentL.setOrientation(LinearLayout.VERTICAL); 
      ExampleCV.addView(parentL); 
     } 
    } 

    @Override 
    public ExampleHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     return new ExampleHolder(ExampleCV); 
    } 

    @Override 
    public void onBindViewHolder(final ExampleHolder holder, int position) { 
     TextView tv = null; 
     final TextView finalTv = tv; 
     RootRef.child("Development").child("Level 2").child("Intent").child("Putextra").child("Examples") 
       .addChildEventListener(new ChildEventListener() { 
      @Override 
      public void onChildAdded(DataSnapshot dataSnapshot, String s) { 
       Log.i("isa 5air", String.valueOf(dataSnapshot.getValue())); 
       finalTv.setText(String.valueOf(dataSnapshot.getValue())); 
       holder.parentL.addView(finalTv); 
       adapter.notifyDataSetChanged(); 
      } 

      @Override 
      public void onChildChanged(DataSnapshot dataSnapshot, String s) { 

      } 

      @Override 
      public void onChildRemoved(DataSnapshot dataSnapshot) { 

      } 

      @Override 
      public void onChildMoved(DataSnapshot dataSnapshot, String s) { 

      } 

      @Override 
      public void onCancelled(FirebaseError firebaseError) { 

      } 
     }); 
    } 

    @Override 
    public int getItemCount() { 
     return 0; 
    } 
    } 

編輯 注:我已經注意到,代碼不進入onBindViewHolder因爲我把測試登錄它不會打印出

回答

0

您可以在使用您與cardview下面cardview給出只能生一個孩子,你可以使用另一個容器佈局,linear或相對,因爲你需要

<android.support.v7.widget.CardView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_margin="5dp" 
     card_view:cardCornerRadius="2dp" 
     card_view:contentPadding="10dp"> 

     <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 

       ............some other views..... 

     </LinearLayout> 

</android.support.v7.widget.CardView> 
+0

但我想建立一個回收視圖來添加更多的一個很好prephomance –

0

在你的代碼,它似乎要傳遞空單RecyclerView Adapter

List<ViewExample> exampleList = new ArrayList<>(); 
Log.i("dddddd", String.valueOf(exampleList)); 
//alos the list just show [] in the Logs 
adapter = new ExampleAdapter(this, exampleList); 

更改建議:

1.You可以傳遞給適配器之前在其中加入一些元素,或添加元素列表中,然後調用

adapter.notifyDataSetChanged(); 

2.In您ExampleAdapter變化getItemCount()方法與波紋管

@Override 
public int getItemCount() { 
    return exampleList.size(); 
} 

編輯:

3.Change您onCreateViewHolder()

@Override 
public ExampleHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     ExampleCV = new CardView(ExampleCV.getContext()); 
     ExampleCV.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT)); 
    return new ExampleHolder(ExampleCV); 
} 

ExampleHolder與4.Constructor:

public ExampleHolder(View itemView) { 
     super(itemView); 
     parentL = new LinearLayout(parentL.getContext()); 

     parentL.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT)); 
     parentL.setOrientation(LinearLayout.VERTICAL); 
     itemView.addView(parentL); 
    } 
+0

謝謝你的解釋,但它不窩rk顯示數據也將我試圖把notifyDataSetChanged放在適配器之前的onCreate中,並且從Firebase獲取數據後不工作,這是錯誤的地方嗎?或者我以錯誤的方式調用CardView? –

+0

你改變了'getItemCount()'方法 –

+0

是的,我也注意到它沒有輸入onBindViewHolder方法 –