在listView

时间:2016-01-09 18:37:28

标签: android android-listview separator

我正在尝试在列表视图项之间添加分隔符,我已将它们分类为日期顺序并添加了分隔符需要去的列表条目但是一旦它到达分隔符就会停止,没有错误消息或任何内容它只是不添加分离器。我添加了断点,它肯定运行代码来添加它但它没有显示。即使在分隔符之后还有其他要添加的项目,它仍然会在分隔符处停止。

代码:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater itemInflater = LayoutInflater.from(getContext());
    JourneyItem singleItem = list.get(position);

    if(singleItem.isSeperator()){
        //set customRow to seperator layout
        View customRow = itemInflater.inflate(R.layout.journey_list_seperator, parent, false);
        TextView monthText = (TextView) customRow.findViewById(R.id.seperatorMonthText);
        TextView yearText = (TextView) customRow.findViewById(R.id.seperatorYearText);
        Date current = new Date();
        SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        String dtp = GeneralUtil.SQLDateFormatToHuman(list.get(position).getDepartDateTime());
        try {
            current = df.parse(dtp);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat sdfDate = new SimpleDateFormat("MMMM");

        monthText.setText(sdfDate.format(current));
        yearText.setText(list.get(position).getDepartDateTime().substring(6,10));
        return customRow;
    }else {
        View customRow = itemInflater.inflate(R.layout.custom_journeyitem_row, parent, false);


        TextView titleText = (TextView) customRow.findViewById(R.id.titleDisplay);
        TextView fromText = (TextView) customRow.findViewById(R.id.fromLocationDisplay);
        TextView departText = (TextView) customRow.findViewById(R.id.departDateTimeDisplay);
        TextView toText = (TextView) customRow.findViewById(R.id.toLocationDisplay);
        TextView colourLbl = (TextView) customRow.findViewById(R.id.colourDisplay);

        titleText.setText(singleItem.getTitle());

        fromText.setText("From: " + singleItem.getFromLocation());
        departText.setText(singleItem.getDepartDateTime());
        toText.setText("To: " + singleItem.getToLocation());
        colourLbl.setBackgroundColor(singleItem.getColourCode());
        return customRow;
    }

1 个答案:

答案 0 :(得分:1)

像这样创建Coustom适配器。

class CustomAdapter extends BaseAdapter {

    private static final int TYPE_ITEM = 0;
    private static final int TYPE_SEPARATOR = 1;

    private ArrayList<String> mData = new ArrayList<String>();
    private TreeSet<Integer> sectionHeader = new TreeSet<Integer>();

    private LayoutInflater mInflater;

    public CustomAdapter(Context context) {
        mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void addItem(final String item) {
        mData.add(item);
        notifyDataSetChanged();
    }

    public void addSectionHeaderItem(final String item) {
        mData.add(item);
        sectionHeader.add(mData.size() - 1);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return sectionHeader.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
    }

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

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public String getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        int rowType = getItemViewType(position);

        if (convertView == null) {
            holder = new ViewHolder();
            switch (rowType) {
            case TYPE_ITEM:
                convertView = mInflater.inflate(R.layout.snippet_item1, null);
                holder.textView = (TextView) convertView.findViewById(R.id.text);
                break;
            case TYPE_SEPARATOR:
                convertView = mInflater.inflate(R.layout.snippet_item2, null);
                holder.textView = (TextView) convertView.findViewById(R.id.textSeparator);
                break;
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.textView.setText(mData.get(position));

        return convertView;
    }

    public static class ViewHolder {
        public TextView textView;
    }

}

Complete link here.