Recycleview1 显示垂直排布框,点击弹出对应id
程序员文章站
2022-04-12 08:48:34
Recycleadapter 适配器文件内容package Recyclerview;//Recycleview的适配器import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.ListAdapter...
Recycleadapter 适配器文件内容
package Recyclerview;//Recycleview的适配器
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.Adapter;
import com.example.myapplication1.R;
public class RecycleAdapter extends Adapter<RecycleAdapter.LinearViewHolder> {//可以当成一个模版套用
private Context mContext;
private OnItemClickListner mlistener;
public RecycleAdapter(Context context, OnItemClickListner listener){
this.mContext = context;
this.mlistener = listener;
}
@NonNull
@Override
public RecycleAdapter.LinearViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {//获得一个new布局
return new LinearViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_linear_item,parent,false));//
}
@Override
public void onBindViewHolder(@NonNull RecycleAdapter.LinearViewHolder holder, final int position) {
holder.textView.setText("hello world");//单纯的输入文字
holder.itemView.setOnClickListener(new View.OnClickListener() {//跳出弹窗显示对应的图框的id;
@Override
public void onClick(View view) {
//Toast.makeText(mContext,"click+..."+position,Toast.LENGTH_SHORT).show();
mlistener.onClick(position);
}
});
}
@Override
public int getItemCount() {//列表的长度
return 30;
}
class LinearViewHolder extends RecyclerView.ViewHolder {//在这里声明layout——linear_item布局里的控件
private TextView textView;
public LinearViewHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id._tv_title);
}
}
public interface OnItemClickListner{//创建一个接口
void onClick(int pos);
}
}
Recycler2Activity文件
package Recyclerview;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.example.myapplication1.R;
public class Recyclerview2Activity extends AppCompatActivity {
private RecyclerView mrecycle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recyclerview2);
mrecycle = findViewById(R.id._recycle);
mrecycle.setLayoutManager(new LinearLayoutManager(Recyclerview2Activity.this));//线性布局管理器;
mrecycle.addItemDecoration(new myDecoration());//分割线
mrecycle.setAdapter(new RecycleAdapter(Recyclerview2Activity.this, new RecycleAdapter.OnItemClickListner() {
@Override
public void onClick(int pos) {
Toast.makeText(Recyclerview2Activity.this, "click"+pos, Toast.LENGTH_SHORT).show();
}
})); //调用adapter
}
class myDecoration extends RecyclerView.ItemDecoration{//设置分割线,recycleview2中的背景色是分割线的颜色,linearitem里面的颜色是每一个框框颜色。
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
outRect.set(0,0,0,getResources().getDimensionPixelOffset(R.dimen.dividerHeight));
}
}
}
本文地址:https://blog.csdn.net/hahahaki/article/details/107519098