WANTED: TableLayout-Like ListView

综合技术 Hello, buddy! (源链)

My question might be, “How does one create a single-line horizontal LinearLayout with two single-line non-wrapping TextViews, where the left TextView always occupies 1/3 of the available screen width, the right TextView always occupies 2/3 of the available screen width, and the text labels truncate with … when they are too long to display?” (If that question is clear enough and the reader — that’s you — already has in mind a solution they are willing to share, further reading of the problem and question description may be unnecessary.)

A ListView populated with four such LinearLayouts would then look something like this:

medium length text ---- short text
short text         ---- text that is too loooooooooooooo...
loooooooooooooo... ---- short text
loooooooooooooo... ---- text that is too loooooooooooooo...

In this mock-up, in the first row , the left and right text labels were short enough to display completely, in the second row , the left label was short enough to display completely, while the text of the right label was too long and was thus truncated, and the start of the text of the right label aligned vertically with the right text of the first row, visually creating a column as if this were in a TableLayout, in the third row , the text of the left label was too long and was truncated so as to vertically align with the left text of the second and first rows, and the text of the right label started vertically aligned with the right text of the second and first rows, and in the fourth row , the text of the left label was too long and thus was displayed similarly to the left text of the third row, and the text of the right label was too long and thus was displayed similarly to the right text of the second row.

From various posts on stackoverflow and other places, I gather that while using a TableLayout with an ArrayAdapter is somewhat possible, there are downsides, and it is probably not ever the right approach to take.

I’ve certainly tried many combinations of layout parameters, but nothing has yet come very close to the goal. I’m trying to figure out a solution that will work on many different screen sizes, so specifying specific pixel widths probably wouldn’t work very well.

Perhaps the following simple code example (with screenshot) is a good starting point for the solution, and just needs some small modifications.

(The stackoverflow code formatter was freaking out about this code. So, please forgive any funky formatting.)

public class TableLayoutLikeListView extends ListActivity
{
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);

    List data = new ArrayList();
    data.add(new Datum("short", "short"));
    data.add(new Datum("short", "loooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnnnnng"));
    data.add(new Datum("loooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnnnnng", "short"));
    data.add(new Datum("loooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnnnnng",
        "loooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnnnnng"));
    setListAdapter(new MyAdapter(this, R.layout.row, data));
  }
}
class Datum
{
  String left, right;

  Datum(String left, String right)
  {
    this.left = left;
    this.right = right;
  }
}

class MyAdapter extends ArrayAdapter
{
  List data;
  int textViewResourceId;

  MyAdapter(Context context, int textViewResourceId, List data)
  {
    super(context, textViewResourceId, data);
    this.data = data;
    this.textViewResourceId = textViewResourceId;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent)
  {
    if (convertView == null)
    {
      LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      convertView = inflater.inflate(textViewResourceId, null);
    }
    Datum datum = data.get(position);
    if (datum != null)
    {
      TextView leftView = (TextView) convertView.findViewById(R.id.left);
      TextView rightView = (TextView) convertView.findViewById(R.id.right);
      if (leftView != null)
      {
        leftView.setText(datum.left);
      }
      if (rightView != null)
      {
        rightView.setText(datum.right);
      }
    }
    return convertView;
  }
}
list.xml:


    
row.xml:


    
    
    

(Note that while TextView does have the ellipsize attribute, it doesn’t appear necessary to alter, as setting singleLine to true truncates and ellipsizes the labels already. I may be wrong about this.)

Following is a screenshot of this code in action:

Any advice is of course appreciated.

您可能感兴趣的

ListView does not call ItemCreated after postback I have an asp:ListView where I want to filter the items based on a property of the contained DataItem I want to set the item invisible unless the sp...
美团点餐—listview内部按钮点击事件 PS:长时间不写博客了,今天来写一下美团的这个点餐界面,今天先写一个加号减号的接口调用,下一篇是整体,有点菜,评价,商家,还有左边的listview和右边的展示项。进入这篇正题,像listview,GridView等这些view都有写好的一些点击监听,但有些并不是咱们想要用到的,比如说listvie...
Android ListView Tutorial with Kotlin Update Note : This tutorial is now up to date with the latest version of Android Studio version 3.0.1, and uses Kotlin for app development. Updat...
VCL Styles Utils – Major Update (Dialogs, Progress... A Major updated was made to the VCL Styles Utils project. This version include several fixes and new features. Vcl.Styles.Hooks The Vcl.Styles....
Is there a simple way to have a ListView automatic... I have a ListView which is bound to an observable collection. As items are added to the observable collection, the listview does not automatically scr...
Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » WANTED: TableLayout-Like ListView



专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录