RecyclerView 的使用
这是RecyclerView所需要的权限:implementation 'com.android.support:recyclerview-v7:26.1.0'这是RecyclerView的布局和添加和删除:<?xml version="1.0" encoding="utf-8"?><LinearLayout xml
·
这是RecyclerView所需要的权限:
implementation 'com.android.support:recyclerview-v7:26.1.0'
这是RecyclerView的布局和添加和删除:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context="com.example.administrator.recyclerview.MainActivity"> <LinearLayout android:layout_width="match_parent" android:orientation="horizontal" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/listview" android:text="ListView" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/gridview" android:text="Gridview" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/delete" android:text="删除" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/add" android:text="添加" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/pubu" android:text="瀑布式" /> </LinearLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/recycler" android:layout_gravity="center" ></android.support.v7.widget.RecyclerView> </RelativeLayout> </LinearLayout>
这是RecyclerView的核心代码:
@Override public void onClick(View view) { switch (view.getId()){ case R.id.listview: recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)); break; case R.id.gridview: recyclerView.setLayoutManager(new GridLayoutManager(this,2)); break; case R.id.pubu: recyclerView.setLayoutManager(new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL)); break; case R.id.add: datas.add("itme"+datas.size()); recycleViewAdapter.notifyDataSetChanged(); recyclerView.setItemAnimator(new DefaultItemAnimator()); break; case R.id.delete: datas.remove(datas.size()-1); recycleViewAdapter.notifyDataSetChanged(); break; } }
这是RecyclerView里面的下划线代码:
recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
这是点击事件:
listview.setOnClickListener(this);//这是listview类型的展示数据 gridview.setOnClickListener(this);//这是GridView类型展示数据 add.setOnClickListener(this);//这是添加 delete.setOnClickListener(this);//这是删除 pubu.setOnClickListener(this);//这是瀑布式展示数据
加载数据:
private void myData() { datas = new ArrayList<>(); for (int i = 0; i <20; i++) { datas.add("itme"+i); } }
这是RecyclerView的适配器(在Activty里面的代码):
private RecycleViewAdapter recycleViewAdapter;
recycleViewAdapter = new RecycleViewAdapter(this,datas); recyclerView.setAdapter(recycleViewAdapter);
这是适配器里面的代码:
public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.ViewHolder> { private List<String> data; private Context context; public RecycleViewAdapter(Context context, List<String> datas) { this.data = datas; this.context = context; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View inflate = View.inflate(context, R.layout.shou_mian, null); ViewHolder viewHolder = new ViewHolder(inflate); return viewHolder; } @Override public void onBindViewHolder(final ViewHolder holder, final int position) { String s = data.get(position); holder.name.setText(s); } @Override public int getItemCount() { return data.size(); } class ViewHolder extends RecyclerView.ViewHolder { private final TextView name; private final ImageView imag; public ViewHolder(View itemView) { super(itemView); name = itemView.findViewById(R.id.name); imag = itemView.findViewById(R.id.imageview); } } }
这是点击事件子条目的图片的代码:
public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.ViewHolder> { private List<String> data; private Context context; public RecycleViewAdapter(Context context, List<String> datas) { this.data = datas; this.context = context; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View inflate = View.inflate(context, R.layout.shou_mian, null); ViewHolder viewHolder = new ViewHolder(inflate); return viewHolder; } @Override public void onBindViewHolder(final ViewHolder holder, final int position) { String s = data.get(position); holder.name.setText(s); holder.imag.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(monClickItmeLisener!=null){ monClickItmeLisener.onclickItme(holder.imag,data.get(position)); } } }); } @Override public int getItemCount() { return data.size(); } class ViewHolder extends RecyclerView.ViewHolder { private final TextView name; private final ImageView imag; public ViewHolder(View itemView) { super(itemView); name = itemView.findViewById(R.id.name); imag = itemView.findViewById(R.id.imageview); } } public interface OnClickItmeLisener{ void onclickItme(View view,String Data); } private OnClickItmeLisener monClickItmeLisener; public void setOnClickItmeLisener(OnClickItmeLisener onClickItmeLisener){ monClickItmeLisener=onClickItmeLisener; } }
这是Activty里面的点击事件:
recycleViewAdapter.setOnClickItmeLisener(new RecycleViewAdapter.OnClickItmeLisener() { @Override public void onclickItme(View view, String Data) { Toast.makeText(MainActivity.this,Data,Toast.LENGTH_SHORT).show(); } });
下面是整个Activty里面的代码:
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.DefaultItemAnimator; 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 android.widget.Button; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button listview,pubu,delete,add,gridview; private RecyclerView recyclerView; private List<String> datas; private RecycleViewAdapter recycleViewAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); myData(); listview.setOnClickListener(this); gridview.setOnClickListener(this); add.setOnClickListener(this); delete.setOnClickListener(this); pubu.setOnClickListener(this); recycleViewAdapter = new RecycleViewAdapter(this,datas); recyclerView.setAdapter(recycleViewAdapter); recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)); recycleViewAdapter.setOnClickItmeLisener(new RecycleViewAdapter.OnClickItmeLisener() { @Override public void onclickItme(View view, String Data) { Toast.makeText(MainActivity.this,Data,Toast.LENGTH_SHORT).show(); } }); } private void myData() { datas = new ArrayList<>(); for (int i = 0; i <20; i++) { datas.add("itme"+i); } } private void initView() { listview = findViewById(R.id.listview); gridview = findViewById(R.id.gridview); add = findViewById(R.id.add); delete = findViewById(R.id.delete); pubu = findViewById(R.id.pubu); recyclerView = findViewById(R.id.recycler); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.listview: recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)); break; case R.id.gridview: recyclerView.setLayoutManager(new GridLayoutManager(this,2)); break; case R.id.pubu: recyclerView.setLayoutManager(new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL)); break; case R.id.add: datas.add("itme"+datas.size()); recycleViewAdapter.notifyDataSetChanged(); recyclerView.setItemAnimator(new DefaultItemAnimator()); break; case R.id.delete: datas.remove(datas.size()-1); recycleViewAdapter.notifyDataSetChanged(); break; } } }
更多推荐
已为社区贡献2条内容
所有评论(0)