【问题标题】:ListView inside a ListItem displays only one entryListItem 内的 ListView 仅显示一个条目
【发布时间】:2017-07-04 18:54:35
【问题描述】:

在我原来的 Activity 中,我初始化了一个 Adapter 来填充一个 ListView,这很顺利。 ArrayList 填充了所有数据作为参数传递给适配器,一切顺利。

现在,在那个适配器或填充的 ListView 内部,还有另一个 ListView 和另一个适配器。

再次,我遵循相同的过程并使用ArrayList 填充适配器,但似乎只有第一个条目显示在嵌套的ListView 中。我认为这是因为父 ListView 被填充,而子 ListView 的数据仅填充一个父条目。

不管怎样,这里是适配器初始化和适配器类:

    PlayerSeasonsStatsAdapter pssAdapter = new PlayerSeasonsStatsAdapter(getContext(), alPlayerSeasonsStats);
            vh.lvPlayerSeasonsStats.setAdapter(pssAdapter);

alPlayerSeasonsStats 是一个 ArrayList,其中包含用于填充子 ListView 的数据。

这是子适配器:

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 @987654341 @ 而不是嵌套的ListView,或者使用沿另一个轴工作的东西(例如,HorizontalScrollView),或者使用两个活动/片段(顶级ListView 向下钻取到一组单独的细节与以前-嵌套ListView)等
  • 我听说嵌套的 ListViews 会导致 UI 问题,但我的设计如此简单,这不会成为问题。话虽如此,在选择重新开始之前,我能做些什么来修复现有的逻辑吗?
  • 但问题不在于它显示的ListView 仅包含一项空间,问题在于用于通过适配器填充ListViewArrayList 仅包含一个条目。
  • 那么您的问题在于您如何填充alPlayerSeasonsStats,如果它的size() 为1。

标签: java android listview android-arrayadapter


【解决方案1】:

你的适配器应该是这样的

public class PlayerSeasonsStatsAdapter extends ArrayAdapter<PlayerStatsTeamModelSeasons> {

private  class ViewHolder {
    TextView tvPSSRCompetition;
}

public PlayerSeasonsStatsAdapter(Context context, List<PlayerStatsTeamModelSeasons> balanceModels) {

    super(context, R.layout.row_account_balance, balanceModels);

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

    PlayerStatsTeamModelSeasons model = getItem(position);

    PlayerSeasonsStatsAdapter.ViewHolder viewHolder;
    if (convertView == null) {

        viewHolder = new PlayerSeasonsStatsAdapter.ViewHolder();

        LayoutInflater inflater = LayoutInflater.from(getContext());

        convertView = inflater.inflate(R.layout.row_account_balance, parent, false);

        viewHolder.tvPSSRCompetition = (TextView) convertView.findViewById(R.id.tvPSSRCompetition);
        convertView.setTag(viewHolder);

    } else {

        viewHolder = (PlayerSeasonsStatsAdapter.ViewHolder) convertView.getTag();
    }
    viewHolder.tvPSSRCompetition.setText(model.getPSSRCompetition());


    if (position%2== 0) {
        convertView.setBackgroundColor(Color.parseColor("#F5EEE5"));
    }else{

        convertView.setBackgroundColor(Color.parseColor("#FFFFFF"));
    }

    return convertView;

   }

  }

像下面这样打电话就行了

   PlayerSeasonsStatsAdapter adapter = new PlayerSeasonsStatsAdapter(MainActivity.this, balanceList);
   listView.setAdapter(adapter);

Listview 如下所示

        <ListView
         android:id="@+id/listView"
          android:layout_width="match_parent"
           android:layout_height="match_parent" />
        </LinearLayout>

这里是 row_account_balance 布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"  >

    <TextView
        android:id="@+id/tvPSSRCompetition"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center|center_vertical|center_horizontal"
        android:text="@string/agent_statement_sl"
        android:textStyle="bold"
        android:paddingLeft="2dp"
        android:textSize="16sp"
        android:paddingBottom="5dp"
        android:paddingTop="5dp"/>   
</LinearLayout>

【讨论】:

  • 您在哪里进行了更改?也许添加一些解释它将如何解决问题将有助于操作..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-07
  • 1970-01-01
  • 2015-10-04
  • 1970-01-01
  • 1970-01-01
  • 2012-05-06
相关资源
最近更新 更多