Search code examples
androidandroid-layoutlistviewwear-os

Show single item at a time in ListView


I am working on a WearOS app. I am using a ListView to show a list of strings. Right now, the ListView looks like this:

enter image description here

I want a single row to take up the entire screen. Once a user swipes up, then Row 2 takes up the entire screen. Swipe up again and Row 3 takes up the entire screen, etc. So like this: enter image description here

I came across this link, which is exactly what I want to do, but the first method doesn't work and the second method suggests a library, but I don't want to use a library for what seems like a pretty simple task. I don't want to use RecyclerView. Thank you for your help.

Here is my XML file for the main activity, which is where the ListView is.

<?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>

Solution

  • Try the following (The idea here is to programmatically set the height of the textView to the height of the screen):

    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) Note: Although this is tested on phone, the same idea can be used to achieve something similar on a wareable. Also, a better approach would be to set the height of the linearLayout (which contains the textView) to the screen height.

    6) Output:

    Output