RecyclerVeiwDemo1
来源:互联网 发布:淘宝网qq飞车雷诺永久 编辑:程序博客网 时间:2024/06/09 22:30
/*****************************************************************************************************/MyAdapter
package com.example.recyclerveiwdemo.adapter;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.recyclerveiwdemo.R;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* Created by hasee on 2017/8/30.
*/
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
ArrayList<String> list;
HashMap<Integer, Boolean> isCheckedHashMap;
public MyAdapter() {
isCheckedHashMap = new HashMap<>();
list = new ArrayList<>();
for (int i = 0; i < 30; i++) {
list.add("条目 " + i);
isCheckedHashMap.put(i, false);
}
}
//创建布局和viewHolder
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.LayoutManager layoutManager = ((RecyclerView) parent).getLayoutManager();
View recyclerViewItem = null;
if (layoutManager instanceof GridLayoutManager) {
//inflate的时候,需要传入parent和attachToRoot==false; 使用传入三个参数的方法
recyclerViewItem = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_item_vertical, parent, false);
} else if (layoutManager instanceof LinearLayoutManager) {
//inflate的时候,需要传入parent和attachToRoot==false; 使用传入三个参数的方法
recyclerViewItem = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_item, parent, false);
}
return new MyViewHolder(recyclerViewItem);
}
//绑定数据
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
holder.title.setText(list.get(position));
//整个item条目的点击
// holder.itemView.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// //// TODO: 2017/8/31 暴露一个单击回调接口
// if (mItemClickListener != null) {
// mItemClickListener.onItemClick(v, position);
// }
// }
// });
holder.icon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//// TODO: 2017/8/31 暴露一个单击回调接口
if (mItemClickListener != null) {
mItemClickListener.onItemClick(v, position);
}
}
});
holder.icon.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (mItemLongClickListener != null) {
mItemLongClickListener.onItemLongClick(v, position);
}
// true 消费这个长按事件
return true;
}
});
//针对ListView中checkBox错乱的问题,解决办法是只操作数据不操作checkBox,数据改变之后去刷新
holder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
isCheckedHashMap.put(position, !isCheckedHashMap.get(position));
notifyDataSetChanged();
}
});
holder.checkBox.setChecked(isCheckedHashMap.get(position));
}
@Override
public int getItemCount() {
return list.size();
}
//增加的方法
public void add(int position) {
//改变数据
list.add(position + 1, "这是新加的");
//调用它带的方法去刷新
notifyItemRangeChanged(position + 1, getItemCount());
}
public void remove(int position) {
list.remove(position);
notifyItemRangeRemoved(position, getItemCount());
}
public void updata(int position, String content) {
list.remove(position);
list.add(position, content);
notifyItemChanged(position);
}
//全选
public void selectedAll() {
Set<Map.Entry<Integer, Boolean>> entries = isCheckedHashMap.entrySet();
//如果发现有没有选中的item,我就应该去全部选中,这个变量就应该设置成true,否则就是false
boolean shouldSelectedAll = false;
//这个for循环就是判断一下接下来要全部选中,还是全部不选中
for (Map.Entry<Integer, Boolean> entry : entries) {
Boolean value = entry.getValue();
//如果有没选中的,那就去全部选中 ,如果发现全都选中了那就全部不选中,
if (!value) {
shouldSelectedAll = true;
break;
}
}
//如果shouldSelectedAll为true说明需要全部选中,
// 如果为false说明没有没有选中的,已经是是全部选中的状态,需要全部不选中
for (Map.Entry<Integer, Boolean> entry : entries) {
entry.setValue(shouldSelectedAll);
}
notifyDataSetChanged();
}
//反选
public void revertSelected() {
Set<Map.Entry<Integer, Boolean>> entries = isCheckedHashMap.entrySet();
for (Map.Entry<Integer, Boolean> entry : entries) {
entry.setValue(!entry.getValue());
}
notifyDataSetChanged();
}
//单选,点击checkBox的时候只选中当前的item,在checkBox的点击事件中调用
public void singleSelected(int postion) {
Set<Map.Entry<Integer, Boolean>> entries = isCheckedHashMap.entrySet();
for (Map.Entry<Integer, Boolean> entry : entries) {
entry.setValue(false);
}
isCheckedHashMap.put(postion, true);
notifyDataSetChanged();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
//把整个view放到viewHolder中
View itemView;
TextView title;
ImageView icon;
CheckBox checkBox;
//findviewById 给控件绑定id
public MyViewHolder(View itemView) {
super(itemView);
this.itemView = itemView;
title = (TextView) itemView.findViewById(R.id.title);
icon = (ImageView) itemView.findViewById(R.id.icon);
checkBox = (CheckBox) itemView.findViewById(R.id.checkbox);
}
}
//单击事件的接口
private OnItemClickListener mItemClickListener;
private OnItemLongClickListener mItemLongClickListener;
//设置单击事件的接口
public void setOnItemClickListener(OnItemClickListener itemClickListener) {
mItemClickListener = itemClickListener;
}
//设置长按事件的接口
public void setOnItemLongClickListener(OnItemLongClickListener itemLongClickListener) {
mItemLongClickListener = itemLongClickListener;
}
public interface OnItemClickListener {
void onItemClick(View v, int position);
}
//长按事件的接口
public interface OnItemLongClickListener {
void onItemLongClick(View v, int position);
}
}
/((((((((((((((((((((((((((((((((*************************************************************/MainActivity
package com.example.recyclerveiwdemo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import com.example.recyclerveiwdemo.adapter.MyAdapter;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private MyAdapter myAdapter;
private LinearLayoutManager linearLayoutManager;
private GridLayoutManager gridLayoutManager;
private StaggeredGridLayoutManager staggeredGridLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
//listView的形式展示
linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//gridview的形式展示,可以通过setSpanSizeLookup 来自定义每个item占的列数
gridLayoutManager = new GridLayoutManager(this, 3);
// gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
// @Override
// public int getSpanSize(int position) {
// return 3 - position % 3;
// }
// });
//瀑布流的形式
staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
myAdapter = new MyAdapter();
recyclerView.setAdapter(myAdapter);
myAdapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
@Override
public void onItemClick(View v, int position) {
//增加和删除的话 不直接对postion做处理,因为此处的position是没有刷新以前的position
// myAdapter.add(position);
myAdapter.selectedAll();
}
});
myAdapter.setOnItemLongClickListener(new MyAdapter.OnItemLongClickListener() {
@Override
public void onItemLongClick(View v, int position) {
//remove的操作
// myAdapter.remove(position);
//更新的操作
// myAdapter.updata(position, "这是我更新的内容");
//去反选
myAdapter.revertSelected();
}
});
recyclerView.addItemDecoration(new MyDecoration(this, LinearLayoutManager.VERTICAL));
}
//切换页面布局
public void onClick(View v) {
//切换布局
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
if (layoutManager == null) {
return;
}
//if和elseif是有先后顺序的,先判断范围小的,然后再判断范围大的,
//因为GridLayoutManager 是继承 LinearLayoutManager ,所以他本质上也是LinearLayoutManager,
//所以不能先判断是否是LinearLayoutManager (LinearLayoutManager范围大)
if (layoutManager instanceof GridLayoutManager) {
recyclerView.setLayoutManager(linearLayoutManager);
//重新设置adapter之后 ,会重新走oncreateViewholder可以改变我们的布局
recyclerView.setAdapter(myAdapter);
} else if (layoutManager instanceof LinearLayoutManager) {
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setAdapter(myAdapter);
}
}
}
/********************************************&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&/MyDecoration
package com.example.recyclerveiwdemo;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
/**
* Created by hasee on 2017/8/31.
*/
public class MyDecoration extends RecyclerView.ItemDecoration {
private Context mContext;
private Drawable mDivider;
private int mOrientation;
public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
//我们通过获取系统属性中的listDivider来添加,在系统中的AppTheme中设置
public static final int[] ATRRS = new int[]{
android.R.attr.listDivider
};
public MyDecoration(Context context, int orientation) {
this.mContext = context;
final TypedArray ta = context.obtainStyledAttributes(ATRRS);
this.mDivider = ta.getDrawable(0);
ta.recycle();
setOrientation(orientation);
}
//设置屏幕的方向
public void setOrientation(int orientation) {
if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
throw new IllegalArgumentException("invalid orientation");
}
mOrientation = orientation;
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
if (mOrientation == HORIZONTAL_LIST) {
drawVerticalLine(c, parent, state);
} else {
drawHorizontalLine(c, parent, state);
}
}
//画横线, 这里的parent其实是显示在屏幕显示的这部分
public void drawHorizontalLine(Canvas c, RecyclerView parent, RecyclerView.State state) {
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
//获得child的布局信息
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
final int top = child.getBottom() + params.bottomMargin;
final int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left+params.leftMargin, top, right-params.leftMargin, bottom);
mDivider.draw(c);
//Log.d("wnw", left + " " + top + " "+right+" "+bottom+" "+i);
}
}
//画竖线
public void drawVerticalLine(Canvas c, RecyclerView parent, RecyclerView.State state) {
int top = parent.getPaddingTop();
int bottom = parent.getHeight() - parent.getPaddingBottom();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
//获得child的布局信息
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
final int left = child.getRight() + params.rightMargin;
final int right = left + mDivider.getIntrinsicWidth();
mDivider.setBounds(left, top+params.topMargin, right, bottom-params.topMargin);
mDivider.draw(c);
}
}
//由于Divider也有长宽高,每一个Item需要向下或者向右偏移
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
if (mOrientation == HORIZONTAL_LIST) {
//画横线,就是往下偏移一个分割线的高度
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
} else {
//画竖线,就是往右偏移一个分割线的宽度
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
}
}
}
/*********************************************************************************************/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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="com.example.recyclerveiwdemo.MainActivity">
<android.support.v7.widget.RecyclerView
android:layout_margin="0dp"
android:padding="0dp"
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello World!"></android.support.v7.widget.RecyclerView>
<Button
android:onClick="onClick"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="切换布局" />
</RelativeLayout>
/&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&/recycler_view_item.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="wrap_content"
android:layout_margin="10dp"
android:background="#00ff00"
android:orientation="horizontal">
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:gravity="center" />
</LinearLayout>
/^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^&&&&&&&&&&&&&&&&&&&&&&&/recycler_view_item_vertical.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:background="#00ff00"
android:orientation="vertical">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:gravity="center" />
</LinearLayout>
/&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&/styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:listDivider">@drawable/item_decoration</item>
</style>
</resources>
/&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&/注册
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.recyclerveiwdemo">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
- RecyclerVeiwDemo1
- Python zlib()实现压缩和解压
- oracle之用户和表空间
- You need to use a Theme.AppCompat theme (or descendant) with this activity解决方法
- 一个数组中有一个数字的次数超过了数组的一半,求出这个字符
- 基于Hash摘要签名的公网URL签名验证设计方案
- RecyclerVeiwDemo1
- 欢迎使用CSDN-markdown编辑器
- JDBC:PreparedStatement预编译执行SQL语句
- P4239追捕游戏
- 写给in4sec的粉丝和志同道合的小伙伴们
- 加盐值的方法
- xml乱码问题(图解)
- JSP基础(六)——JSP的动作元素
- 微信小程序WeUI.js网页开发样式库