Android ListView - ellipsize子项

时间:2017-11-30 22:06:58

标签: java android listview

我是Android编码的新手,我的ListView有问题。我有它显示标题和标题的描述。我想简化描述,但我不知道如何。

这是activity_main.xml的代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.filip.htecjson.MainActivity">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:footerDividersEnabled="false"
        android:headerDividersEnabled="false"
        android:ellipsize="marquee"
        android:singleLine="true"
        app:layout_constraintEnd_toEndOf="parent"
        tools:layout_editor_absoluteY="8dp" />
</android.support.constraint.ConstraintLayout>

这是我正在使用的适配器:

class MyAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<Item> items;

    public MyAdapter(Context context, ArrayList<Item> items) {
        this.context = context;
        this.items = items;
    }

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

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        TwoLineListItem twoLineListItem;

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            twoLineListItem = (TwoLineListItem) inflater.inflate(
                    android.R.layout.simple_list_item_2, null);
        } else {
            twoLineListItem = (TwoLineListItem) convertView;
        }

        TextView text1 = twoLineListItem.getText1();
        TextView text2 = twoLineListItem.getText2();

        text1.setText(items.get(position).getTitle());
        text2.setText("" + items.get(position).getDescription());

        return twoLineListItem;
    }
}

我尝试添加这个,但据我所知,它没有做任何事情。

android:ellipsize="marquee"
android:singleLine="true"

那么有没有办法可以简化描述呢?

1 个答案:

答案 0 :(得分:0)

在getView中更改为此行:

  

getLayoutInflater()。inflate(R.layout.anthing_to_inflate,parent,false);