Switch to GridLayoutManager to support two-column layout
In order to display both half-width card and full-width card, we use GridLayoutManager and SpanSizeLookup to achieve this purpose. Bug: 111822407 Test: manual Change-Id: I225fb0b5f731e3faf2bca9858395c40d318acf2f
This commit is contained in:
@@ -21,6 +21,7 @@ import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import androidx.recyclerview.widget.GridLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -28,14 +29,16 @@ import java.util.List;
|
||||
|
||||
public class HomepageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements
|
||||
HomepageCardUpdateListener {
|
||||
static final int SPAN_COUNT = 2;
|
||||
|
||||
private static final String TAG = "HomepageAdapter";
|
||||
private static final int HALF_WIDTH = 1;
|
||||
private static final int FULL_WIDTH = 2;
|
||||
|
||||
private final Context mContext;
|
||||
private final ControllerRendererPool mControllerRendererPool;
|
||||
|
||||
private List<HomepageCard> mHomepageCards;
|
||||
private RecyclerView mRecyclerView;
|
||||
|
||||
public HomepageAdapter(Context context, HomepageManager manager) {
|
||||
mContext = context;
|
||||
@@ -81,7 +84,21 @@ public class HomepageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
|
||||
@Override
|
||||
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
|
||||
super.onAttachedToRecyclerView(recyclerView);
|
||||
mRecyclerView = recyclerView;
|
||||
final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
|
||||
if (layoutManager instanceof GridLayoutManager) {
|
||||
final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
|
||||
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
|
||||
@Override
|
||||
public int getSpanSize(int position) {
|
||||
final HomepageCard card = mHomepageCards.get(position);
|
||||
//TODO(b/114009676): may use another field to make decision. still under review.
|
||||
if (card.isHalfWidth()) {
|
||||
return HALF_WIDTH;
|
||||
}
|
||||
return FULL_WIDTH;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -16,11 +16,14 @@
|
||||
|
||||
package com.android.settings.homepage;
|
||||
|
||||
import static com.android.settings.homepage.HomepageAdapter.SPAN_COUNT;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import androidx.recyclerview.widget.GridLayoutManager;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
@@ -35,7 +38,7 @@ public class PersonalSettingsFragment extends InstrumentedFragment {
|
||||
private RecyclerView mCardsContainer;
|
||||
//TODO(b/113966426): rename
|
||||
private HomepageAdapter mHomepageAdapter;
|
||||
private LinearLayoutManager mLayoutManager;
|
||||
private GridLayoutManager mLayoutManager;
|
||||
//TODO(b/113966426): rename
|
||||
private HomepageManager mHomepageManager;
|
||||
|
||||
@@ -52,8 +55,8 @@ public class PersonalSettingsFragment extends InstrumentedFragment {
|
||||
final View rootView = inflater.inflate(R.layout.settings_homepage,
|
||||
container, false);
|
||||
mCardsContainer = (RecyclerView) rootView.findViewById(R.id.card_container);
|
||||
//TODO(b/111822407): May have to swap to GridLayoutManager
|
||||
mLayoutManager = new LinearLayoutManager(getActivity());
|
||||
mLayoutManager = new GridLayoutManager(getActivity(), SPAN_COUNT,
|
||||
LinearLayoutManager.VERTICAL, false /* reverseLayout */);
|
||||
mCardsContainer.setLayoutManager(mLayoutManager);
|
||||
mHomepageAdapter = new HomepageAdapter(getContext(), mHomepageManager);
|
||||
mCardsContainer.setAdapter(mHomepageAdapter);
|
||||
|
Reference in New Issue
Block a user