我的代码有问题,我想先在片段中单击适配器的项目位置,然后再从片段到MainActivity这个位置,我已经在main_activity.xml中实现了一些设计,因此我希望从该位置获得模型类的一些数据实现适配器单击并成功地接收到片段,但是现在我希望将此位置片段发送到我的代码下面的MainActivty.java
我的适配器类
public class RvAdapter extends RecyclerView.Adapter<RvAdapter.ProductViewHolder> {
private Context mCtx;
private List<DataModal> productList;
private OnItemClickReturnData mListener;
public interface OnItemClickReturnData{
public void OnItemClick(int position);
}
public void setOnClickListener(OnItemClickReturnData listener)
{
mListener = listener;
}
public RvAdapter(Context mCtx, List<DataModal> productList) {
this.mCtx = mCtx;
this.productList = productList;
}
@NonNull
@Override
public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
@SuppressLint("InflateParams") View view = inflater.inflate(R.layout.song_handle_layout, null);
return new ProductViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ProductViewHolder holder, @SuppressLint("RecyclerView") final int position) {
final DataModal product = productList.get(position);
webUrl = "https://musicsongs.000webhostapp.com/";
Glide.with(mCtx)
.load(webUrl + product.getpics())
.into(holder.dp);
holder.pic.setText(product.getpic());
holder.pics.setText(product.getdp());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onClick(View v) {
if (mListener != null)
{
mListener.OnItemClick(position);
}
else {
Toast.makeText(mCtx, "mListeren is null" + mListener, Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public int getItemCount() {
return productList.size();
}
class ProductViewHolder extends RecyclerView.ViewHolder{
TextView textViewTitle, textViewShortDesc;
ImageView imageView;
ProductViewHolder(View itemView) {
super(itemView);
pic= itemView.findViewById(R.id.pic);
pics = itemView.findViewById(R.id.pics);
dp = itemView.findViewById(R.id.dp);
}
}
}
我的片段
public class MainActivityMusicFragment extends Fragment implements RvAdapter.OnItemClickReturnData {
}// i have achieve Adapter click in fragment by implements click method
@Override
public void OnItemClick(int position) {
Toast.makeText(getActivity(), "Posiotion OnClick in Adapter HAndle in Fragment" + position, Toast.LENGTH_SHORT).show();
}// i have received position in fragment but how can i send this position fragment to activity please help me
答案 0 :(得分:0)
在您的活动中创建一个公共方法,例如
public void OnItemClickActivity(int position) {
Toast.makeText(this, "Posiotion OnClick in Activity" + position, Toast.LENGTH_SHORT).show();
}
然后在您的片段中添加
@Override
public void OnItemClick(int position) {
((MainActivity) getActivity()).OnItemClickActivity(position)
Toast.makeText(getActivity(), "Posiotion OnClick in Adapter HAndle in Fragment" + position, Toast.LENGTH_SHORT).show();
}//
答案 1 :(得分:0)
这很简单;-
@Override
public void OnItemClick(int position) {
// this is for your fragment
Bundle bundle = new Bundle();
Intent intent = new Intent(getActivity(), yourActivity.clss);
bundle.putString("position", position);
setArguments(bundle);
startActivity(intent);
Toast.makeText(getActivity(), "Posiotion OnClick in Adapter HAndle in Fragment" + position, Toast.LENGTH_SHORT).show();
}
// this is for your activity
String pos = getIntent().getStringExtra("position");
答案 2 :(得分:0)
您必须在活动中实现回调
声明用于回调的接口:
interface OnMainActivityMusicFragmentListener{
void onItemSelected(int position);
}
在您的活动中实施OnMainActivityMusicFragmentListener
class MainActivity extends AppCompactActivity implements OnMainActivityMusicFragmentListener{
----
}
从Fragment触发回调
private OnMainActivityMusicFragmentListener listener:
@Override
public void onAttach(Context context) {
super.onAttach(context);
listener = (OnMainActivityMusicFragmentListener) context;
}
// pass your position to activity
@Override
public void OnItemClick(int position) {
Toast.makeText(getActivity(), "Posiotion OnClick in Adapter HAndle in Fragment" + position, Toast.LENGTH_SHORT).show();
listener.onItemSelected(position);
}
有关更多信息,check this。