package com.example.yibaifen;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.yibaifen.Bean.Zhiyuan;
import java.util.ArrayList;
import java.util.List;
public class HomeFragment extends Fragment {
// UI组件声明(如果有)
private TextView textViewSample;
RecyclerView mRecyclerView;
MyAdapter mMyAdapter ;
List<Zhiyuan> myzhiyuanList = new ArrayList<>();
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// 加载Fragment的布局
return inflater.inflate(R.layout.homefragment, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// 初始化UI组件
textViewSample = view.findViewById(R.id.textViewSample1);
mRecyclerView = view.findViewById(R.id.rev);
// 设置文本或执行其他初始化操作
textViewSample.setText("find");
for (int i = 0; i < 50; i++) {
Zhiyuan zhiyuan = new Zhiyuan();
zhiyuan.title = "标题" + i;
zhiyuan.content = "内容" + i;
myzhiyuanList.add(zhiyuan);
}
//设置rec的适配器和布局格式
mMyAdapter = new MyAdapter();
mRecyclerView.setAdapter(mMyAdapter);
LinearLayoutManager layoutManager = new LinearLayoutManager(requireContext());
mRecyclerView.setLayoutManager(layoutManager);
//设置分割线
DividerItemDecoration mDivider = new
DividerItemDecoration(requireContext(),DividerItemDecoration.VERTICAL);
mRecyclerView.addItemDecoration(mDivider);
// 在这里添加更多初始化逻辑或监听器等
}
//适配器中的内容
class MyAdapter extends RecyclerView.Adapter<MyViewHoder> {
@NonNull
@Override
public MyViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//加载每一行视图
View view = View.inflate(requireContext(), R.layout.item_list, null);
MyViewHoder myViewHoder = new MyViewHoder(view);
return myViewHoder;
}
//设置每一个行的内容
@Override
public void onBindViewHolder(@NonNull MyViewHoder holder, int position) {
Zhiyuan zhiyuans = myzhiyuanList.get(position);
holder.mTitleTv.setText(zhiyuans.title);
holder.mTitleContent.setText(zhiyuans.content);
holder.button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(requireContext(),"查看"+holder.getAbsoluteAdapterPosition(),Toast.LENGTH_SHORT).show();
}
});
}
//返回数据数量
@Override
public int getItemCount() {
return myzhiyuanList.size();
}
}
//每一行的视图里部件导入
class MyViewHoder extends RecyclerView.ViewHolder {
TextView mTitleTv;
TextView mTitleContent;
Button button1;
public MyViewHoder(@NonNull View itemView) {
super(itemView);
mTitleTv = itemView.findViewById(R.id.textView);
mTitleContent = itemView.findViewById(R.id.textView2);
button1 = itemView.findViewById(R.id.button1);
}
}
}