我正在学习Android应用程序开发,但是我陷入了僵局。我的RecyclerView正在拾取我的String数组中有多少项,但它没有在text1中显示实际数据,任何帮助都会非常感激。
这是我的适配器
public class EventCalenderAdapter extends RecyclerView.Adapter<EventCalenderAdapter.ViewHolder> {
static String[] fakeData = new String[] {
"One","Two","Three","Four","Five","Ah..Ah..Ah"
};
static class ViewHolder extends RecyclerView.ViewHolder {
CardView cardView;
TextView titleView;
public ViewHolder(CardView card) {
super(card);
cardView = card;
titleView = (TextView) card.findViewById(R.id.text1);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
CardView v = (CardView) LayoutInflater.from(parent.getContext()).inflate(R.layout.event_task, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
viewHolder.titleView.setText(fakeData[i]);
}
@Override
public int getItemCount() {
return fakeData.length;
}
这是相应的XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:event_view="http://schemas.android.com/apk/res-auto"
android:layout_height="100dp"
android:layout_width="match_parent"
android:id="@+id/event_view"
android:layout_marginTop="@dimen/task_card_half_spacing"
android:layout_marginBottom="@dimen/task_card_half_spacing"
android:layout_marginLeft="@dimen/gutter"
android:layout_marginRight="@dimen/gutter"
android:layout_gravity="center"
android:elevation="@dimen/task_card_elevation"
android:foreground="?android:attr/selectableItemBackground"
event_view:cardCornerRadius="0dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/image"
android:scaleType="centerCrop"
android:layout_alignParentLeft="true"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@android:style/TextAppearance.Medium.Inverse"
android:id="@+id/text1"
android:maxLines="1"
android:ellipsize="end"
android:padding="10dp"
android:layout_alignTop="@+id/image"
android:layout_toRightOf="@+id/image"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
style="@android:style/TextAppearance.Inverse"
android:id="@+id/text2"
android:maxLines="3"
android:ellipsize="end"
android:padding="10dp"
android:layout_alignStart="@+id/text1"
android:layout_below="@+id/text1"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
这是我的片段
public class EventCalenderFragment extends Fragment {
RecyclerView recyclerView;
EventCalenderAdapter adapter;
public EventCalenderFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adapter = new EventCalenderAdapter();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.fragment_event_calender, container, false);
recyclerView = (RecyclerView) v.findViewById(R.id.recycler);
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return v;
}
答案 0 :(得分:2)
嗯......一切看起来都不错,你可以试试这个吗:
android:textColor="#000000"