在ListView中一次显示单个项目

时间:2018-06-25 13:05:54

标签: android android-layout listview wear-os

我正在开发WearOS应用。我正在使用ListView显示字符串列表。现在,ListView看起来像这样:

enter image description here

我希望单行占据整个屏幕。用户向上滑动后,第2行将占据整个屏幕。再次向上滑动,第3行将占据整个屏幕,依此类推:enter image description here

我碰到了这个link,这正是我想要做的,但是第一种方法不起作用,第二种方法建议使用一个库,但是我不想将库用于任何用途似乎是一个非常简单的任务。我不想使用RecyclerView。谢谢您的帮助。

这是我主要活动的XML文件,它是ListView所在的位置。

<?xml version="1.0" encoding="utf-8"?>
<android.support.wear.widget.BoxInsetLayout 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"
    android:background="@color/dark_grey"
    android:padding="@dimen/box_inset_layout_padding"
    tools:context=".MainActivity"
    tools:deviceIds="wear">

    <!-- Change FrameLayout to a RelativeLayour -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="@dimen/inner_frame_layout_padding"
        app:boxedEdges="all">

        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true" />
    </RelativeLayout>
</android.support.wear.widget.BoxInsetLayout>

1 个答案:

答案 0 :(得分:1)

尝试以下操作(这里的想法是通过编程将textView的高度设置为屏幕的高度):

1)MnnnnnnnActivity.class:----------

public class MnnnnnnnActivity extends AppCompatActivity {

private ListView lv;
private CustomAdapter customAdapter;
private String[] s = new String[10];

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.layout6);

    for (int i = 0; i < 10; i++) {
        s[i] = "ROW " + String.valueOf(i + 1);
    }

    lv = (ListView) findViewById(R.id.lv);
    customAdapter = new CustomAdapter(MnnnnnnnActivity.this, s);
    lv.setAdapter(customAdapter);

}

public int getScreenHeight() {

    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    return displayMetrics.heightPixels;

}
}

2)CustomAdapter.class:--------

public class CustomAdapter extends ArrayAdapter<String> {

private String[] s;
private WeakReference<MnnnnnnnActivity> mActivity;

public CustomAdapter(MnnnnnnnActivity activity1, String[] s) {
    super(activity1.getApplicationContext(), R.layout.list_view_item, s);
    this.s = s;
    mActivity = new WeakReference<MnnnnnnnActivity>(activity1);

}

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

    ViewHolder holder;

    if(mActivity != null) {
        MnnnnnnnActivity activity = mActivity.get();
        if (activity != null) {
            if (convertView == null) {
                LayoutInflater inflater = LayoutInflater.from(activity);
                convertView = inflater.inflate(R.layout.list_view_item, null);
                holder = new ViewHolder();

                holder.tv = (TextView) convertView.findViewById(R.id.tv);

                convertView.setTag(holder);

            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.tv.setText(s[position]);
            holder.tv.setHeight(activity.getScreenHeight());


        }
    }

    return convertView;

}

private class ViewHolder {
    TextView tv;
}

}

3)layout6.xml:-----------

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/lv">
</ListView>

</android.support.constraint.ConstraintLayout>

4)list_view_item.xml:----------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/tv"
android:singleLine="true"
    android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="-"
    android:gravity="center"
android:textSize="20sp"
android:textStyle="bold">
</TextView>

</LinearLayout>

5)注意:尽管已在电话上对此进行了测试,但可以将相同的想法用于在可售商品上实现类似的目的。另外,更好的方法是将linearLayout的高度(包含textView)设置为屏幕高度。

6)输出:

Output