问题
I am trying to figure out how would you implement something like an Android Settings View for both phones and tablets? It doesn't look like a ListView or RecyclerView utilizing CardView? Which Android class or component would you use to implement/design a similar looking ListView?
Its sort of a 2 column layout on tablets and one column layout on phones:


Any sample code or tips would be appreciated.
回答1:
You can accomplish this by using a combination of RecyclerView and CardView for the General settings (Ex: Wireless and networks or Device), then you can use a GridView inside each card to hold the specific settings (Ex: WiFi or Controller). Then to accomplish the two column trick, use resource qualifiers on an xml file containing an integer for the number of columns you want to display.
I'll give you an overview of the code.
You have an xml file like recycler_layout.xml
that holds the frame for everything.
<?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">
<android.support.v7.widget.RecyclerView
android:id="@+id/friend_rv"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Then you have the frame for a card with a GridView inside of it.
<?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">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/settings_card">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your General Setting Title"
android:id="@+id/textView3" />
<GridView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/setting_content"
android:numColumns="@integer/yourColumnInteger"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Finally you have a file like myIntegers.xml that contains yourColumnInteger
where you define the value to be 1 if the user is on a phone (i.e. their screen is X size) and 2 if the user is on a tablet (i.e. their screen is Y size which is bigger than X)
Then all you have to do is wire up all the adapters for the thing. You'll need an adapter that populates the cards and an adapter for the GridView that populates the specific settings. (And I assume you understand that you will probably need another xml layout for the frame that makes of the specific settings like the setting icon and name in a vertical linear layout)
来源:https://stackoverflow.com/questions/30583120/technical-details-of-android-lollipop-settings-view