Created
February 20, 2015 04:08
-
-
Save aows/3701dd55b3c8c899878e to your computer and use it in GitHub Desktop.
Grid Layout with Header
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:background="@android:color/white" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin"> | |
<android.support.v7.widget.RecyclerView | |
android:id="@+id/recycler_view" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<TextView xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_margin="5dp" | |
android:background="@color/material_blue_grey_800" | |
android:padding="10dp" | |
android:textColor="@android:color/holo_orange_light" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ws.otero.adrian.gridexample; | |
import android.graphics.Color; | |
import android.os.Bundle; | |
import android.support.v7.app.ActionBarActivity; | |
import android.support.v7.widget.GridLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.TextView; | |
public class MainActivity extends ActionBarActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
TextView header = new TextView(this); | |
header.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); | |
header.setTextColor(Color.parseColor("#000000")); | |
header.setText("This is the header"); | |
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); | |
recyclerView.setHasFixedSize(true); | |
GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2); | |
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { | |
@Override | |
public int getSpanSize(int position) { | |
return position == 0 ? 2 : 1; | |
} | |
}); | |
recyclerView.setLayoutManager(gridLayoutManager); | |
ElementsAdapter elementsAdapter = new ElementsAdapter(5); | |
elementsAdapter.setHeader(header); | |
recyclerView.setAdapter(elementsAdapter); | |
} | |
public class ElementsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { | |
private static final int VIEW_HEADER = 0; | |
private static final int VIEW_NORMAL = 1; | |
private View headerView; | |
private int datasetSize; | |
public class HeaderViewHolder extends RecyclerView.ViewHolder { | |
public HeaderViewHolder(View v) { | |
super(v); | |
} | |
} | |
public class ViewHolder extends RecyclerView.ViewHolder { | |
public TextView textView; | |
public ViewHolder(TextView v) { | |
super(v); | |
textView = v; | |
} | |
} | |
public ElementsAdapter(int size) { | |
this.datasetSize = size; | |
} | |
public void setHeader(View v) { | |
this.headerView = v; | |
} | |
@Override | |
public int getItemViewType(int position) { | |
return position == 0 ? VIEW_HEADER : VIEW_NORMAL; | |
} | |
@Override | |
public int getItemCount() { | |
return datasetSize + 1; | |
} | |
@Override | |
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
if (viewType == VIEW_HEADER) { | |
return new HeaderViewHolder(headerView); | |
} else { | |
TextView textView = (TextView) LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_element, parent, false); | |
return new ViewHolder(textView); | |
} | |
} | |
@Override | |
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) { | |
if (position == 0) return; | |
ViewHolder holder = (ViewHolder) viewHolder; | |
holder.textView.setText("Position " + (position - 1)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment