Android RecyclerView 点击
activity_main.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">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
layout_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:gravity="center"
android:padding="20dp"
android:background="@drawable/selector_item_bg">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#fff"
android:textSize="20sp"
android:padding="20dp"
android:text="1"/>
</LinearLayout>
res/values/colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="item_press">#44ff0000</color>
<color name="item_normal">#009688</color>
</resources>
res/drawable/selector_item_bg.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/item_press"></item>
<item android:drawable="@color/item_normal"></item>
</selector>
MyAdatper.java :
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class MyAdatper extends RecyclerView.Adapter<MyAdatper.ViewHolder> {
private List<String> mDatas;
private LayoutInflater inflater;
interface OnItemClickListener {
void onClick(View v, int position, String name);
}
private OnItemClickListener listener;
public void setOnItemClickListener(OnItemClickListener listener) {
this.listener = listener;
}
public MyAdatper(List<String> datas) {
mDatas = datas;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.layout_item,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.textView.setText(mDatas.get(position));
}
@Override
public int getItemCount() {
return mDatas.size();
}
public void addData(int position, String name) {
mDatas.add(position, name);
notifyItemInserted(position);
}
public void removeData(int position) {
mDatas.remove(position);
notifyItemRemoved(position);
}
class ViewHolder extends RecyclerView.ViewHolder {
private TextView textView;
public ViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.text);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
int position = getLayoutPosition();
listener.onClick(v, position, mDatas.get(position));
}
}
});
}
}
}
MainActivity.java:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<String> datas = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initDatas();
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
recyclerView.setItemAnimator(new DefaultItemAnimator());
MyAdatper adatper = new MyAdatper(datas);
adatper.setOnItemClickListener(new MyAdatper.OnItemClickListener() {
@Override
public void onClick(View v, int position, String name) {
Toast.makeText(MainActivity.this, "姓名:"+ name,Toast.LENGTH_SHORT).show();
}
});
recyclerView.setAdapter(adatper);
}
private void initDatas() {
datas.add("木子才");
datas.add("普特斯");
datas.add("圣华尔");
}
}
添加menu
增加文件 res/menu/menu_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/id_action_add"
android:orderInCategory="100"
android:title="add"
app:showAsAction="ifRoom"
android:icon="@mipmap/as_actionbar_add_icon"></item>
<item android:id="@+id/id_action_delete"
android:orderInCategory="100"
android:title="delete"
app:showAsAction="ifRoom"
android:icon="@mipmap/as_ic_menu_delete"></item>
<item android:id="@+id/id_action_gridview"
android:orderInCategory="100"
android:title="GirdView"
app:showAsAction="never"></item>
<item android:id="@+id/id_action_listview"
android:orderInCategory="100"
android:title="ListView"
app:showAsAction="never"></item>
<item android:id="@+id/id_action_horizontalGridView"
android:orderInCategory="100"
android:title="HorizontalGridView"
app:showAsAction="never"></item>
</menu>
在 MainActivity.java 处添加以下内容:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.id_action_add:
adatper.addData(0, "新名字");
break;
case R.id.id_action_delete:
adatper.removeData(1);
break;
case R.id.id_action_gridview:
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
break;
case R.id.id_action_listview:
recyclerView.setLayoutManager(new LinearLayoutManager(this));
break;
case R.id.id_action_horizontalGridView:
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.HORIZONTAL));
break;
}
return true;
}
添加三点按钮: