先看 MainActivity
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState == null) { FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); RecyclerViewFragment fragment = new RecyclerViewFragment(); transaction.replace(R.id.sample_content_fragment, fragment); transaction.commit(); }
}
在activity_main.xml 裡面有個frame_layout
<FrameLayout android:id="@+id/sample_content_fragment" android:layout_weight="2" android:layout_width="match_parent" android:layout_height="0px" />接下來看 RecyclerViewFragment
public class RecyclerViewFragment extends Fragment {@Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Initialize dataset, this data would usually come from a local content provider or // remote server. initDataset(); }}// 初始化 mDataset string array
private void initDataset() { mDataset = new String[DATASET_COUNT]; for (int i = 0; i < DATASET_COUNT; i++) { mDataset[i] = "This is element #" + i; } }@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.recycler_view_frag, container, false); rootView.setTag(TAG); // BEGIN_INCLUDE(initializeRecyclerView) mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView); // LinearLayoutManager is used here, this will layout the elements in a similar fashion // to the way ListView would layout elements. The RecyclerView.LayoutManager defines how // elements are laid out. mLayoutManager = new LinearLayoutManager(getActivity()); mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER; if (savedInstanceState != null) { // Restore saved layout manager type. mCurrentLayoutManagerType = (LayoutManagerType) savedInstanceState .getSerializable(KEY_LAYOUT_MANAGER); } setRecyclerViewLayoutManager(mCurrentLayoutManagerType); mAdapter = new CustomAdapter(mDataset); // Set CustomAdapter as the adapter for RecyclerView. mRecyclerView.setAdapter(mAdapter); // END_INCLUDE(initializeRecyclerView) mLinearLayoutRadioButton = (RadioButton) rootView.findViewById(R.id.linear_layout_rb); mLinearLayoutRadioButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { setRecyclerViewLayoutManager(LayoutManagerType.LINEAR_LAYOUT_MANAGER); } }); mGridLayoutRadioButton = (RadioButton) rootView.findViewById(R.id.grid_layout_rb); mGridLayoutRadioButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { setRecyclerViewLayoutManager(LayoutManagerType.GRID_LAYOUT_MANAGER); } }); return rootView; }recycler_view_frag.xml 內容如下: 有一個 LinearLayout....裡面包含著一個 <android.support.v7.widget.RecyclerView<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="horizontal" android:checkedButton="@+id/linear_layout_rb"> <RadioButton android:id="@+id/linear_layout_rb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/linear_layout_manager"/> <RadioButton android:id="@+id/grid_layout_rb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/grid_layout_manager"/> </RadioGroup> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>再回去看 RecyclerViewFragment
// LinearLayoutManager is used here, this will layout the elements in a similar fashion// to the way ListView would layout elements. The RecyclerView.LayoutManager defines how// elements are laid out.mLayoutManager = new LinearLayoutManager(getActivity());mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;if (savedInstanceState != null) { // Restore saved layout manager type. mCurrentLayoutManagerType = (LayoutManagerType) savedInstanceState .getSerializable(KEY_LAYOUT_MANAGER); } setRecyclerViewLayoutManager(mCurrentLayoutManagerType);mAdapter = new CustomAdapter(mDataset); // Set CustomAdapter as the adapter for RecyclerView.mRecyclerView.setAdapter(mAdapter); // END_INCLUDE(initializeRecyclerView) // 設下去就變成 LINEAR_LAYOUT_MANAGER mLinearLayoutRadioButton = (RadioButton) rootView.findViewById(R.id.linear_layout_rb); mLinearLayoutRadioButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { setRecyclerViewLayoutManager(LayoutManagerType.LINEAR_LAYOUT_MANAGER); } }); // 設下去就變成gridlayout mGridLayoutRadioButton = (RadioButton) rootView.findViewById(R.id.grid_layout_rb); mGridLayoutRadioButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { setRecyclerViewLayoutManager(LayoutManagerType.GRID_LAYOUT_MANAGER); } }); return rootView;}public void setRecyclerViewLayoutManager(LayoutManagerType layoutManagerType) { int scrollPosition = 0; // If a layout manager has already been set, get current scroll position. if (mRecyclerView.getLayoutManager() != null) { scrollPosition = ((LinearLayoutManager) mRecyclerView.getLayoutManager()) .findFirstCompletelyVisibleItemPosition(); } switch (layoutManagerType) { case GRID_LAYOUT_MANAGER: mLayoutManager = new GridLayoutManager(getActivity(), SPAN_COUNT); mCurrentLayoutManagerType = LayoutManagerType.GRID_LAYOUT_MANAGER; break; case LINEAR_LAYOUT_MANAGER: mLayoutManager = new LinearLayoutManager(getActivity()); mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER; break; default: mLayoutManager = new LinearLayoutManager(getActivity()); mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER; } mRecyclerView.setLayoutManager(mLayoutManager); mRecyclerView.scrollToPosition(scrollPosition); }重點就是上面那兩行.... 利用 setLayoutManager(mLayoutManager); 來變成linear 或是 gridlayout
再來看public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {裡面又包含著一個 ViewHolder的類別
public static class ViewHolder extends RecyclerView.ViewHolder { private final TextView textView; public ViewHolder(View v) { super(v); // Define click listener for the ViewHolder's View. v.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d(TAG, "Element " + getAdapterPosition() + " clicked."); } }); textView = (TextView) v.findViewById(R.id.textView); } public TextView getTextView() { return textView; } }// 建構子public CustomAdapter(String[] dataSet) { mDataSet = dataSet; } // BEGIN_INCLUDE(recyclerViewOnCreateViewHolder)// Create new views (invoked by the layout manager)@Overridepublic ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { // Create a new view. View v = LayoutInflater.from(viewGroup.getContext()) .inflate(R.layout.text_row_item, viewGroup, false); return new ViewHolder(v); }
沒有留言:
張貼留言