我正在尝试在片段中实现一个onItemClick,当点击一个元素时,但我没有结果却有人可以帮助我?
public class FotoFragment extends Fragment {
private List<DataPictures> mediaList = new ArrayList<>();
private RecyclerView mRecyclerView;
private MediaRVAdapter adapter;
public FotoFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_blank, container, false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
mRecyclerView.setPadding(10,10,10,10);
mRecyclerView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast.makeText(v.getContext(), "Intent onLongClick= ", Toast.LENGTH_SHORT).show();
return false;
}
});
return view;
}
这是我的xml fragment_blank
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
我试图制作一个多选摄影能力,但我已经看到你可以用onItemClick方法做到这一点
public void onItemClick(AdapterView parent,View view,int position,long id){
我如何获得该元素的位置?使用onLongClick方法?
答案 0 :(得分:0)
试试这个
可以使用自定义界面
获取OnCLickinterface Listener {
void performOperation(int Position);
}
适配器中的
private Listener mListener;
在你的适配器中用这样的构造函数初始化你的列表器
mListener = (Listener) context;
在bindViewHolder方法中
holder.yourView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.performOperation(position);
}
});
现在你的Fragment实现了这样的接口
public class FotoFragment extends Fragment implements listner {
@Override
public void performOperation(int Position) {
//Do whatever you want to do with this position
}
}