RecyclerView单项ID

时间:2017-03-13 09:49:02

标签: java android android-recyclerview

Ehi,我正在修改我在互联网上找到的示例应用程序。 (learn2crack) 我想在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);
    }
}

如你所见,我做了一些改变。我试图实现的beahviour是这样的: 当我点击其中一个ReclyclerView按钮时,我想打印它的“id”(我在适配器中使用的相同的东西,现在是评论的背后。 我想为每个点击/类别加载不同的recyclerview。所以我需要一个交换机箱,但是我需要从recyclerview获取每一个ID。 如果我使用“适配器版本”它可以工作,每次点击显示“1”,“2”,依此类推。但我需要这个在main上工作,所以我可以再次调用InitViews(),或者调用它的修改版本。 我正在寻找类似的东西:int id1 = recycleritem.getid(); 任何的想法? 提前谢谢。

2 个答案:

答案 0 :(得分:1)

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

OnClick的视图上设置onBindViewHolder。触发后,使用触摸项目的行回拨Activity

使用界面

创建适配器
    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);

            }
        });

    }

}

答案 1 :(得分:0)

创建一个名为ItemClickListener的接口

public interface ItemClickListener {
    void onClick(int position);
}

在您的活动中实现此接口,并保留适配器输入的引用

private ArrayList<AndroidVersion> android_version;

....
....

@Override
public void onClick(int position) {
    AndroidVersion version = android_version.get(position);
    // do your task with retrieved AndroidVersion
}

更改适配器构造函数,如下所示

new DataAdapter(getApplicationContext(),androidVersions, itemClickListener);

在DataAdapter类中

viewHolder.tv_android.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View argi) {
            //Toast.makeText(context,String.valueOf(i),Toast.LENGTH_SHORT).show();
            if(itemClickListener != null) {
                itemClickListener.onClick(i);
            }
        }
    });