我在这附上了我的代码。
我有一个片段,里面有回收者列表项目列表。
如果我单击片段上的回收项目,则无法移动到新活动。
我想获得我点击的项目的回收商品值。
如果点击了片段中的回收者列表项以及如何获取回收商项目价值,请建议我如何将片段分段到活动?
ContactOneFragment
public class ContactOneFragment extends Fragment {
protected RecyclerView.LayoutManager mLayoutManager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_contactone, null, false);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
DetailModel detail[] = {
new DetailModel("Meenakshi.A","9566415797"),
new DetailModel("Pandimurugeswari.S","8940144873"),
new DetailModel("Selvam. K","7708410451"),
new DetailModel("Raja Lakshmi.V","9952754812"),
new DetailModel("Narmatha Devi.S","9894762259"),
new DetailModel("Murugan.M","9566368816"),
new DetailModel("SenthilMurugan.A","7667453595"),
};
mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
DetailAdapter mAdapter = new DetailAdapter(detail);
recyclerView.setAdapter(mAdapter);
recyclerView.setItemAnimator(new DefaultItemAnimator());
return view;
}
}
DetailAdapter
public class DetailAdapter extends RecyclerView.Adapter<DetailAdapter.ViewHolder> {
private DetailModel[] itemsData;
private final ArrayList<Integer> selected = new ArrayList<>();
OnItemClickListener mItemClickListener;
Context context;
public DetailAdapter(DetailModel[] itemsData) {
this.itemsData = itemsData;
}
// Create new views (invoked by the layout manager)
@Override
public DetailAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View itemLayoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.contactnamenumber, null);
// create ViewHolder
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
// - get data from your itemsData at this position
// - replace the contents of the view with that itemsData
viewHolder.namelist.setText(itemsData[position].getName());
viewHolder.numberlist.setText(itemsData[position].getNumber());
// viewHolder.imgViewIcon.setImageResource(itemsData[position].getImageUrl());
// viewHolder.imgViewIcon.setTag(new Integer(position));
}
// inner class to hold a reference to each item of RecyclerView
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView namelist;
public TextView numberlist;
String name;
String number;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
namelist = (TextView) itemLayoutView.findViewById(R.id.name);
numberlist = (TextView) itemLayoutView.findViewById(R.id.contactnumber);
//imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.item_icon);
name=namelist.getText().toString();
number=numberlist.getText().toString();
itemLayoutView.setOnClickListener(this);
}
@Override public void onClick(View view) {
}
}
public interface OnItemClickListener {
public void onItemClick(View view , int position);
}
public void SetOnItemClickListener(final OnItemClickListener mItemClickListener) {
this.mItemClickListener = mItemClickListener;
}
// Return the size of your itemsData (invoked by the layout manager)
@Override
public int getItemCount() {
return itemsData.length;
}
} //Update - Code formatting
答案 0 :(得分:1)
修改构造函数DetailAdapter
,如下所示:
public DetailAdapter(Context context, DetailModel[] itemsData) {
this.context = context;
this.itemsData = itemsData;
}
在适配器的onClick()
方法中添加以下代码:
@Override
public void onClick(View view) {
int pos = getAdapterPosition();
String tempName = itemsData[pos].getName();
String tempNumber = itemsData[pos].getNumber();
Intent intent = new Intent(context, YourActivity.class);
startActivity(intent);
}
答案 1 :(得分:0)
在你的适配器上添加它,
View.OnClickListener mOnClickListener = new OrderClickListener();
然后
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.contactnamenumber, parent, false);
view.setOnClickListener(mOnClickListener);
return new ViewHolder(view);
}
现在在适配器中,
private class OrderClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
mOrderSelect.onOrderSelect(rvOrderHistory.getChildLayoutPosition(v));
}
}
从上面的步骤中,您可以获得单击项目的位置。在这里,您可以进入下一个活动。