如何添加clicklistener为点击时,我想他们每个人的每个项目

我这里有一个数组列表,如何添加clicklistener为点击时,我想他们每个人的每个项目

,他们会做一个特定功能或目的的活动。

当前:每个项目包含ImageButton和列表中的Textview。

import android.os.Bundle; 

import android.support.annotation.Nullable;

import android.support.design.widget.BottomSheetDialogFragment;

import android.support.v7.widget.LinearLayoutManager;

import android.support.v7.widget.RecyclerView;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import java.util.ArrayList;

import java.util.List;

public class MyBottomSheetDialogFragment extends BottomSheetDialogFragment {

String mString;

private RecyclerView mRecyclerView;

private VerticalListAdapter mVerticalListAdapter;

static MyBottomSheetDialogFragment newInstance(String string) {

MyBottomSheetDialogFragment f = new MyBottomSheetDialogFragment();

Bundle args = new Bundle();

args.putString("string", string);

f.setArguments(args);

return f;

}

@Override

public void onCreate(@Nullable Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

mString = getArguments().getString("string");

}

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

View v = inflater.inflate(R.layout.bottom_sheet_modal_list, container, false);

mRecyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);

return v;

}

@Override

public void onActivityCreated(Bundle savedInstanceState) {

super.onActivityCreated(savedInstanceState);

List<Model> list = new ArrayList<Model>();

list.add(new Model(R.drawable.a1, "Call"));

list.add(new Model(R.drawable.a2, "Chat"));

list.add(new Model(R.drawable.a3, "Android"));

list.add(new Model(R.drawable.a4, "Location"));

list.add(new Model(R.drawable.a5, "Call"));

list.add(new Model(R.drawable.a6, "Chat"));

list.add(new Model(R.drawable.a7, "Android"));

mVerticalListAdapter = new VerticalListAdapter(getContext(), list);

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);

mRecyclerView.setLayoutManager(linearLayoutManager);

mRecyclerView.setAdapter(mVerticalListAdapter);

}

我可以提供额外的XML,即相对连接到该代码项目的Java代码,请留下澄清评论。

import android.content.Context; 

import android.support.v7.widget.RecyclerView;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ImageButton;

import android.widget.TextView;

import java.util.List;

public class VerticalListAdapter extends RecyclerView.Adapter<VerticalListAdapter.MyViewHolder>{

private Context mContext;

private List<Model> mModelList;

public VerticalListAdapter(Context context, List<Model> list) {

this.mContext = context;

this.mModelList = list;

}

@Override

public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View itemView = LayoutInflater.from(parent.getContext())

.inflate(R.layout.modal_list_item, parent, false);

return new MyViewHolder(itemView);

}

@Override

public void onBindViewHolder(MyViewHolder holder, int position) {

holder.txtView.setText(mModelList.get(position).getName());

holder.imgView.setImageDrawable(mContext.getResources().getDrawable(mModelList.get(position).getImgId()));

}

@Override

public int getItemCount() {

return mModelList.size();

}

public class MyViewHolder extends RecyclerView.ViewHolder {

public TextView txtView;

public ImageButton imgView;

public MyViewHolder(View view) {

super(view);

txtView = (TextView) view.findViewById(R.id.txtView);

imgView = (ImageButton) view.findViewById(R.id.imgView);

}

}

}

回答:

你需要做的是落实OnClickListener您Holder类,并加上id modal_list_item.xml你的LinearLayout这样你就可以点击整个布局,不仅自己的观点(如果我理解正确的话)并最终设置onClickListener。

当你这样做时,你需要实现onClick方法。灵感请参阅下面的代码:

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

public TextView txtView;

public ImageButton imgView;

private LinearLayout linearLayout;

public MyViewHolder(View view) {

super(view);

txtView = (TextView) view.findViewById(R.id.txtView);

imgView = (ImageButton) view.findViewById(R.id.imgView);

linearLayout = (LinearLayout) view.findViewById(R.id.linear_l);

linearLayout.setOnClickListener(this);

}

@Override

public void onClick(View v) {

Log.i("RecyclerView Item", String.valueOf(getLayoutPosition()));

mModelList.remove(getLayoutPosition());

notifyDataSetChanged();

}

}

以上是 如何添加clicklistener为点击时,我想他们每个人的每个项目 的全部内容, 来源链接: utcz.com/qa/261410.html

回到顶部