RecyclerView仅显示动态列表的第一项。

时间:2016-10-19 01:22:47

标签: android xml listview android-recyclerview

我有一个名字的动态列表。但是,RecyclerView仅显示列表的第一项。可以做些什么来解决这个问题?

RecyclerView

routes_recyclerview.xml包括:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".ui.activities.RoutesAssignActivity"
    tools:showIn="@layout/activity_routes_assign"
    android:layout_marginStart="10dp"
    android:layout_marginEnd="10dp">


<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical"
    android:layout_marginTop="10dp"
/>
</RelativeLayout>

用于显示列表名称,

routes_recycler_listview.xml包括:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:gravity="center_vertical"
        android:paddingEnd="0dp"
        android:paddingStart="10dp"
        android:textColor="@color/md_black_1000"
        android:textSize="20sp" />
</RelativeLayout>

我包含列表的标题。所以,对于标题,我有

view_header.xml包括:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp"
    android:background="#001F3F"
    android:textSize="28sp"
    android:textStyle="bold"
    android:textColor="@android:color/white"
    tools:context=".ui.activities.RoutesAssignActivity"
    />

路由类的getter and setter方法:

Routes.jave包括:

public class Routes{
   @SerializedName("assignDate")
    private String assignDate;
    @SerializedName("routeName")
    private String routeName;

    public String  getAssignDate() {
        return assignDate;
    }
    public void setAssignDate(String assignDate) {
        this.assignDate = assignDate;
    }

    public String getRouteName() {
        return routeName;
    }

    public void setRouteName(String routeName) {
        this.routeName = routeName;
    }
}

要列出总路线,我有RoutesList.java。这里,

public class RoutesList implements Comparable<RoutesList>{

    @SerializedName("date")
    private String date;
    @SerializedName("routes")
    private List<Routes> routes = new ArrayList<>();

    public String getDateString() {
        return date;
    }

    public Date getDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
        try {
            return format.parse(getDateString());
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public List<Routes> getRoutes() {
        return routes;
    }

    public void setRoutes(List<Routes> routes) {
        this.routes = routes;
    }

    @Override
    public int compareTo(@NonNull RoutesList another) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
        try {
            Date date = format.parse(another.getDateString());
            return date.compareTo(getDate());
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return 0;
    }
}

在我的RecyclerViewRecyclerAdapter.java中,我有: 在ViewHolder我使用:

public TextView textView;

        public ViewHolder(View v) {
            super(v);
            textView = (TextView) v.findViewById(R.id.textView);
        }

onCreateViewHolder方法,我使用:

public ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
        this.context = parent.getContext();
        return new ViewHolder(LayoutInflater.from(context)
                .inflate(R.layout.routes_recycler_listview, parent, false));
    }

onBindViewHolder方法,我使用:

public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.textView.setText(item.getRoutes().get(position).getRouteName());

    }

RoutesAssignActivity.java班级,我使用:

recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this).build());
        adapter = new StickyHeaderListView(routes,this);
        final StickyRecyclerHeadersDecoration headersDecor = new StickyRecyclerHeadersDecoration(adapter);
        recyclerView.addItemDecoration(headersDecor);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);

毕竟,我只能得到这个:

enter image description here

如何在列表中获取所有名称?

0 个答案:

没有答案