下面是解決方案,通過一個國旗給你Adapter
描述當前的方向。
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
//Update the Flag here
orientationLand = (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE ? true : false);
}
在適配器類:
@Override
public CalenderSessionHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
//User Flag here to change layou
View itemView = LayoutInflater.from(parent.getContext()).inflate(orientationLand ? R.layout.item_calender_session_land : R.layout.item_calender_session_port, null);
return new CalenderSessionHolder(itemView);
}
確保您處理視圖ID的正確,以避免例外。
更新:
如果你有兩個View持有人:
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
if (orientationLand)
{
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_calender_session_land , parent, false);
return new LandViewHolder(v);
}
else
{
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_calender_session_port, parent, false);
return new PortViewHolder(v);
}
return null;
}
在綁定ViewHolder
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
if (holder instanceof PortViewHolder)
{
PortViewHolder portHolder = (PortViewHolder) holder;
//Initialize Views here for Port View
} else if (holder instanceof LandViewHolder)
{
LandViewHolder landViewHolder = (LandViewHolder) holder;
//Initialize Views here for Land View
}
}
你找到一個解決辦法? – Sufian
@Sufian是的,請在下面查看我的答案。 – Akshay