Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

android - Listview: Only one list item with multiple textviews

I need to show multiple text views in only one list item and the rest of the list view items will have just one textview.

How do I achieve this? Any samples or tutorials you can point me at?

Thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Make your own adapter:

BaseAdapter already provieds methods for working with different View types (different layout for list item cells) and to effektivly recycle views:

  • getViewTypeCount(): The count of how many different Views (layouts) are present.
  • getItemViewType(): Returns a integer to identify the view type of the item in the cell. Note the internally BaseAdapter implementation uses an array. Therefore your values returned here must be from 0 to n. You are not allowed to skip indexes.

    public class MyAdapter extends BaseAdapter {

    private final int VIEW_TYPE_NORMAL = 0;
    private final int VIEW_TYPE_4_TEXTS = 1;
    
    /**
     * The inflater for
     */
    protected LayoutInflater inflater;
    protected Context context;
    
    public MyAdapter(Context context) {
        this.context = context;
        this.inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    
    @Override
    public int getViewTypeCount() {
        // There are two view types
        return 2;
    }
    
    @Override
    public int getItemViewType(int position) {
        if (position == 0)
            return VIEW_TYPE_4_TEXTS;
        else
            return VIEW_TYPE_NORMAL;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int type = getItemViewType(position);
        if (convertView == null) {
            convertView = newView(type, parent);
        }
        bindView(position, type, convertView);
        return convertView;
    }
    
    @Override
    public long getItemId(int position) {
        return 0; // replace it with your id, if you have stable ids in your
                    // adapter
    }
    
    /** Inflates the correct view accorind the view type **/
    public View newView(int type, ViewGroup parent) {
    
        if (VIEW_TYPE_4_TEXTS == type) {
            View view = inflater.inflate(R.layout.your_layout_with_4_textviews,
                    parent, false);
            view.setTag(new ViewHolder4TextViews(view)); // ViewHolder pattern
    
            return view;
        }
    
        // Otherwise its a normal item with VIEW_TYPE_NORMAL
        View view = inflater
                .inflate(R.layout.your_normal_layout, parent, false);
        view.setTag(new NormalViewHolder(view)); // ViewHolder pattern
    
        return view;
    
    }
    
    /** Bind the data for the specified {@code position} to the {@code view}. */
    public void bindView(int position, int type, View view) {
    
        if (VIEW_TYPE_4_TEXTS == type) {
            // set the 4 text view values
            ViewHolder4TextViews holder = (ViewHolder4TextViews) view.getTag();
    
            holder.textview1.setText("...");
            holder.textview2.setText("...");
            holder.textview3.setText("...");
            holder.textview4.setText("...");
    
        } else {
            // VIEW_TYPE_NORMAL
            NormalViewHolder holder = (NormalViewHolder) view.getTag();
    
            holder.textview.setText("...");
        }
    
    }
    

    }


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...