我需要什么:
我尝试使用RecyclerView
这样做,根据模型动态设置项目的高度。我正在使用
布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="vertical"
android:gravity="center|bottom"
android:layout_weight="0">
<TextView
android:id="@+id/teacher"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:padding="5dp"
android:textColor="@android:color/black"
android:textSize="16sp" />
<TextView
android:id="@+id/room"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/teacher"
android:padding="5dp"
android:textColor="@android:color/black"
android:textSize="16sp" />
<TextView
android:id="@+id/subject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#1976D2"
android:gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:textColor="#ffffff"
android:textSize="13sp"
android:maxLines="3"
android:lines="1" />
</RelativeLayout>
</android.support.v7.widget.CardView>
适配器代码:
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SampleViewHolders {
val layoutView = LayoutInflater.from(parent.context).inflate(
R.layout.rv_grid, null)
layoutView.getViewTreeObserver().addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
override fun onPreDraw(): Boolean {
val lp = layoutView.getLayoutParams()
if (lp is StaggeredGridLayoutManager.LayoutParams) {
val sglp = lp as StaggeredGridLayoutManager.LayoutParams
when (viewType) {
1 -> sglp.isFullSpan = false
2 -> {
sglp.height = layoutView.height * 2;
layoutView.refreshDrawableState();
}
3 -> sglp.height = layoutView.height * 3;
}
layoutView.setLayoutParams(sglp)
val lm = (parent as RecyclerView).layoutManager as StaggeredGridLayoutManager
lm.invalidateSpanAssignments()
}
layoutView.getViewTreeObserver().removeOnPreDrawListener(this)
return true
}
})
return ViewHolder(layoutView)
}
它会与会议所需的小时数相乘。
问题是,如果在2小时的会议后有一小时的会议,那么会转移到第二天。
请参阅:
2小时会议:
会议1小时:
如何在适配器中修复此问题/如何在不移动元素的情况下调整高度?
答案 0 :(得分:0)
将库导入项目。 通过maven抓住
<dependency>
<groupId>com.github.alamkanak</groupId>
<artifactId>android-week-view</artifactId>
<version>1.2.6</version>
<type>aar</type>
</dependency>
通过gradle抓住
compile 'com.github.alamkanak:android-week-view:1.2.6'
在xml布局中添加WeekView。
<com.alamkanak.weekview.WeekView
android:id="@+id/weekView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:eventTextColor="@android:color/white"
app:textSize="12sp"
app:hourHeight="60dp"
app:headerColumnPadding="8dp"
app:headerColumnTextColor="#8f000000"
app:headerRowPadding="12dp"
app:columnGap="8dp"
app:noOfVisibleDays="3"
app:headerRowBackgroundColor="#ffefefef"
app:dayBackgroundColor="#05000000"
app:todayBackgroundColor="#1848adff"
app:headerColumnBackground="#ffffffff"/>
在java文件中写下以下代码。
/
/ Get a reference for the week view in the layout.
mWeekView = (WeekView) findViewById(R.id.weekView);
// Set an action when any event is clicked.
mWeekView.setOnEventClickListener(mEventClickListener);
// The week view has infinite scrolling horizontally. We have to provide the events of a
// month every time the month changes on the week view.
mWeekView.setMonthChangeListener(mMonthChangeListener);
// Set long press listener for events.
mWeekView.setEventLongPressListener(mEventLongPressListener);
根据需要实施WeekView.MonthChangeListener,WeekView.EventClickListener,WeekView.EventLongPressListener。
在WeekView.MonthChangeListener.onMonthChange()回调中提供WeekView的事件。请记住,日历会预先加载连续三个月的事件,以启用无滞后滚动。
MonthLoader.MonthChangeListener mMonthChangeListener = new MonthLoader.MonthChangeListener() {
@Override
public List<WeekViewEvent> onMonthChange(int newYear, int newMonth) {
// Populate the week view with some events.
List<WeekViewEvent> events = getEvents(newYear, newMonth);
return events;
}
};