在我原來的Activity中,我初始化一個Adapter來填充一個ListView
,並且順利進行。所有數據填充的ArrayList
都會作爲參數傳遞給Adapter,並且一切順利。ListView裏面的ListView只顯示一個條目
現在在該適配器中,或者那個被填充的ListView
,還有另一個ListView
和另一個適配器。
同樣,我遵循相同的程序,並用ArrayList
填充適配器,但似乎只有第一個條目顯示在嵌套ListView
中。我認爲這是因爲家長ListView
得到填充,並且兒童ListView
的數據僅填充一個父母條目。
總之,這裏的適配器初始化和適配器類:
PlayerSeasonsStatsAdapter pssAdapter = new PlayerSeasonsStatsAdapter(getContext(), alPlayerSeasonsStats);
vh.lvPlayerSeasonsStats.setAdapter(pssAdapter);
alPlayerSeasonsStats
是包含的數據填充到孩子ListView
一個ArrayList。
這裏的孩子適配器:
public class PlayerSeasonsStatsAdapter extends ArrayAdapter{
private ArrayList<PlayerStatsTeamModelSeasons> playerSeasons = new ArrayList<PlayerStatsTeamModelSeasons>();
private LayoutInflater mInflater;
public PlayerSeasonsStatsAdapter(Context context, ArrayList<PlayerStatsTeamModelSeasons> playerSeasons) {
super(context, 0, playerSeasons);
this.playerSeasons = playerSeasons;
mInflater = LayoutInflater.from(context);
}
@Override
public PlayerStatsTeamModelSeasons getItem(int position) {
return playerSeasons.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vh;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.player_seasons_stats_row, parent, false);
vh = new ViewHolder();
vh.tvPSSRCompetition = (TextView) convertView.findViewById(R.id.tvPSSRCompetition);
vh.tvPSSRMatchesPlayed = (TextView) convertView.findViewById(R.id.tvPSSRMatchesPlayed);
vh.tvPSSRGoalsScored = (TextView) convertView.findViewById(R.id.tvPSSRGoalsScored);
vh.tvPSSRAssists = (TextView) convertView.findViewById(R.id.tvPSSRAssists);
vh.tvPSSRYellowCards = (TextView) convertView.findViewById(R.id.tvPSSRYellowCards);
vh.tvPSSRRedCards = (TextView) convertView.findViewById(R.id.tvPSSRRedCards);
vh.ivPSSRCompetitionLogo = (ImageView) convertView.findViewById(R.id.ivPSSRCompetitionLogo);
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
PlayerStatsTeamModelSeasons pstmsModel = getItem(position);
vh.tvPSSRCompetition.setText(pstmsModel.pstmSeasonModel.name);
vh.tvPSSRMatchesPlayed.setText(pstmsModel.pstmsModel.matchesPlayed);
vh.tvPSSRGoalsScored.setText(pstmsModel.pstmsModel.goalsScored);
vh.tvPSSRAssists.setText(pstmsModel.pstmsModel.assists);
vh.tvPSSRYellowCards.setText(pstmsModel.pstmsModel.yellowCards);
int totalRedCards = Integer.parseInt(pstmsModel.pstmsModel.yellowRedCards) + Integer.parseInt(pstmsModel.pstmsModel.redCards);
vh.tvPSSRRedCards.setText(totalRedCards + "");
//loadLogoToView(vh.ivPSSRCompetitionLogo, pstmsModel.pstmSeasonModel.id, true);
return convertView;
}
public static void loadLogoToView(ImageView iv, String teamId, boolean transform) {
String image = Constant.IMAGES_ROOT +
Constant.LOGO_PATH +
teamId +
Constant.LOGO_EXTENSION;
RequestCreator img = Picasso.with(iv.getContext())
.load(image);
if (transform) img.transform(new RoundedCornersTransform(2));
img.into(iv);
}
static class ViewHolder {
ImageView ivPSSRCompetitionLogo;
TextView tvPSSRCompetition;
TextView tvPSSRMatchesPlayed;
TextView tvPSSRGoalsScored;
TextView tvPSSRAssists;
TextView tvPSSRYellowCards;
TextView tvPSSRRedCards;
}
什麼是我選擇這裏?
我會開始沒有一個'ListView'嵌套在另一個'ListView'中。使用一個具有多個行類型的'ListView',或者使用'ExpandableListView',或者使用具有某種擴展語義的'RecyclerView',或者使用具有多個項目類型的'RecyclerView',或者使用'vertical'' LinearLayout '而不是嵌套的'ListView',或者使用沿着另一個軸工作的東西(例如'HorizontalScrollView'),或者使用兩個活動/片段(頂層ListView'向下鑽取到一組單獨的細節中)ListBox等) – CommonsWare
我聽說嵌套的ListView會導致用戶界面問題,但我的設計儘可能簡單,它不會成爲問題。話雖如此,我有什麼可以在選擇全面開始之前修復現有的邏輯? – user1938007
可以看看這個:https://stackoverflow.com/questions/31458803/listview-inside-listview-only-one-element – rafsanahmad007