RecyclerView滚动和项目缺失问题

时间:2017-03-14 13:34:58

标签: android android-recyclerview

您好我已经实现了多个RecyclerView,一切都可以使用API​​ 19,我可以看到时间(288个元素 - 滚动很长)和所有频道名称。 see the picture

API 25出现问题,我看不到垂直(黑色)RecyclerView中的元素,但滚动表示存在元素。此外,水平(绿色)RecyclerView显示的元素很少,而不是 288 。我不知道为什么会这样,我已经检查了四次代码,一切都很好。

I am not sure what is wrong in my code. Can you help me to find the problem?

enter image description here

enter image description here

水平RCV适配器

public class EpgTimeRecyclerViewAdapter extends RecyclerView.Adapter<EpgTimeRecyclerViewAdapter.TimeViewHolder> {


    private static final Long THREE_DAY_IN_MILLIS = 259200000l;
    private static final Long HALF_AN_HOUR_IN_MILLIS = 1800000l;
    private List<Long> time = new ArrayList<Long>();
    private String TIME_RCV_ADAPTER_TAG="TIME RCV ADAPTER" ;

    public EpgTimeRecyclerViewAdapter() {

        generateTimeForThreeDaysInBothDirection();
    }

    private void generateTimeForThreeDaysInBothDirection() {
        Date currentTime = new Date();
        Long currentTimeMillisec = currentTime.getTime();
        Long minusThreeDays = currentTimeMillisec - THREE_DAY_IN_MILLIS;
        while (minusThreeDays < currentTimeMillisec + THREE_DAY_IN_MILLIS) {
            time.add(minusThreeDays);
            minusThreeDays += HALF_AN_HOUR_IN_MILLIS;
        }
        Log.i(TIME_RCV_ADAPTER_TAG,time.size()+"");
    }

    @Override
    public TimeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.epg_time_view, parent, false);

        return new TimeViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(TimeViewHolder holder, int position) {
        Long timeItem = time.get(position);

        String time = convertTimeToHoursAndMinutes(timeItem);

        holder.time.setText(time);

    }

    private String convertTimeToHoursAndMinutes(Long millis) {
        long seconds = millis/1000;
        long s = seconds % 60;
        long m = (seconds / 60) % 60;
        long h = (seconds / (60 * 60)) % 24;
        return String.format("%d:%02d", h,m);

    }

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

    public class TimeViewHolder extends RecyclerView.ViewHolder {

        private TextView time;

        public TimeViewHolder(View itemView) {
            super(itemView);
            time = (TextView) itemView.findViewById(R.id.epg_time);
        }
    }
}

垂直RCV适配器

public class EpgChannelAdapter extends RecyclerView.Adapter<EpgChannelAdapter.ChannelViewHolder> {

    private List<WebTvChannel> channelList;

    public EpgChannelAdapter(List<WebTvChannel> channelList) {
        this.channelList = channelList;

    }


    @Override
    public ChannelViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.epg_channel_view, parent, false);

        return new ChannelViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ChannelViewHolder holder, int position) {
        WebTvChannel channel = channelList.get(position);
        holder.channelName.setText(channel.getName());

    }

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

    public class ChannelViewHolder extends RecyclerView.ViewHolder {

        private ImageView icon;
        private TextView channelName;

        public ChannelViewHolder(View itemView) {
            super(itemView);
            icon = (ImageView) itemView.findViewById(R.id.channelImageView);
            channelName = (TextView) itemView.findViewById(R.id.channelTextView);
        }
    }

}

活动

public class EpgActivity extends Activity {

    private RecyclerView channelsRecyclerView, epgRecyclerView, timeRecyclerView;
    private TextView time;
    private Realm realm;
    private static final String EPG_ACTIVITY_TAG="EPG ACTIVITY";
    public Map<Integer, String> day;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_epg);

        day = new HashMap<>();
        day.put(0,"SUNDAY");
        day.put(1,"MONDAY");
        day.put(2,"TUESDAY");
        day.put(3,"WEDNESDAY");
        day.put(4,"THURSDAY");
        day.put(5,"FRIDAY");
        day.put(6,"SATURDAY");


        channelsRecyclerView = (RecyclerView) findViewById(R.id.rcv_channel);
        epgRecyclerView = (RecyclerView) findViewById(R.id.rcv_epg);
        timeRecyclerView = (RecyclerView) findViewById(R.id.rcv_time);
        time = (TextView) findViewById(R.id.tv_change_date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());

        int dayInWeek = calendar.get(Calendar.DAY_OF_WEEK);

        time.setText(day.get(dayInWeek));

        realm = Realm.getDefaultInstance();

        List<WebTvChannel> channelList = new ArrayList<WebTvChannel>();

        channelList = realm.where(WebTvChannel.class).findAll();
        Log.i(EPG_ACTIVITY_TAG,channelList.size()+"");
        channelsRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        channelsRecyclerView.setItemAnimator(new DefaultItemAnimator());
        RecyclerView.Adapter adapter = new EpgChannelAdapter(channelList);

        channelsRecyclerView.setAdapter(adapter);

        LinearLayoutManager layoutManagerForTimeBar
                = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);

        timeRecyclerView.setLayoutManager(layoutManagerForTimeBar);

        timeRecyclerView.setItemAnimator(new DefaultItemAnimator());
        timeRecyclerView.setAdapter(new EpgTimeRecyclerViewAdapter());

        //timeRecyclerView.smoothScrollToPosition(144);

    }

    @Override
    protected void onStart() {
        super.onStart();
           }
}

XML

<?xml version="1.0" encoding="utf-8"?>
<!--Outer container layout-->
<LinearLayout
    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"
    android:orientation="horizontal"
    tools:context=".view.EpgActivity">

    <!--To display Channels list-->
    <LinearLayout
        android:layout_width="150dp"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <!--Position (0,0)-->
        <TextView
            android:text="TIME"
            android:paddingTop="15dp"
            android:paddingLeft="25dp"
            android:id="@+id/tv_change_date"
            android:layout_width="match_parent"
            android:layout_height="50dp" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rcv_channel"
            android:scrollbars="vertical"
            android:layout_width="150dp"
            android:layout_height="match_parent"/>

    </LinearLayout>


    <!--To display Time and Programs list-->
    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

            <!--Time horizontal list-->
            <android.support.v7.widget.RecyclerView
                android:id="@+id/rcv_time"
                android:background="#658918"
                android:scrollbars="horizontal"
                android:layout_width="match_parent"
                android:layout_height="50dp"/>

            <!--Vertical list whose each elements are horizontal list to show programs-->
            <android.support.v7.widget.RecyclerView
                android:background="#4286f4"
                android:id="@+id/rcv_epg"
                android:scrollbars="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
        </LinearLayout>

    </HorizontalScrollView>
</LinearLayout>

0 个答案:

没有答案