otak info official Logo   
OTAK.INFO
Personal Blog

Hariyanto Lim

정길상 / 鄭吉祥
Home   Last Posts  
 
Please login to see more topics and discussion categories.
 
goto main category list >> Software programming >> Android

Title : RecyclerView and StaggeredGridLayoutManager
Total Reply : 0
Total View : 4060


Harry
Harry
Total thread: 131
Total reply: 50

Post #152
RecyclerView and StaggeredGridLayoutManager
Using
'
'.str_replace('
', ' ', '
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:+' // actionBarActivity
compile 'com.android.support:cardview-v7:+'
compile 'com.android.support:recyclerview-v7:+'
}
').'
'

activity_main.xml
'
'.str_replace('
', ' ', '
<RelativeLayout 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/my_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</RelativeLayout>
').'
'

my_card_view.xml
'
'.str_replace('
', ' ', '
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:cardCornerRadius="15dp"
android:layout_margin="1dp"
>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>

<TextView android:id="@+id/tv_card_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="@string/app_name"
android:gravity="center"
/>

<ImageView android:id="@+id/iv_card_photo1"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@mipmap/ic_launcher"
/>

<ImageView android:id="@+id/iv_card_photo2"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@mipmap/ic_launcher"
/>

<ImageView android:id="@+id/iv_card_photo3"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@mipmap/ic_launcher"
/>

<ImageView android:id="@+id/iv_card_photo4"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@mipmap/ic_launcher"
/>

</LinearLayout>
</android.support.v7.widget.CardView>
').'
'

MainActivity.java
'
'.str_replace('
', ' ', '
public class MainActivity extends ActionBarActivity {

private final String TAG = MainActivity.class.getSimpleName();

private RecyclerView mRecyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

final boolean bUseLikeListView = false;
if(bUseLikeListView) {
// define layout
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);

// set the RecyclerView's layout
// NOTE: RecyclerView does NOT handle handle by itself (unlike ListView which define layout)
mRecyclerView.setLayoutManager(llm);

// allows for optimizations if all item views are of the same size
// NOTE: do NOT use this IF use StaggeredGridLayoutManager
mRecyclerView.setHasFixedSize(true);
} else {
StaggeredGridLayoutManager sglm = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(sglm);
}

// set adapter (data provider)
mRecyclerView.setAdapter(new MyRecyclerViewAdapter());

// apply animation
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_add) {
Log.d(TAG, "add menu is clicked");
return true;
} else if(id == R.id.action_remove) {
Log.d(TAG, "remove menu is clicked");
return true;

}

return super.onOptionsItemSelected(item);
}
}
').'
'

MyRecyclerViewAdapter.java
'
'.str_replace('
', ' ', '
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyAdapterViewHolder> {

private final String TAG = MyRecyclerViewAdapter.class.getSimpleName();

// create the view
@Override
public MyAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.d(TAG, "onCreateViewHolder(ViewGroup, viewType: " + viewType + ")");

// inflate layout
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_card_view, parent, false);

// parse the view into view holder
return new MyAdapterViewHolder(itemView);
}

// bind the data with the view to show on UI
@Override
public void onBindViewHolder(MyAdapterViewHolder holder, int position) {
Log.d(TAG, "onBindViewHolder(MyAdapterViewHolder, position: " + position + ")");

// set the name base on the position
holder.tvCardName.setText("position: " + position);

holder.iPosition = position;

// JUST DEMO to randomize views for StaggeredGridLayoutManager
int remainder = position % MyAdapterViewHolder.cardPhotoResIds.length;

for(int i = 0; i < MyAdapterViewHolder.cardPhotoResIds.length; i++) {
if(i <= remainder) {
holder.ivCardPhotos[i].setVisibility(View.VISIBLE);
} else {
holder.ivCardPhotos[i].setVisibility(View.GONE);
}
}
}

@Override
public int getItemCount() {
Log.d(TAG, "getItemCount()");
return 100;
}
}
').'
'

MyAdapterViewHolder.java
'
'.str_replace('
', ' ', '
public class MyAdapterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

private final String TAG = MyAdapterViewHolder.class.getSimpleName();

public TextView tvCardName;

public static final int cardPhotoResIds[] = new int[] { R.id.iv_card_photo1, R.id.iv_card_photo2, R.id.iv_card_photo3, R.id.iv_card_photo4 };
public ImageView ivCardPhotos[] = new ImageView[cardPhotoResIds.length];

public int iPosition;

public MyAdapterViewHolder(View itemView) {
super(itemView);

tvCardName = (TextView) itemView.findViewById(R.id.tv_card_name);

for(int i = 0; i < cardPhotoResIds.length; i++) {
ivCardPhotos[i] = (ImageView) itemView.findViewById(cardPhotoResIds[i]);
}

// register click listener
itemView.setOnClickListener(this);
}

/**
* process on click listener
* @param v
*/
@Override
public void onClick(View v) {
Log.d(TAG, "onClick(view), position: " + iPosition);
}
}
').'
'

RecyclerView and StaggeredGridLayoutManager
Falling in love with the world

Write : 2015-03-27 11:23:26
Last edit : 2015-03-27 11:25:15

If you want to create a new reply then please login first.



www.OTAK.INFO
Since 19 January 2007
Page hit : 858,694

Code update 24th June 2013
Brain is a very capable to solve big problems
but requires constant reminders about how to.
peace bird