RecyclerView控件代替listview,比listview具有更多的属性,比如删除动画等等,更有三种布局任你挑选。因为RecyclerView并没自带的item点击事件,所以我们需要仿照listview一样自己写一个item的点击事件。首先给出程序运行截图:
接着开始贴出RecyclerView 的代码:这个是我在一个项目中运用的RecyclerView控件而不是针对于它的demo,所以 只能贴出源码,没有源码下载连接.
RecyclerView的xml文件的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="49dp"
android:background="#ee105a"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<ImageView
android:layout_width="109dp"
android:layout_height="match_parent"
android:background="@mipmap/siyifu"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<ImageButton
android:id="@+id/imagebutton_fragmentgame_menu"
android:layout_width="27dp"
android:layout_height="match_parent"
android:layout_marginRight="10dp"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:background="@mipmap/maincaidan"/>
LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview_fragmentgame"
android:layout_width="match_parent"
android:layout_height="match_parent">
android.support.v7.widget.RecyclerView>
LinearLayout>
item的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/imageview_item_recyclerviewgame"
android:adjustViewBounds="true"
android:scaleType="centerCrop"/>
LinearLayout>
Item_RecyclerViewGame文件是针对与item新建的一个属性类:用来存放图片的bitmap
/**
* item的图片。gamefragment的图片。
* Created by lwp on 2016/8/2.
*/
public class Item_RecyclerViewGame {
private Bitmap bitmap_up;
private Bitmap bitmap_back;
public Bitmap getBitmap_back() {
return bitmap_back;
}
public Bitmap getBitmap_up() {
return bitmap_up;
}
public void setBitmap_back(Bitmap bitmap_back) {
this.bitmap_back = bitmap_back;
}
public void setBitmap_up(Bitmap bitmap_up) {
this.bitmap_up = bitmap_up;
}
}
RecyclerViewGameAdapter是Recyclerview的适配器,处理数据并加载:
/**
* 需求;写recycleview的适配器
* Created by lwp on 2016/8/3.
*/
public class RecyclerViewGameAdapter extends RecyclerView.Adapter<RecyclerViewGameAdapter.ViewHodle>{
private List itemRecyclerViewGames;
public RecyclerViewGameAdapter(List item_recyclerViewGames){
super();
this.itemRecyclerViewGames = item_recyclerViewGames;
}
@Override
public ViewHodle onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.item_recyclerview_game,parent,false);
ViewHodle viewHodle = new ViewHodle(view);
return viewHodle;
}
@Override
public int getItemCount() {
return itemRecyclerViewGames.size();
}
@Override
public void onBindViewHolder(ViewHodle holder, int position) {
holder.imageView.setImageBitmap(itemRecyclerViewGames.get(position).getBitmap_up());
}
public static class ViewHodle extends RecyclerView.ViewHolder{
private ImageView imageView;
public ViewHodle(View view){
super(view);
imageView = (ImageView)view.findViewById(R.id.imageview_item_recyclerviewgame);
}
}
}
由于Recyclerview自身并没有自带item的点击事件所以我们需要自己去写一个它的点击事件,即就产生了RecyclerItemClickListener文件:
/**
* 需求:设置recyclerview的点击事件,解决它的item的点击事件处理问答题
* Created by lwp on 2016/8/3.
*/
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener{
public interface OnItemClickListener{
void onItemClick(View view, int position);
void onItemLongClick(View view, int position);
}
private OnItemClickListener mOnItemClickListener;
private GestureDetector gestureDetector;
public RecyclerItemClickListener(Context context,final RecyclerView recyclerView,OnItemClickListener onItemClickListener){
mOnItemClickListener = onItemClickListener;
gestureDetector = new GestureDetector(context,new GestureDetector.SimpleOnGestureListener(){
@Override
public void onLongPress(MotionEvent e) {
View view = recyclerView.findChildViewUnder(e.getX(),e.getY());
if (view != null && mOnItemClickListener != null)
mOnItemClickListener.onItemLongClick(view,recyclerView.getChildPosition(view));
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View view = rv.findChildViewUnder(e.getX(),e.getY());
if (view != null && mOnItemClickListener != null && gestureDetector.onTouchEvent(e)){
mOnItemClickListener.onItemClick(view,rv.getChildPosition(view));
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
}
最后主页面数据的初始化及其展示即Game文件:
/**
* Created by lwp on 2016/7/21.
*/
public class Game extends Fragment{
private View rootView;
public static ImageButton imageButton_fragmentGame_menu;
private RecyclerView recyclerView;
private List itemRecyclerViewGames;
private RecyclerViewGameAdapter recyclerViewGameAdapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
rootView = (View)inflater.inflate(R.layout.fragment_game,container,false);
//控件绑定
isBinDing();
//设置点击事件
isOnClickListener();
//瀑布流失布局设置
StaggeredGridLayoutManager gridLayoutManager = new
StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(gridLayoutManager);
initRecyclerView();
//创建适配器并且设置数据
recyclerViewGameAdapter = new RecyclerViewGameAdapter(itemRecyclerViewGames);
//设置adapter
recyclerView.setAdapter(recyclerViewGameAdapter);
//设置item增加删除的动画
recyclerView.setItemAnimator(new DefaultItemAnimator());
//设置间距
// recyclerView.addItemDecoration(new SpaceItemDecoration(5));
// Intent intent = new Intent(getActivity(), GamePinTuActivity.class);
// startActivity(intent);
recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(),recyclerView,
new RecyclerItemClickListener.OnItemClickListener(){
@Override
public void onItemClick(View view, int position) {
Toast.makeText(getContext(),"您点击了"+position,Toast.LENGTH_SHORT).show();
}
@Override
public void onItemLongClick(View view, int position) {
Toast.makeText(getContext(),"您长删除了"+position,Toast.LENGTH_SHORT).show();
itemRecyclerViewGames.remove(position);
recyclerViewGameAdapter.notifyItemRemoved(position);
Log.e("长度----->",""+(itemRecyclerViewGames.size()));
}
}));
return rootView;
}
/**
* 初始化recyclerview的数据设置
*/
private void initRecyclerView() {
itemRecyclerViewGames = new ArrayList();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.pintu1_up);
for (int i = 0;i<20;i++) {
Item_RecyclerViewGame item_recyclerViewGame = new Item_RecyclerViewGame();
item_recyclerViewGame.setBitmap_up(bitmap);
itemRecyclerViewGames.add(item_recyclerViewGame);
}
}
/**
* 控件的绑定
*/
private void isBinDing(){
imageButton_fragmentGame_menu = (ImageButton)rootView.findViewById(R.id.imagebutton_fragmentgame_menu);
recyclerView = (RecyclerView)rootView.findViewById(R.id.recyclerview_fragmentgame);
}
/**
* 控件的点击事件的设置
*/
private void isOnClickListener(){
imageButton_fragmentGame_menu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
}
到此代码已经全部贴玩,有什么意见可以留言指出。