How can I make a cell in a ListView in Android expand and contract vertically when it's touched?

前端 未结 6 1085
别那么骄傲
别那么骄傲 2020-12-01 08:20

I have a cell in a ListView that has a bunch of text in it. I show the first two rows of text and then end it with a \"...\" if it goes beyond. I want a user to be able to t

6条回答
  •  攒了一身酷
    2020-12-01 08:51

    I've never used ExpandableListViews, but I figure it's pretty simple to do a "manual" implementation of that. Extending an ArrayAdapter we can manipulate the row layout as we want. So, create your extended adapter, and two onClickListeners, one to expand, one to contract. I've pasted the whole class here.

    If you don't want some of the items to be extendable just adjust the listeners to your needs. If you want it to look better you can add some sliding animation, but from here you should have enough to do whatever you need!

    public class ExtendedAdapter extends BaseAdapter {
    
    public static final String TAG = "TodoAdapter";
    
    private Context mContext;
    private int layoutResource; 
    private List items; 
    
    private OnClickListener expand;
    private OnClickListener contract;
    public ExtendedAdapter(Context context, int textViewResourceId,
            List items) {
        this.items = items;
        this.mContext = context;
        this.layoutResource = textViewResourceId;
    
        expand = new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                TextView tv = (TextView) v;
                String[] textSplited = tv.getText().toString().split(" "); // somehow split your text
                tv.setMaxLines(textSplited.length);
                StringBuilder sb = new StringBuilder();
                for (String word : textSplited)
                    sb.append(word + "\n");
                tv.setText(sb.toString());
                tv.setOnClickListener(contract);
            }
        };
    
        contract = new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                TextView tv = (TextView) v;
                tv.setMaxLines(1);
                String[] textSplitted = tv.getText().toString().split("\n");
                StringBuilder sb = new StringBuilder();
                    for (String word : textSplitted)
                        sb.append(word + " ");
                tv.setText(sb.toString());
                tv.setOnClickListener(expand);
            }
        };
    
    }
    
    public int getCount() {
        return items.size();
    }
    
    public Object getItem(int position) {
        return position;
    }
    
    public long getItemId(int position) {
        return position;
    }
    
    
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(layoutResource, null);
        }           
        TextView text = (TextView) v.findViewById(R.id.extended_text);
        text.setText(items.get(position));
        text.setOnClickListener(expand);
        return v;
    }
    }
    

    And the row_layout.xml

    
    
    
    
         
    
    
    
    

提交回复
热议问题