简体   繁体   English

如何从另一个ViewHolder中获取对ViewHolder的引用? (RecyclerView)

[英]How to get a reference to a ViewHolder, from inside another ViewHolder? (RecyclerView)

I have a RecyclerView with some view 's/ card 's (let's call it a view for now), which all contain the same things, including a View that I'm using as a divider bar. 我有一个带有一些view / cardRecyclerView (现在我们称之为视图),它们都包含相同的东西,包括我用作分隔栏的View

I want to be able to change the properties of this divider bar in the view above the current one. 我希望能够在当前view上方的view更改此分隔栏的属性。 So say I click on the card that says test3 , I want to be able to set the properties of the divider bar that's in the test2 view . 所以说我点击说明test3的卡片,我希望能够设置test2 view分隔栏的属性。

To clarify: 澄清:

I only need a reference to the ViewHolder for the view above (or any other for that matter) the one I click, that's all. 我只需要一个ViewHolder引用上面的视图(或任何其他的)我点击的那个,就是这样。 How should I do this? 我该怎么做?

在此输入图像描述

public class StuffManagerAdapter extends RecyclerView.Adapter<StuffManagerAdapter.MyViewHolder> {

    private List<StuffManagerClass> stuffList;
    private Context context;
    private String TAG = "CStA";
    DBHandler dbHandler;
    ArrayList<String> tagArray = new ArrayList<>();
    RecyclerView mRecyclerView;

    public StuffManagerAdapter(List<StuffManagerClass> stuffList, Context context) {
        this.stuffList = stuffList;
        this.context = context;
        dbHandler = DBHandler.getInstance(context);
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView DeviceTV, NameTV, TagTV;
        private ImageButton editButton, deleteButton;
        public ImageView tagWarningImage;
        View view, bottomBar ,cover;

        public MyViewHolder(final View view) {
            super(view);
            this.view = view;
            DeviceTV = (TextView) view.findViewById(R.id.SMVFDeviceTextView);
            // etc., etc.

            editButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // do stuff
                }
            });
            deleteButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // do stuff
                }
            });
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_stuffmanagervariablefragment, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        final StuffManagerClass sc = stuffList.get(position);

        // Initialize card, set all textviews
        holder.DeviceTV.setText(sc.getDevice());
        holder.NameTV.setText(sc.getName());
        holder.TagTV.setText(sc.getTag());
        sc.setPosition(position);


        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {    
                // This is where I would like to get a reference to the divider bar on the other view/card
            }
        });
    }

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

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!-- TextViews and stuff -->

        <View
            android:id="@+id/SMVFDividerBar"
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_below="@+id/LL2"
            android:layout_centerHorizontal="true"
            android:layout_marginLeft="7dp"
            android:layout_marginRight="7dp"
            android:background="@color/colorPrimaryDark">

    </RelativeLayout>

</FrameLayout>

Don't know why I didn't realize this sooner, but it works. 不知道为什么我没有早点意识到这一点,但它确实有效。

First, create an instance variable (please correct me if this is not an instance variable). 首先,创建一个实例变量(如果这不是实例变量,请纠正我)。

public class StuffManagerAdapter extends 
                    RecyclerView.Adapter<StuffManagerAdapter.MyViewHolder> {

    RecyclerView mRecyclerView;

Then, assign this variable in the onCreateViewHolder() method like this: 然后,在onCreateViewHolder()方法中分配此变量,如下所示:

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.thingy, parent, false);
    mRecyclerView = (RecyclerView) parent;
    return new MyViewHolder(itemView);
}

Then, in your onBindViewHolder() method, you can access the other ViewHolder 's like this: 然后,在您的onBindViewHolder()方法中,您可以像这样访问其他ViewHolder

mRecyclerView.getChildAt(holder.getAdapterPosition-1);

You can set this to a variable, or call stuff like findViewById() on it directly. 您可以将其设置为变量,或直接调用findViewById()等内容。

Obviously, you can manually input any position you want in the getChildAt() statement, and you can find any object in the view by using findViewById() . 显然,您可以在getChildAt()语句中手动输入所需的任何位置,并且可以使用findViewById()在视图中找到任何对象。

it would have been easy if you would have posted your adapter code too, 如果您也发布了适配器代码,那将会很容易,

well when you click on some item it gives you its position and you can handle other items too by mItems.getAdapterPosition() +1 or -1 当你点击某个项目时它会给你它的位置,你也可以通过mItems.getAdapterPosition()+1或-1来处理其他项目

here is simple example 这是一个简单的例子

In your onClickListerner in ViewHolder all you need to do is 在你onClickListernerViewHolder所有你需要做的是

@Override
    public void onClick(View v) {

        // Do something with item at mItems.getAdapterPosition() -1

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM