在android

时间:2017-01-03 19:52:27

标签: android

我想制定一年中的日期时间表。 RecyclerView对于其循环视图的功能非常有用。我见过的项目有无穷无尽的列表,但所有项目都有一个开头。是否可以使用RecycleView或其他组件来创建没有开头或结尾的列表?

3 个答案:

答案 0 :(得分:1)

您可以附加onScrollListener,然后根据dy

的值获取并显示更多项目
private boolean loading = true;
int pastVisiblesItems, visibleItemCount, totalItemCount;

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() 
{
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) 
    {
        if(dy > 0) //check for scroll down and do the same for scroll up
        {
            visibleItemCount = mLayoutManager.getChildCount();
            totalItemCount = mLayoutManager.getItemCount();
            pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();

            if (loading) 
            {
                if ( (visibleItemCount + pastVisiblesItems) >= totalItemCount) 
                {
                    loading = false;
                    Log.v("...", "Last Item Wow !");
                    //Do pagination.. i.e. fetch new data
                }
            }
        }
    }
});

答案 1 :(得分:0)

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        totalItemCount = linearLayoutManager.getItemCount();
        lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
        if (!loading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
            if (onLoadMoreListener != null) {
               onLoadMoreListener.onLoadMore();
            }
            loading = true;
            if (onLoadMoreListenerLeft != null) {
                 onLoadMoreListenerLeft.onLoadMoreLeft();
            }
            loading = true;
     }
        });

答案 2 :(得分:0)

要显示TimeLine视图,我使用下面的库。首先将此库添加到您的项目中。我提到了https://github.com/vipulasri/Timeline-View

    compile 'com.github.vipulasri:timelineview:1.0.5'

<强> MainActivity.java

public class MainActivity extends AppCompatActivity {

RecyclerView recyclerView;
String[] name = {"Event 1", "Event 2", "Event 3"};
String[] status = {"active", "inactive", "inactive"};
String[] description = {"Description 1","Description 2","Description 3"};
String[] time = {"11:00 PM", "10:03 AM", "10:03 PM"};

List<TimeLineModel> timeLineModelList;
TimeLineModel[] timeLineModel;
Context context;
LinearLayoutManager linearLayoutManager;

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

    timeLineModelList = new ArrayList<>();
    int size = name.length;
    timeLineModel = new TimeLineModel[size];
    context = MainActivity.this;
    linearLayoutManager = new LinearLayoutManager(this);

    for (int i = 0; i < size; i++) {
        timeLineModel[i] = new TimeLineModel();
        timeLineModel[i].setName(name[i]);
        timeLineModel[i].setStatus(status[i]);
        timeLineModel[i].setDescription(description[i]);
        timeLineModel[i].setTime(time[i]);
        timeLineModelList.add(timeLineModel[i]);
    }

    recyclerView = (RecyclerView) findViewById(R.id.activity_main_recycler_view);
    recyclerView.setLayoutManager(linearLayoutManager);
    DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), linearLayoutManager.getOrientation());
    recyclerView.addItemDecoration(dividerItemDecoration);  //for divider

    recyclerView.setAdapter(new TimeLineAdapter(context, timeLineModelList));
}}

<强> TimeLineModel.java

class TimeLineModel {
private String name;
private String status;
private String description;

String getTime() {
    return time;
}

void setTime(String time) {
    this.time = time;
}

private String time;

String getDescription() {
    return description;
}

void setDescription(String description) {
    this.description = description;
}

String getName() {
    return name;
}

void setName(String name) {
    this.name = name;
}

String getStatus() {
    return status;
}

void setStatus(String status) {
    this.status = status;
}}

<强> TimeLineAdapter.java

class TimeLineAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private List<TimeLineModel> timeLineModelList;
private Context context;

TimeLineAdapter(Context context, List<TimeLineModel> timeLineModelList) {
    this.timeLineModelList = timeLineModelList;
    this.context = context;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_timeline_layout, parent, false);
    return new ViewHolder(view, viewType);
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    ((ViewHolder) holder).textView.setText(timeLineModelList.get(position).getName());
    ((ViewHolder) holder).textViewDescription.setText(timeLineModelList.get(position).getDescription());
    ((ViewHolder)holder).textViewTime.setText(timeLineModelList.get(position).getTime());

    if (timeLineModelList.get(position).getStatus().equals("inactive"))
        ((ViewHolder) holder).timelineView.setMarker(context.getDrawable(ic_remove_circle_outline_black_24dp));
    else
        ((ViewHolder) holder).timelineView.setMarker(context.getDrawable(ic_check_circle_black_24dp));
}

@Override
public int getItemViewType(int position) {
    return TimelineView.getTimeLineViewType(position, getItemCount());
}

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

private class ViewHolder extends RecyclerView.ViewHolder {

    TimelineView timelineView;
    TextView textView, textViewDescription, textViewTime;

    ViewHolder(View itemView, int viewType) {
        super(itemView);
        timelineView = itemView.findViewById(R.id.row_timeline_layout_time_marker);
        textView = itemView.findViewById(R.id.row_timeline_layout_text_view_name);
        textViewDescription = itemView.findViewById(R.id.row_timeline_layout_text_view_description);
        textViewTime = itemView.findViewById(R.id.row_timeline_layout_text_view_time);

        timelineView.initLine(viewType);
    }
}}

在我的代码中,我创建了一个包含数据的模型类,根据列表大小的时间线,数据将在recyclerview中绑定,为此引用 TimeLineAdapter.java row_timeline_layout 是行xml文件,此布局在recyclerview中绑定。

您还可以为静态数据创建时间轴,并将其绑定到recyclerview。