关闭

RecyclerView实现 gridview效果!

标签: AndroidRecycleView移动开发
6214人阅读 评论(1) 收藏 举报
分类:
主界面代码

/**
 * @author 
 * @Date 2016829 * @describe 主页面
 */
public class InternetAccessLogFragment extends BaseFragment {

    private View v;
    private RecyclerView recyclerView;
    private TextView top_center;
    private String[] data = {"流量日志", "浏览器日志", "桌面日志", "聊天日志", "邮件日志", "发帖日志", "警告日志", "搜索日志", "移动磁盘", "  ", "  ", "  "};
    private int[] imgdata = {R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
            R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
            R.mipmap.item, R.mipmap.item, R.mipmap.item};
    private RecyclerGridViewAdapter recyclerGridViewAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        return super.onCreateView(inflater, container, savedInstanceState);
        v = inflater.inflate(R.layout.fragment_internet_access_log, null);
        recyclerView = (RecyclerView) v.findViewById(R.id.fragment_recyclerview);
        top_center = (TextView) v.findViewById(R.id.top_center);
        GridLayoutManager mgr = new GridLayoutManager(context, 4);
        recyclerView.setLayoutManager(mgr);
//        int spanCount = 4;//跟布局里面的spanCount属性是一致的
//        int spacing = 2;//每一个矩形的间距
//        boolean includeEdge = false;//如果设置成false那边缘地带就没有间距s
//        //设置每个item间距
//        recyclerView.addItemDecoration(new GridSpacingItemDecoration(spanCount, spacing, includeEdge));
        //设置适配器
        recyclerGridViewAdapter = new RecyclerGridViewAdapter(context, data, imgdata);
        recyclerView.setAdapter(recyclerGridViewAdapter);
        onRecyclerItemClickListener();
        return v;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        top_center.setText("上网日志");
        onRecyclerItemClickListener();

    }

    private void onRecyclerItemClickListener() {
        recyclerGridViewAdapter.setOnRecyclerViewItemListener(new RecyclerGridViewAdapter.OnRecyclerViewItemListener() {
            @Override
            public void onItemClickListener(View view, int position) {
                Toast.makeText(context, "onClick:" + position, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onItemLongClickListener(View view, int position) {
                Toast.makeText(context, "onLongClick:" + position, Toast.LENGTH_SHORT).show();
            }
        });
    }

}

---------------------------------------------------------------------------------------------------------------
适配器代码!!!

//继承自 RecyclerView.Adapter
public class RecyclerGridViewAdapter extends RecyclerView.Adapter<RecyclerGridViewAdapter.ViewHolder> {
    private Context mContext;
    private String[] data;
    private int[] imgdata;
    private LayoutInflater inf;
    //view是否充满了手机屏幕
    private boolean isCompleteFill = false;

    public interface OnRecyclerViewItemListener {
        public void onItemClickListener(View view, int position);

        public void onItemLongClickListener(View view, int position);
    }

    private OnRecyclerViewItemListener mOnRecyclerViewItemListener;

    public void setOnRecyclerViewItemListener(OnRecyclerViewItemListener listener) {
        mOnRecyclerViewItemListener = listener;
    }

    public RecyclerGridViewAdapter(Context mContext, String[] data, int[] imgdata) {
        this.mContext = mContext;
        this.data = data;
        this.imgdata = imgdata;
        inf = LayoutInflater.from(mContext);

    }


    //RecyclerView显示的子View
    //该方法返回是ViewHolder,当有可复用View时,就不再调用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = inf.inflate(R.layout.fragment_internetlog_recycler_item, viewGroup, false);
        return new ViewHolder(v);
    }

    //将数据绑定到子View,会自动复用View
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        if (viewHolder == null) {
            return;
        }
        if (mOnRecyclerViewItemListener != null) {
            itemOnClick(viewHolder);
            itemOnLongClick(viewHolder);
        }
        viewHolder.textView.setText(data[i]);
        viewHolder.imageView.setBackgroundResource(imgdata[i]);
    }

    //RecyclerView显示数据条数
    @Override
    public int getItemCount() {
        return data.length;
    }

    //自定义的ViewHolder,减少findViewById调用次数
    class ViewHolder extends RecyclerView.ViewHolder {

        TextView textView;
        ImageView imageView;

        //在布局中找到所含有的UI组件
        public ViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.fragment__internetlog_recycler_item_textview);
            imageView = (ImageView) itemView.findViewById(R.id.fragment__internetlog_recycler_item_iamgeview);
        }
    }
    //单机事件
    private void itemOnClick(final RecyclerView.ViewHolder holder) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getLayoutPosition();
                mOnRecyclerViewItemListener.onItemClickListener(holder.itemView, position);
            }
        });
    }
    //长按事件
    private void itemOnLongClick(final RecyclerView.ViewHolder holder) {
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                int position = holder.getLayoutPosition();
                mOnRecyclerViewItemListener.onItemLongClickListener(holder.itemView, position);
                //返回true是为了防止触发onClick事件
                return true;
            }
        });
    }

}
-------------------------------------------------------------------------------------------------------------------
Item间距
/**
 * @author 
 * @Date 2016829 * @describe RecyclerView Item间距
 */
public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {

    private int spanCount;
    private int spacing;
    private boolean includeEdge;

    public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
        this.spanCount = spanCount;
        this.spacing = spacing;
        this.includeEdge = includeEdge;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int position = parent.getChildAdapterPosition(view); // item position
        int column = position % spanCount; // item column

        if (includeEdge) {
            outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
            outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)

            if (position < spanCount) { // top edge
                outRect.top = spacing;
            }
            outRect.bottom = spacing; // item bottom
        } else {
            outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
            outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f /    spanCount) * spacing)
            if (position >= spanCount) {
                outRect.top = spacing; // item top
            }
        }
    }
}
-------------------------------------------------------------------------------------------------------------------
主页面XML 代码

<?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="match_parent"
    android:orientation="vertical">

    <include layout="@layout/head" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="5dp"
        android:text="分类"
        android:textColor="#000"
        android:textSize="16dp" />


    <android.support.v7.widget.RecyclerView
        android:id="@+id/fragment_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#fff"
        android:overScrollMode="never"
        android:scrollbars="none" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#DEDEDE" />
</LinearLayout>

其中 android:overScrollMode="never"  android:scrollbars="none" 是去掉滑动阴影效果的代码
-------------------------------------------------------------------------------------------------------------------
Item 代码:

<?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">
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#DEDEDE" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#fff"
            android:orientation="vertical"
            android:padding="8dp">

            <ImageView
                android:id="@+id/fragment__internetlog_recycler_item_iamgeview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center" />

            <TextView
                android:id="@+id/fragment__internetlog_recycler_item_textview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="流量日志" />
        </LinearLayout>

        <View
            android:layout_width="2dp"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="8dp"
            android:layout_weight="1"
            android:background="#DEDEDE" />
    </LinearLayout>
</LinearLayout>




   
0
6

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:6679次
    • 积分:84
    • 等级:
    • 排名:千里之外
    • 原创:2篇
    • 转载:1篇
    • 译文:0篇
    • 评论:1条
    文章分类
    文章存档
    最新评论