我正在尝试使用cardView
创建recyclerView
并以编程方式创建CardView
,这是我的代码,如果不可能请告诉我使用{{1} }
XML
主要类
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;
}
}
因为我把测试记录放入它不打印出来
答案 0 :(得分:0)
您可以使用下面lesson_layout
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>
只有一个孩子,您可以根据需要使用其他容器布局do_shortcode( 'your_short_code_here' );
或亲戚{ p>
if ( is_front_page() ) {
do_shortcode( 'your_short_code_here' );
}
答案 1 :(得分: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.您可以在传递给适配器之前添加一些元素,或者在列表中添加元素然后调用
adapter.notifyDataSetChanged();
2.在您的ExampleAdapter
更改getItemCount()
方法中使用以下
@Override
public int getItemCount() {
return exampleList.size();
}
修改强>
3.更改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);
}
4. ExampleHolder
的构造者:
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);
}