首先 我要介绍,我是学习的鸿洋大神的博客,然后写的这个,有兴趣的可以看一下,大神介绍的很详细,连接如下:
首先看一下效果图:
第一步:导入所需要的包
/*首先到导入这两个包*/
compile 'com.android.support:cardview-v7:26.0.0-alpha1'
compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
第二步:写mainActivity,和RecyclerView的 Item 所要用的布局文件
activity_main.xml 的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ljp.recyclerview.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
item的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:cardCornerRadius="5dp"
android:id="@+id/cv"
app:cardElevation="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:id="@+id/item_tv"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="1" />
<ImageView
android:id="@+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/q" />
</LinearLayout>
</android.support.v7.widget.CardView>
第三步:写activity和adapter所需要的代码:
在adapter中,主要是把数据和view进行绑定,由于RecyclerView没有点击事件,所以需要我们自己写回调,然后实现,具体的步骤是,在adapter中自己定义接口,然后在onBindViewHolder方法中给holder.itemView设置对应的点击事件,最后在Mainactivity中回调我们的监听就行了。
adapter的代码如下:
public class Myadapter extends RecyclerView.Adapter<ViewHolder> {
protected Context context;
protected List<String> list;
/**
* 首先在adapter中自定义一个接口, 然后在onBindViewHolder中去为holder.itemView去设置相应
的监听最后回调我们设置的监听。
*/
protected onItemClickListener onItemClickListener;
public Myadapter(Context context, List<String> list) {
this.context = context;
this.list = list;
}
public void setOnItemClickListener(Myadapter.onItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.e("aaa", "Myadapter--onCreateViewHolder--parent" + parent.toString());
ViewHolder holder = new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_vh, parent, false));
return holder;
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.tv.setText(list.get(position) + "");
if (position % 5 == 0) {
Log.e("aaa", "position-->" + position);
holder.iv.setVisibility(View.VISIBLE);
} else {
holder.iv.setVisibility(View.GONE);
}
if (onItemClickListener != null) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int pos = holder.getLayoutPosition();
onItemClickListener.onItemClick(holder.itemView, pos);
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
int pos = holder.getLayoutPosition();
onItemClickListener.onItemLongClick(holder.itemView, pos);
return false;
}
});
}
}
@Override
public int getItemCount() {
return list.size();
}
public void Remove(int position){
list.remove(position);
notifyItemRemoved(position);
}
public void addData(int position){
list.add(position,"刚增加的");
notifyItemInserted(position);
}
public interface onItemClickListener {
void onItemClick(View view, int position);
void onItemLongClick(View view, int position);
}
}
ViewHolder的代码:
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView tv;
public ImageView iv;
public CardView cv;
public ViewHolder(View itemView) {
super(itemView);
tv = itemView.findViewById(R.id.item_tv);
iv = itemView.findViewById(R.id.imageView);
cv = itemView.findViewById(R.id.cv);
}
}
mainActivity的代码:
/**
* 这个项目有一个小问题,它不能在第一个位置添加,我也不知道为什么
*/
public class MainActivity extends AppCompatActivity {
protected RecyclerView rv;
protected List<String> list = new ArrayList<>();
private Myadapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
initView();
//初始化数据
initData();
//listview
//rv.setLayoutManager(new LinearLayoutManager(this));
//GridView
//rv.setLayoutManager(new GridLayoutManager(this,4));
//第二个参数表示横向滑动,还是纵向滑动,第一个参数表示几行,或者几列
rv.setLayoutManager(new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL));
adapter = new Myadapter(this, list);
rv.setAdapter(adapter);
//添加的动画效果
//rv.setItemAnimator(new DefaultItemAnimator());
//添加点击效果
adapter.setOnItemClickListener(new Myadapter.onItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(MainActivity.this, "你点击了-》" + list.get(position), Toast.LENGTH_SHORT).show();
adapter.addData(position);
}
@Override
public void onItemLongClick(View view, int position) {
Toast.makeText(MainActivity.this, "你长按了-》" + list.get(position), Toast.LENGTH_SHORT).show();
adapter.Remove(position);
}
});
}
private int anInt;
/**
* 这属于写着玩呢,没什么意义
* @param menu
* @return
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
MenuItem item = menu.findItem(R.id.search);
SearchView sv = (SearchView) item.getActionView();
//设置搜索框默认是否自动缩小为图标。
sv.setIconifiedByDefault(false);
//设置输入的类型
sv.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
// 设置是否显示搜索按钮
sv.setSubmitButtonEnabled(false);
sv.setQueryHint("请输入增加或者删除的位置");
String s = sv.getQuery().toString();
Log.e("aaa", "sv___.>" + s);
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
Log.e("aaa", "query.>" + query);
anInt = Integer.parseInt(query);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
Log.e("aaa", "newText.>" + newText);
if (!newText.equals("")) {
anInt = Integer.parseInt(newText);
}
return false;
}
});
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.add:
Log.e("aaa","anint--->"+anInt);
adapter.addData(anInt);
break;
case R.id.remove:
adapter.Remove(anInt);
break;
}
return super.onOptionsItemSelected(item);
}
private void initData() {
for (int i = 'A'; i <= 'z'; i++) {
list.add("" + (char) i);
}
}
private void initView() {
rv = (RecyclerView) findViewById(R.id.rv);
}
}