一旦单击带有 in 片段的回收器列表项,如何将片段意向活动,以及如何获取回收器项目值?
Posted
技术标签:
【中文标题】一旦单击带有 in 片段的回收器列表项,如何将片段意向活动,以及如何获取回收器项目值?【英文标题】:How to intent fragment to activity once clicked recycler list item with in fragment and how to get the recycler item value? 【发布时间】:2016-06-06 09:31:47 【问题描述】:我在这里附上了我的代码。
我有一个片段,其中包含回收站列表项列表。
如果我单击片段上的回收器项目,我将无法移动到新活动。
我想获取我点击了哪个项目的回收站项目值。
所以请建议我如何在单击带有 in 片段的回收器列表项后将片段意图活动,以及如何获取回收器项值?
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
【问题讨论】:
你检查过***.com/a/26196831/4281182 【参考方案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);
【讨论】:
不客气。如果您解决了,请接受答案。 @karthik【参考方案2】:将此添加到您的适配器上,
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));
通过以上步骤,您可以获得点击项目的位置。在这里你可以进入下一个活动。
【讨论】:
以上是关于一旦单击带有 in 片段的回收器列表项,如何将片段意向活动,以及如何获取回收器项目值?的主要内容,如果未能解决你的问题,请参考以下文章