需求總是在變睡汹,你的代碼千萬(wàn)別寫死?
(就當(dāng)做是工作不飽和吧拨脉,每天上午將bug fix 完之后,從下午就開始干坐到晚上9點(diǎn)下班。)閑來(lái)無(wú)事浮庐,學(xué)習(xí)些新姿勢(shì)和一些在項(xiàng)目中遇到的問(wèn)題硼砰。
今天主要是記錄一下在使用RecyclerView和ScrollView 的時(shí)候菲驴,出現(xiàn)的幾個(gè)問(wèn)題凄杯。
<pre>
1.RecyclerView 顯示不全
2.RecyclerView 與ScrollView 滑動(dòng)沖突
3.嵌套布局不顯示在頂部,直接顯示RecyclerView 第一個(gè)item
</pre>
Solution - 1
先說(shuō)第一個(gè)問(wèn)題是怎么解決的菠秒,自從RecyclerView加入布局管理器之后似踱,可以通過(guò)布局管理器去設(shè)置上顯示風(fēng)格。本文中用到的是垂直列表的布局方式所以用到的布局管理器為L(zhǎng)inearLayoutManager稽煤。重寫LinearLayoutManager(網(wǎng)上很多這種重寫demo)
<code>
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;import android.view.ViewGroup;
public class AutoLinearLayoutManager extends LinearLayoutManager {
private static final String TAG = AutoLinearLayoutManager.class.getSimpleName(); public AutoLinearLayoutManager(Context context) {
super(context);
}
public AutoLinearLayoutManager(Context context, int orientation,
boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
private int[] mMeasuredDimension = new int[2];
@Override
public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {
final int widthMode = View.MeasureSpec.getMode(widthSpec);
final int heightMode = View.MeasureSpec.getMode(heightSpec);
final int widthSize = View.MeasureSpec.getSize(widthSpec);
final int heightSize = View.MeasureSpec.getSize(heightSpec);
int width = 0;
int height = 0;
for (int i = 0; i < getItemCount(); i++) {
measureScrapChild(recycler, i,
View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), mMeasuredDimension);
if (getOrientation() == HORIZONTAL) {
width = width + mMeasuredDimension[0];
if (i == 0) {
height = mMeasuredDimension[1];
}
} else {
height = height + mMeasuredDimension[1];
if (i == 0) {
width = mMeasuredDimension[0];
}
}
}
switch (widthMode) {
case View.MeasureSpec.EXACTLY:
width = widthSize;
case View.MeasureSpec.AT_MOST:
case View.MeasureSpec.UNSPECIFIED:
}
switch (heightMode) {
case View.MeasureSpec.EXACTLY:
height = heightSize;
case View.MeasureSpec.AT_MOST:
case View.MeasureSpec.UNSPECIFIED:
}
setMeasuredDimension(width, height);
}
private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec, int heightSpec, int[] measuredDimension)
{
try {
View view = recycler.getViewForPosition(0);
if (view != null) {
RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec, getPaddingLeft() + getPaddingRight(), p.width);
int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec, getPaddingTop() + getPaddingBottom(), p.height); view.measure(childWidthSpec, childHeightSpec);
measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin; measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin;
recycler.recycleView(view);
}
} catch (Exception e)
{
e.printStackTrace();
} finally {
}
}
}
</code>
重寫代碼排版不是很好核芽,自己復(fù)制下來(lái),在AS 中格式化一下吧酵熙。
同時(shí)ScrollView 布局設(shè)置屬性
<code>android:fillViewport="true"</code>
Solution - 2
關(guān)于滑動(dòng)沖突的話轧简,一般這種情況下,都是按照ScrollView 去滑動(dòng)即可匾二。
設(shè)置代碼
<code>
recyclerView.setNestedScrollingEnabled(false);
</code>
Solution - 3
在代碼中設(shè)置代碼
ScrollView sv_container;
<code>
sv_container.smoothScrollTo(0,0);
</code>
即可解決哮独。
好了拳芙,記錄一下。不然每次皮璧,都得重新開始來(lái)過(guò)一遍
2017年01月03日16:31:01
我又來(lái)了舟扎,最好的方法就是用type 的方式去獲取不同的item。這種就不會(huì)出現(xiàn)上面嵌套的問(wèn)題悴务。
最新更新
當(dāng)item 中有wrap_content 的睹限,不能用nestscrollview 不然沒(méi)辦法讓最后一個(gè)item 顯示完整,所以建議全部用viewtype 去做讯檐。