【问题标题】:Using two different listview row types使用两种不同的列表视图行类型
【发布时间】:2016-02-18 15:00:12
【问题描述】:

我正在尝试制作一个包含足球(足球)队赛程的列表视图,但我未能在标题(包含日期)和该日期下的比赛之间交替

这是我的代码

  public class FixturesCursorAdapter extends CursorAdapter {
    private static final String TAG = "CURSORLOADER";
    private final int VIEW_TYPE_TITLE = 0;
    private final int VIEW_TYPE_BODY = 1;
    private final int VIEW_TYPE_COUNT = 2;
    private String itemname;
    private int n = 0;
    private ImageView imageView;

    public static class ViewHolder {
        public final TextView homeTeam;
        public final TextView awayTeam;
        public final TextView score;
        public final TextView date;
        public final ImageView homeImage;
        public final ImageView awayImage;

        public ViewHolder(View view) {

            homeTeam = (TextView) view.findViewById(R.id.homeTeam);
            awayTeam = (TextView) view.findViewById(R.id.awayTeam);
            score = (TextView) view.findViewById(R.id.score);
            date = (TextView) view.findViewById(R.id.date);
            homeImage = (ImageView) view.findViewById(R.id.homeTeamImage);
            awayImage = (ImageView) view.findViewById(R.id.awayImageTeam);
        }
    }


    public FixturesCursorAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);
    }

    @Override
    public int getItemViewType(int position) {
        return (position == 0)? VIEW_TYPE_TITLE : VIEW_TYPE_BODY;
    }

    @Override
    public int getViewTypeCount() {
        return VIEW_TYPE_COUNT;
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

        int viewType = getItemViewType(cursor.getPosition());

        int layoutId = -1;

        if (viewType == VIEW_TYPE_TITLE){
            layoutId = R.layout.fixtures_row_title;
        }
        else if (viewType == VIEW_TYPE_BODY){
            layoutId = R.layout.fixtures_table_row;
        }

        View view = LayoutInflater.from(context).inflate(layoutId, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        view.setTag(viewHolder);
        return view;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor){
        String homeTeam = cursor.getString(cursor.getColumnIndex(SoccerContract.FixturesTable.TAG_HOME_TEAM_NAME));
        String awayTeam = cursor.getString(cursor.getColumnIndex(SoccerContract.FixturesTable.TAG_AWAY_TEAM_NAME));
        String goalsHomeTeam = cursor.getString(cursor.getColumnIndex(SoccerContract.FixturesTable.TAG_GOALS_HOME_TEAM));
        String goalsAwayTeam = cursor.getString(cursor.getColumnIndex(SoccerContract.FixturesTable.TAG_GOALS_AWAY_TEAM));
//        String date = cursor.getString(cursor.getColumnIndex(SoccerContract.FixturesTable.TAG_DATE));
        Log.d("CURSOR_bindview", homeTeam);
        Log.d("CURSOR_bindview", awayTeam);

        ViewHolder viewHolder = (ViewHolder) view.getTag();

        try {
            viewHolder.homeTeam.setText(homeTeam);
            viewHolder.awayTeam.setText(awayTeam);
            viewHolder.score.setText(goalsHomeTeam + " - " + goalsAwayTeam);
//            viewHolder.date.setText(Utility.timeTruncate(date));

            Log.d("PicassoLoader", "initiating" + n);
            for (n = 0; n < 2; n++) {
                Log.d("PicassoLoader", "started" + n);
                if (n == 0) {
                    itemname = homeTeam;
                    imageView = viewHolder.homeImage;
                } else {
                    itemname = awayTeam;
                    imageView = viewHolder.awayImage;
                }

                if (imageView != null && itemname != null) {
                    Utility.picassoLoader(context, 0, imageView,itemname);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

标题仅出现在顶部。那么如何在标题(VIEW_TYPE_TITLE)和正文(VIEW_TYPE_BODY)之间手动切换

【问题讨论】:

    标签: android listview


    【解决方案1】:

    您不应该使用newView() 来指定每个项目的视图。请改用getView()

    有关getView()newView() 以及何时使用它们的详细信息,请参阅this question 的答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-24
      相关资源
      最近更新 更多