VIDEO
activity_main layout
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
xmlns: tools = "http://schemas.android.com/tools"
android :layout_width= "match_parent"
android :layout_height= "match_parent"
tools :context= ".MainActivity" >
<android.support.v7.widget.RecyclerView
android :id= "@+id/list"
android :layout_width= "match_parent"
android :layout_height= "match_parent"
android :clipToPadding= "false"
android :paddingBottom= "80dp"
android :scrollbars= "vertical" />
</RelativeLayout >
content_main layout
<? 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= "wrap_content"
android :orientation= "vertical" >
<TextView
android :id= "@+id/textView"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :layout_marginBottom= "8dp"
android :layout_marginLeft= "12dp"
android :layout_marginRight= "12dp"
android :layout_marginTop= "8dp"
android :textSize= "22sp" />
</LinearLayout >
RecyclerViewAdapter class
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private Context mContext ;
private String[] mList ;
public RecyclerViewAdapter(Context contexts, String[] list) {
this .mContext = contexts;
this .mList = list;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from (parent.getContext());
View itemView = inflater.inflate(R.layout.content_main , parent, false );
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.titleTextView .setText(mList [position]);
// setonclick here
holder.setClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
if (isLongClick)
Toast.makeText (mContext , "pos:" + position + " isLongClick" , Toast.LENGTH_SHORT ).show();
else
Toast.makeText (mContext , "pos:" + position, Toast.LENGTH_SHORT ).show();
}
});
}
@Override
public int getItemCount() {
return mList .length ;
}
public static class ViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener, View.OnLongClickListener {
private TextView titleTextView ;
private ItemClickListener clickListener ;
public ViewHolder(View itemView) {
super (itemView);
titleTextView = (TextView) itemView.findViewById(R.id.textView );
itemView.setTag(itemView);
itemView.setOnClickListener(this );
itemView.setOnLongClickListener(this );
}
public void setClickListener(ItemClickListener itemClickListener) {
this .clickListener = itemClickListener;
}
@Override
public void onClick(View view) {
clickListener .onClick(view, getPosition(), false );
}
@Override
public boolean onLongClick(View view) {
clickListener .onClick(view, getPosition(), true );
return true ;
}
}
}
ItemClickListener interface
public interface ItemClickListener {
void onClick(View view, int position, boolean isLongClick);
}
MainActivity class
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView ;
private StaggeredGridLayoutManager mGridLayoutManager ;
private RecyclerViewAdapter mAdapter ;
private String[] mList = {"a" , "b" , "c" , "d" , "e" , "f" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main );
mRecyclerView = (RecyclerView) findViewById(R.id.list );
mGridLayoutManager = new StaggeredGridLayoutManager(1 , StaggeredGridLayoutManager.VERTICAL );
mRecyclerView .setItemAnimator(new DefaultItemAnimator());
mRecyclerView .setLayoutManager(mGridLayoutManager );
mAdapter = new RecyclerViewAdapter(getApplicationContext(), mList );
mRecyclerView .setAdapter(mAdapter );
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main , menu);
return true ;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings ) {
return true ;
}
return super .onOptionsItemSelected(item);
}
}
No comments:
Post a Comment