我正在尝试使用四种视图类型制作recyclerview,每个视图类型将代表某些数据,我的应用程序用于英超联赛(足球),因此我对目标,黄牌和红牌具有视图类型, 每个字符串都有进球的时间,或得到黄牌和红牌的时间,我正在尝试运用一些逻辑,但对我来说没用
首先:这是我要实现的目标 Image that shows how goals , and cards are ordered by the time they happened
这是我尝试实现的代码:
//Splitting Strings
String[] homeg = hometiming.split(";");
String[] homeyello = homeyellow.split(";");
String[] homere = homered.split(";");
for (String h : homeg) {
if (!h.equals("")) {
int subgoals = Integer.parseInt(h.substring(0, 1));
for (String y : homeyello) {
if (!y.equals("")) {
int subYellow = Integer.parseInt(y.substring(0, 1));
for (String n : homere) {
if (!n.equals("")) {
int subRed = Integer.parseInt(n.substring(0, 1));
/// if goals time is smaller than time of yellow and rec card , show
/// goals
if (subgoals < subYellow && subgoals < subRed) {
GoalsItem goalsItem = new GoalsItem();
goalsItem.setHomegoals("goals :" + h + "\n");
consolidatedlist.add(goalsItem);
}
/// if yellow cards time is smaller than time of goals and red cards ,
/// show yellow cards
else if (subYellow < subgoals && subYellow < subRed) {
YellowItems yellowItems = new YellowItems();
yellowItems.setHomeyellow("yellow :" + y + "\n");
consolidatedlist.add(yellowItems);
}
/// if red card time is smaller than time of goals and yellow cards ,
/// show red cards
else if (subRed < subgoals && subRed < subYellow) {
RedItems redItems = new RedItems();
redItems.setHomered("red : " + n + "\n");
consolidatedlist.add(redItems);
} } } } } } }
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
switch (holder.getItemViewType()) {
case TypeItem.TYPE_PHOTO:
PhotoItem photoItem = (PhotoItem) list.get(position);
ViewPhoto viewphoto = (ViewPhoto) holder;
viewphoto.date.setText("" + photoItem.getDate());
Picasso.with(context).load(photoItem.getPhoto()).fit().into(((ViewPhoto) holder).photo);
break;
case TypeItem.TYPE_Goals:
GoalsItem goalsItem = (GoalsItem) list.get(position);
ViewGoals viewgoals = (ViewGoals) holder;
viewgoals.homegoals.setText("" + goalsItem.getHomegoals());
viewgoals.awaygoals.setText("" + goalsItem.getAwaygoals());
break;
case TypeItem.TYPE_Yellow:
YellowItems yellowItems = (YellowItems) list.get(position);
ViewYellow viewyellow = (ViewYellow) holder;
viewyellow.homeyellow.setText("" + yellowItems.getHomeyellow());
viewyellow.awayyellow.setText("" + yellowItems.getAwayyellow());
break;
case TypeItem.TYPE_RED:
RedItems redItems = (RedItems) list.get(position);
ViewRed viewred = (ViewRed) holder;
viewred.homered.setText("" + redItems.getHomered());
viewred.awayred.setText("" +redItems.getAwayred());
break;
}
}
"strHomeGoalDetails":"",
"strHomeRedCards":"",
"strHomeYellowCards":"13':Luka Milivojevic;",
"strAwayRedCards":"",
"strAwayYellowCards":"47':Raheem Sterling;",
"strAwayGoalDetails":"39':Gabriel Jesus;41':David Silva;",
答案 0 :(得分:1)
您必须override getItemViewType(position: Int)
,而不是viewType
中的onCreatecreate
返回不同的ViewHolders
(如果需要),并且比onBindViewHolder
中的绑定您的类型
例如,在您的适配器类中,使用typeField: String
和key: Int
private val keyMap = HashMap<String, Int>().apply {
put("FIRST_TYPE", 1)
put("SECOND_TYPE", 2)
put("THIRD_TYPE", 3)
}
第二步是覆盖getItemViewType(position: Int)
override fun getItemViewType(position: Int): Int {
return keyMap.get(yourItemList(position).getType())
第三步-override onCreateViewHolder
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ResourceViewHolder {
return when (viewType) {
keyMap.get("FIRST_TYPE") -> //create specific ViewHolder
keyMap.get("SECOND_TYPE") -> //create specific ViewHolder
//etc
}
最后一部分是绑定onBindViewHolder
您可以执行与onCreateViewHolder