gpt4 book ai didi

java - RecyclerView 单项 ID

转载 作者:行者123 更新时间:2023-11-30 00:39:29 28 4
gpt4 key购买 nike

嘿,我正在修改我在 Internet 上找到的示例应用程序。 (学习2破解)我想在我的 ReclycerView 中获取单行的 ID。这是代码。

主要

    private void initViews(){
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.card_recycler_view);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);

ArrayList<AndroidVersion> androidVersions = prepareData();
DataAdapter adapter = new DataAdapter(getApplicationContext(),androidVersions);
recyclerView.setAdapter(adapter);
recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener(){

@Override
public void onTouchEvent(RecyclerView recycler, MotionEvent event) {
Toast.makeText(getApplicationContext(),"Here i need the ID of the clicked row",Toast.LENGTH_SHORT).show();
}

@Override
public boolean onInterceptTouchEvent(RecyclerView recycler, MotionEvent event) {
return true;
}

@Override
public void onRequestDisallowInterceptTouchEvent(boolean a){

}

});

}
private ArrayList<AndroidVersion> prepareData(){

ArrayList<AndroidVersion> android_version = new ArrayList<>();
for(int i=0;i<android_version_names.length;i++){
AndroidVersion androidVersion = new AndroidVersion();
androidVersion.setAndroid_version_name(android_version_names[i]);
androidVersion.setAndroid_image_url(android_image_urls[i]);
android_version.add(androidVersion);
}
return android_version;
}

适配器

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private ArrayList<AndroidVersion> android_versions;
private Context context;


public DataAdapter(Context context,ArrayList<AndroidVersion> android_versions) {
this.context = context;
this.android_versions = android_versions;

}

@Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_layout, viewGroup, false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, final int i) {

viewHolder.tv_android.setText(android_versions.get(i).getAndroid_version_name());
Picasso.with(context).load(android_versions.get(i).getAndroid_image_url()).resize(120, 60).into(viewHolder.img_android);
viewHolder.tv_android.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View argi) {
//Toast.makeText(context,String.valueOf(i),Toast.LENGTH_SHORT).show();

}
});

}

@Override
public int getItemCount() {
return android_versions.size();
}

public class ViewHolder extends RecyclerView.ViewHolder{
TextView tv_android;
ImageView img_android;
public ViewHolder(View view) {
super(view);

tv_android = (TextView)view.findViewById(R.id.tv_android);
img_android = (ImageView)view.findViewById(R.id.img_android);
}
}

如您所见,我做了一些更改。我试图实现的行为是这样的:当我单击其中一个 ReclyclerView 按钮时,我想打印它的“id”(与我在适配器中使用的相同,现在在注释后面。我想为每次点击/类别加载不同的 recyclerview。所以我需要一个 switch-case,但是我需要从 recyclerview 中获取每一个 ID。如果我使用它工作的“适配器版本”,每次点击都会显示“1”、“2”等。但是我需要它在 main 上工作,所以我可以再次调用 InitViews(),或者调用它的修改版本。我正在寻找类似的东西:int id1=recycleritem.getid();任何想法?提前致谢。

最佳答案

不要在 RecyclerView 本身上使用 OnTouch 事件。

onBindViewHolder 中的 View 上设置一个 OnClick。当它被触发时,回调到包含被触摸项目所在行的 Activity

使用接口(interface)创建适配器

    MyAdapter adp=new MyAdapter(new MyAdapter.InterfaceCommands() {
@Override
public void OnItemPressed(int position) {
// do something
}
});

不完整的示例适配器。

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.VH> {

public MyAdapter(InterfaceCommands i) {
MyCommands = i;
}

public interface InterfaceCommands {

public void OnItemPressed(int position);

}

private InterfaceCommands MyCommands;

@Override
public void onBindViewHolder(final MyAdapter.VH holder, final int position) {

// Set Up ViewHolder pattern
// ....

holder.containerView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

int position = holder.getAdapterPosition();

MyCommands.OnItemPressed(position);

}
});

}

}

关于java - RecyclerView 单项 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42760629/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com