如何将RecyclerView的位置传递给活动?

时间:2017-03-09 06:14:58

标签: android android-intent android-recyclerview retrofit2 cardview

我正在尝试将recyclerview的位置传递给另一个我正在呼唤改造界面的活动。我可以通过Recyclerview项目的位置,以便我可以从api加载不同的帖子

 @Override
public void onBindViewHolder(TagAdapter.Tag_ViewHolder holder, final int position) {
    holder.tagName.setText(Tags.get(position));
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (position < 8){
                int  pos = (int) getItemId(position) + 1;
                Intent intent = new Intent (view.getContext(), Tag1Post.class);
                intent.putExtra("pos", pos);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);

            }
        }

我的终点

@GET("/tag/{id}/")
Call<Tag> test(@Path("id") int id);

我想用RecyclerView位置替换id。

4 个答案:

答案 0 :(得分:2)

您可以创建一个界面,然后将位置从适配器转移到活动。

holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (position < 8){
                int  pos = (int) getItemId(position) + 1;
                Intent intent = new Intent (view.getContext(), Tag1Post.class);
                intent.putExtra("pos", pos);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
                //create your interface in Activity, then send it to adapter and call it here:
                yourListener.onTransferPosition(position);
            }
        }

在实现接口的活动上:

 public void onTransferPosition(int position) {
    this.position= position;
  }

答案 1 :(得分:2)

不是传递Recycler View的位置,而是传递特定帖子的ID以导航到对该帖子的评论。

<强> 1。在适配器的Bindview持有者

上提供Intent Extra和点击帖子的ID
public void onBindViewHolder(PostViewHolder holder, final int position) {
        holder.postTitle.setText(posts.get(position).getTitle());
        holder.postBody.setText(posts.get(position).getBody());
        setAnimation(holder.itemView);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int id = posts.get(position).getId(); // get Id
                Intent intent = new Intent(context, CommentActivity.class);
                intent.putExtra("pos", id); // Pass Id
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        });
    }

<强> 2。检索传递的意图数据

int mPostId = getIntent().getIntExtra("pos", 0);

第3。将检索到的数据传递给改造端点

Call<List<Comment>> call = apiService.getComments(mPostId);

改造终点

@GET("/posts/{id}/comments/")
    Call<List<Comment>> getComments(@Path("id") int id);

我希望这会帮助某人遇到这类问题。

快乐编码:)

答案 2 :(得分:0)

由于您使用Intent类传递视图位置,您只需在第二个活动中获取位置,如下所示

  public interface OnRecyclerClickListener  {
    void makeRestCall(int position);
}

或者您可以使用界面传递值

onRecyclerClickListener  = (OnRecyclerClick) this.mContext;

实例化界面如下

 holder.itemView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (position < 8){
            int  pos = (int) getItemId(position) + 1;
            startActivity(new Intent (view.getContext(), Tag1Post.class));
            onRecyclerClickListener.makeRestCall(pos);    
        }
    }

发送视图位置如下

public class SecondActivity extends AppCompatActivity implements OnRecyclerClickListener{

@Override
public void makeRestCall(int pos) {
   // do your GET request
}

最后,在第二个活动中实现接口

    static public String CompileScript(String InputFile, String OutputFile)
        {
        Process Compiler = new Process();
        String Result = String.Empty;
        try
            {
            Compiler.StartInfo.FileName = CLuaCreatorOptions.TrainSimulatorDirectory + "\\luac.exe";
            Compiler.StartInfo.Arguments = "-v -o " + OutputFile + " " + InputFile;
            Compiler.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            Compiler.StartInfo.CreateNoWindow = true;
            Compiler.StartInfo.UseShellExecute = false;
            Compiler.StartInfo.RedirectStandardOutput = true;
            Compiler.StartInfo.RedirectStandardError = true;
            Compiler.Start();
            Result = Compiler.StandardOutput.ReadToEnd() + "\n" + Compiler.StandardError.ReadToEnd();
            Compiler.WaitForExit();
            }
        catch (Exception e)
            {
            return "Error compiling script " + e.Message + "\r\n" + Result;
            }
        return Result;
        }

答案 3 :(得分:0)

<table id="sortabletbl1" class="datatable" cellspacing="1" cellpadding="3" border="0" width="100%">
   <tbody>
      <tr>
         <th>Date</th>
         <th>Subject</th>
         <th width="20"></th>
         <th width="20"></th>
      </tr>
      <tr>
         <td>07/03/2017 00:00</td>
         <td><a href="#" onclick="window.open('messages.php?&amp;displaymessage=true&amp;id=3606','email_window','width=650,height=400,scrollbars=yes');return false">Message Subject #1</a></td>
         <td><a href="resendmessage.php?resend=true&amp;emailid=3606"><img src="//placehold.it/32x32/33d" alt="Resend Email" border="0"></a></td>
         <td><a href="#" onclick="doDelete('3606')"><img src="//placehold.it/32x32/d33" alt="Delete" height="16" border="0" width="16"></a></td>
      </tr>
      <tr>
         <td>28/02/2017 13:08</td>
         <td><a href="#" onclick="window.open('messages.php?&amp;displaymessage=true&amp;id=3571','email_window','width=650,height=400,scrollbars=yes');return false">Message Subject #2</a></td>
         <td><a href="resendmessage.php?resend=true&amp;emailid=3571"><img src="//placehold.it/32x32/33d" alt="Resend Email" border="0"></a></td>
         <td><a href="#" onclick="doDelete('3571')"><img src="//placehold.it/32x32/d33" alt="Delete" height="16" border="0" width="16"></a></td>
      </tr>
      <tr>
         <td>28/02/2017 09:48</td>
         <td><a href="#" onclick="window.open('messages.php?&amp;displaymessage=true&amp;id=3555','email_window','width=650,height=400,scrollbars=yes');return false">Message Subject #3</a></td>
         <td><a href="resendmessage.php?resend=true&amp;emailid=3555"><img src="//placehold.it/32x32/33d" alt="Resend Email" border="0"></a></td>
         <td><a href="#" onclick="doDelete('3555')"><img src="//placehold.it/32x32/d33" alt="Delete" height="16" border="0" width="16"></a></td>
      </tr>
      <tr>
         <td>28/02/2017 00:00</td>
         <td><a href="#" onclick="window.open('messages.php?&amp;displaymessage=true&amp;id=3518','email_window','width=650,height=400,scrollbars=yes');return false">Message Subject #4</a></td>
         <td><a href="resendmessage.php?resend=true&amp;emailid=3518"><img src="//placehold.it/32x32/33d" alt="Resend Email" border="0"></a></td>
         <td><a href="#" onclick="doDelete('3518')"><img src="//placehold.it/32x32/d33" alt="Delete" height="16" border="0" width="16"></a></td>
      </tr>
   </tbody>
</table>

在你的适配器中使用它,并从你的构造函数中询问。

    sudo apt-get update

    sudo apt-get install python-software-properties

    sudo apt-get install apt-file

    sudo apt-file update

    sudo apt-get install software-properties-common

    sudo apt-get install <your filename>  
持有者类中的onclick方法中的

是这样的:

private AdapterView.OnItemClickListener mOnClickListener;

在您的活动中实施Adapter(AdapterView.OnItemClickListener onClickListener){ this.mOnClickListener=onClickListener; }

 @Override
        public void onClick(View view) {

            mOnClickListener.onItemClick(null, view, getAdapterPosition(), view.getId());
        }