2017-08-17 76 views
1

如果這是一個簡單的問題,那麼Android中的新增功能對我來說就顯得很空曠。Android RecyclerView onClick不同活動

一切正常,我在我的recyclerView中的onClick正在獲取我的物品的位置,但我的設計要求是能夠點擊recyclerView的一個項目並打開一個新的活動(作爲popover或彈出)。我可以做到這一點,但是我的問題伴隨着我需要在popover上顯示的信息。該信息來自這樣的活動中(一個火力地堡值調用內部)

attributeList.removeAll(attributeList); 
for (DataSnapshot child : dataSnapshot.child("Attribute").getChildren()){ 
    Attribute attribute = child.getValue(Attribute.class); 
      attribute_list newAttributeList = new attribute_list(attribute.Name + ": " + attribute.Value); 
      attributeList.add(newAttributeList); 
} 
attributeAdapter = new attribute_list_adapter(attributeList, getContext()); 
recyclerAttribute.setAdapter(attributeAdapter); 

這完美的作品,顯示信息,但還有更多的則只是一個「值」,並點擊相關的「名字」。基本上,當我選擇一個項目時,我需要獲取被點擊項目的位置(我有),並將它與attributeList中的位置進行比較,以便我可以調用Firebase調用(或以某種方式將數據傳遞給)酥料餅從 「屬性」 類(如姓名,價值,描述,和另一列表(recyclerView)顯示值

我recyclerView:

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

    private List<attribute_list> listItems; 
    private Context context; 

    public attribute_list_adapter(List<attribute_list> listItems, Context context) { 
     this.listItems = listItems; 
     this.context = context; 
    } 

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

    @Override 
    public void onBindViewHolder(ViewHolder holder, int position) { 
     attribute_list listItem = listItems.get(position); 

     holder.txtTitle.setText(listItem.getTxtTitle()); 
    } 

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

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 

     public TextView txtTitle; 

     public ViewHolder(View itemView) { 
      super(itemView); 
      itemView.setOnClickListener(this); 
      txtTitle = (TextView) itemView.findViewById(R.id.txtTitle); 
     } 

     @Override 
     public void onClick(View v) { 

     } 
    } 
} 

回答

1

這是例如:

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

private List<attribute_list> listItems; 
private Context context; 

public attribute_list_adapter(List<attribute_list> listItems, Context context) { 
    this.listItems = listItems; 
    this.context = context; 
} 


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

@Override 
public void onBindViewHolder(ViewHolder holder, int position) { 
    holder.itemView.setOnClickListener(new View.onClickListener() { 
    @Override 
    public void onClick(View v) { 
    onItemClickListener.onItemClick(position); 
    } 
}); 

} 


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

public class ViewHolder extends RecyclerView.ViewHolder { 
    TextView txtberita; 
    ImageView imgberita; 
    TextView txtnama; 
    public ViewHolder(View itemView) { 
     super(itemView); 
     txtnama = (TextView) itemView.findViewById(R.id.txtnama); 
     txtberita = (TextView) itemView.findViewById(R.id.txtberita); 
     imgberita = (ImageView) itemView.findViewById(R.id.imgberita); 
    } 
} 

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){ 
    this.onItemClickListener = onItemClickListener; 
} 

OnItemClickListener onItemClickListener; 

    public interface OnItemClickListener{ 
    void onItemClick(int position); 
} 
} 

您的活動。在OnCreate中()

public class TestActivity extends AppCompatActivity implements attribute_list_adapter.OnItemClickListener { 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_test); 
    attribute_list_adapter adapter = new attribute_list_adapter(listItems, this); 
    adapter.setOnItemClickListener(this); 
} 

    @Override 
public void onItemClick(int position) { 
    // code here 
} 
} 
+0

您必須更改***的值onItemClick(int position); *** –

+0

看起來很有希望。 your_adapter.setOnItemClickListener(本);行爲不正確。當你指定'your_adapt'時,你是在說我的RecyclerView.Adapter變量?我的變量都沒有這個方法來設置哪個結果(我認爲)爲位置爲null。你有時間澄清? – Torewin

+0

我更新了我的答案? –

0

創建界面類似

public interface OnSingleItemClickListener{ 

void onSingleItemClick(int position); 
} 

然後實現它在你的ViewHolder這樣

public class ViewHolder extends RecyclerView.ViewHolder implements OnSingleItemClickListener { 

    public ViewHolder(View itemView) { 
     super(itemView); 
    } 

@Override 
void onSingleItemClick(int position){ 
if(listItems.get(position) == listItems.get(getAdapterPosition)){ 
// TODO do something here 
    } 

} 
現在

您OnBindViewHolder您轉接器內,你必須這樣做。

holder.setOnClickListener(new View.OnClickListener(){ 
@Override 
public void onClick(View view) { 
     holder.onSingleItemClick(position); 
     } 
}):