• 首页

  • 官方

  • 主题

  • 关注

  • 联系

Android安卓开发:RecyclerView的快速使用

Android安卓开发:RecyclerView的快速使用

前言:

1. 我借鉴了网络上的代码

2. 我并没有深入了解,难免出错

3. 默认已经导入了依赖包,跳过

效果(并非此次给出代码的效果):

image

代码:

1.准备layout文件(命名格式为adapter_*.xml)

2.准备*Adapter.java文件

3.默认存在Activity.java或Fragment.java文件

4.下面代码以RankAdapter为例

  • adapter_rank.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="150dp"
    android:layout_margin="8dp">

    <LinearLayout
        android:id="@+id/items"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="10dp">

           <TextView
               android:id="@+id/upinfo"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"/>
    </LinearLayout>

</RelativeLayout>
  • RankFragment.java
public class RankFragment extends BaseFragment{
	private RankAdapter mAdapter;
	private FragmentRankBinding mBinding;
	
	public static RankFragment newInstance() {
		return new RankFragment();
	}
	
	@Override
	protected ViewBinding getBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {
		return mBinding = FragmentRankBinding.inflate(inflater, container, false);
	}
	
	@Override
	protected void initView() {
		setRecyclerView();
		mAdapter.getData("...省略","...省略");
	}
	
	private void setRecyclerView() {
		mBinding.recycler.setHasFixedSize(true);
		mBinding.recycler.setItemAnimator(null);
		mBinding.recycler.setLayoutManager(new GridLayoutManager(getContext(), 1));
		//戈小戈,上面是设置布局管理器
		mBinding.recycler.setAdapter(mAdapter = new RankAdapter(this));
		//戈小戈,设置适配器
	}
	
	@Override
	public void onItemClick(int position,String upinfo) {
		Log.d("gxg",upinfo);
    }
	//戈小戈,实现点击跳转事件,对应RankAdapter.java里的OnClickListener{onItemClick}
}
  • RankAdapter.java
public class RankAdapter extends RecyclerView.Adapter<RankAdapter.ViewHolder> {
	private final OnClickListener mListener;
	private List<String> mUpinfoItems;

	public RankAdapter(OnClickListener listener) {
		this.mUpinfoItems = new ArrayList<>();
	}
	
	public interface OnClickListener {
		void onItemClick(int position,String upinfo);
	}
	
	public void getData(String urls,String urlss) {
		OkHttp.newCall(urls, Headers.of(HttpHeaders.REFERER, urlss)).enqueue(new Callback() {
			@Override
			public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
				ResponseBody responseBody = response.body();
				BufferedSource source = responseBody.source();
				source.request(Long.MAX_VALUE); 
				Buffer buffer = source.getBuffer();
				String responseBodyString = buffer.clone().readString(StandardCharsets.UTF_8);

				mUpinfoItems.clear();
				mUpinfoItems = ...省略;
				addAll();//戈小戈,添加数据
			}
		});
		//戈小戈,这上面是在获取数据
	}
	
	public void addAll(){
		App.post(this::refresh,0);
		//戈小戈,这个地方为了避免出现非创建线程操作样式报错采用另开线程更新样式数据
	}
	
	@SuppressLint("NotifyDataSetChanged")
	public void refresh(){
		notifyDataSetChanged();
	}
	//戈小戈,上面是获取到数据后刷新
	
	@Override
	public int getItemCount() {
		return mUpinfoItems.size();
		//戈小戈,获取item数目
	}
	
	@NonNull
	@Override
	public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
		return new ViewHolder(AdapterRankBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
		//戈小戈,上面是获取adapter的样式
	}
//    戈小戈,这个函数主要是用来加载子项布局,然后创建ViewHolder实例并把子项布局传入到构造函数中,最后返回ViewHolder实例

	@SuppressLint("SetTextI18n")
	@Override
	public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
		holder.binding.upinfo.setText(mUpinfoItems.get(position));//戈小戈,设置文字
		holder.binding.items.setOnClickListener(v -> mListener.onItemClick(position,mUpinfoItems.get(position)));//戈小戈,设置点击事件
	}
//戈小戈,重写onBindViewHolder()方法,这个方法是用来对传入的子项布局进行赋值的,通过position参数得到当前项的实例,然后再把数据设置到ViewHolder的ImageView和TextView里

	static class ViewHolder extends RecyclerView.ViewHolder {
		private final AdapterRankBinding binding;
		ViewHolder(@NonNull AdapterRankBinding binding) {
			super(binding.getRoot());
			this.binding = binding;
		}
	}
}
posted @ 2024-01-31 22:39  戈小戈  阅读(43)  评论(0编辑  收藏  举报