我曾经添加了viewtype方式为RecycleView添加一个标题,但是接口会直接跳过标题刷新,但是如果拒绝,标题仍会显示,但刷新会跳过这一行,这是由它
这是适配器
中onCreateViewHolder()方法的代码@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_HEADER) {
View headerView = LayoutInflater.from(parent.getContext()).inflate(R.layout.header_online, parent, false);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
headerView.setLayoutParams(layoutParams);
return new OnlineHeaderHolder(headerView, context);
} else if (viewType == TYPE_STAR) {
View starView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_online_star, parent, false);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
starView.setLayoutParams(layoutParams);
return new OnlineStarViewHolder(starView, context);
} else {
View normalView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_online, parent, false);
return new OnlineListHolder(normalView, context);
}
}
这是标题布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="@dimen/xx"
android:background="@color/colorPrimary">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/dimens_xh_15dp"
android:layout_marginStart="@dimen/dimens_xh_15dp"
android:text="@string/latest_active"
android:textColor="@color/xx"
android:textSize="17sp" />
<ImageView
android:id="@+id/xx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="@dimen/xx"
android:layout_marginRight="@dimen/xx"
android:layout_toLeftOf="@+id/xx"
android:layout_toStartOf="@+id/xx"
android:src="@mipmap/xx" />
<ImageView
android:id="@+id/xx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginEnd="xx"
android:layout_marginRight="xx"
android:src="@mipmap/xx" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/xx" />
</LinearLayout>
这里用xx删除了一些无关的信息
这是我的onBindViewHolder()方法 我使用的模型包含所有数据,有一个&#34;类型&#34;模型中的属性区分不同类型
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
OnlineNewAdaper.OnlineModel onlineModel = modelList.get(position);
switch (onlineModel.getType()) {
case TYPE_HEADER:
((OnlineHeaderHolder) holder).setData(onlineModel);
((OnlineHeaderHolder) holder).setOnlineSelectListener(listener);
break;
case TYPE_STAR:
((OnlineStarViewHolder) holder).setData(onlineModel);
break;
case TYPE_NORMAL:
((OnlineListHolder) holder).setData(onlineModel, mServerTime);
break;
default:
break;
}
}
这是OnlineHeaderHolder代码:
public class OnlineHeaderHolder extends RecyclerView.ViewHolder {
public OnlineHeaderHolder(View itemView, Context context) {
super(itemView);
this.context = context;
ViewHelper.inject((ViewGroup) itemView, this);
}
public void setData(OnlineNewAdaper.OnlineModel data) {
if (data.getType() != TYPE_HEADER) {
return;
}
}
}
答案 0 :(得分:1)
您可以在根容器中尝试使用以下属性。
android:descendantFocusability="blocksDescendants"
这将禁用其后代视图的可聚焦性。您还可以使用其他两个选项值:afterDescendant
&amp; beforeDescendant
。