recyclerview textview点击获取post_id?

时间:2017-02-26 14:06:08

标签: android android-recyclerview

我有这样的回收视图:

Name
image
Publisher
post_id textview with ID value hidden
Like unlike(invisible)

Name
image
Publisher
post_id textview with ID value hidden
Like unlike(invivible)
...

当有人点击时,它将变为不可见,并且不同的文本视图将可见。

问题是,我需要将post id值传递给该用户想要的.PHP。

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {

//Imageloader to load image
private ImageLoader imageLoader;
private final Context context;

//List to store all posts
List<Posts> Posts;

//Constructor of this class
public CardAdapter(List<Posts> Post, Context context){
    super();
    //Getting all
    this.Posts = Post;
    this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.posts_list, parent, false);
    ViewHolder viewHolder = new ViewHolder(v);
    return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

    //Getting the particular item from the list
    Posts post =  Posts.get(position);

    //Loading image from url
    imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();

    //Showing data on the views
    holder.imageView.setImageUrl(post.getImageUrl(), imageLoader);
    holder.textViewName.setText(post.getName());
    holder.textViewPublisher.setText(post.getPublisher());
    holder.textViewID.setText(post.getPid());
    holder.setIsRecyclable(false);

}

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

class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    //Views
    public NetworkImageView imageView;
    public TextView textViewName;
    public TextView textViewPublisher;
    public TextView textViewID;

    //Initializing Views
    public ViewHolder(View itemView) {
        super(itemView);
        imageView = (NetworkImageView) itemView.findViewById(R.id.imageViewHero);
        textViewName = (TextView) itemView.findViewById(R.id.textViewName);
        textViewPublisher = (TextView) itemView.findViewById(R.id.textViewPublisher);
        textViewID = (TextView) itemView.findViewById(R.id.textViewID);
        textViewName.setClickable(true);
        textViewName.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        //
    }

}
}

所以我有很多喜欢和不喜欢的东西,很多post_id值...我怎样才能在用户点击过的同一张卡片中获取ID值?

2 个答案:

答案 0 :(得分:2)

您可以在onBindViewHolder()中执行此操作,如下所示

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            holder.getAdapterPosition(); //use this as index to get item in adapter list
        }
    });
}

答案 1 :(得分:1)

您必须使用回调设计模式。

创建界面

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.properties import BooleanProperty


Builder.load_string('''

<CustomDropDown>:

    Button:
        id: mainbtn
        text: 'Difficulty Level'
        on_release: 
            if not root.state: dropdown.open(self); root.state = True
            else: dropdown.dismiss(); root.state = False
        size_hint_y: None
        height: 50

    DropDown:
        id: dropdown
        on_parent: 
            self.dismiss()
        on_select: mainbtn.text = '{}'.format(args[1]); root.state = False
        Button:
            text: 'easy'
            size_hint_y: None
            height: 50
            on_release: dropdown.select(self.text)

        Button:
            text: 'difficult'
            size_hint_y: None
            height: 50
            on_release: dropdown.select(self.text)

''')

class CustomDropDown(BoxLayout):
    state = BooleanProperty(False)

class ttddkvApp(App):
    def build(self):
        return CustomDropDown()

if __name__=='__main__':
    ttddkvApp().run()

现在创建一个如下所示的适配器

public interface OnItemClickListener {
        //view on which user clicked and the post_id for which you want to 
        //some specific action
        void onItemClick(View itemView, int post_id);
    }

从活动或片段

设置适配器
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    // ...

    /***** Creating OnItemClickListener *****/

    // Define listener member variable
    private OnItemClickListener listener;

    // Define the method that allows the parent activity or fragment to define the listener
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView tvName;
        public TextView tvHometown;

        public ViewHolder(final View itemView) {
            super(itemView);
            this.tvName = (TextView) itemView.findViewById(R.id.tvName);
            this.tvHometown = (TextView) itemView.findViewById(R.id.tvHometown);
            // Setup the click listener
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // Triggers click upwards to the adapter on click
                    if (listener != null) {
                        int position = getAdapterPosition();
                        if (position != RecyclerView.NO_POSITION) {
                            // Assuming positionList contains list of post id's
                            int post_id = positionList.get(position);
                            listener.onItemClick(itemView, post_id);
                        }
                    }
                }
            });
        }
    }

    // ...
}