我在Adapter RecyclerView中单击按钮时出现问题。点击按钮总是更多点击以获得响应。但我想点击一下,然后得到回应..
这是我的按钮。
<Button
android:id="@+id/btn_action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_add_white_24dp"
android:theme="@style/Button.Blue" />
这是我的适配器
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> implements View.OnClickListener {
.............
private OnNewsItemClickListener OnNewsItemClickListener;
public void setOnNewsItemClickListener(OnNewsItemClickListener onNewsItemClickListener) {
this.OnNewsItemClickListener = onNewsItemClickListener;
}
public interface OnNewsItemClickListener {
public void onActionClick(View v, int position);
}
public NewsAdapter(Activity activity, ArrayList<HashMap<String, String>> News,
ArrayList<HashMap<String, String[]>> newsArticleImages) {
this.activity = activity;
dataNews = News;
}
@Override
public void onClick(View v) {
final int viewId = v.getId();
if (viewId == R.id.btn_action) {
if (OnNewsItemClickListener != null) {
OnNewsItemClickListener.onActionClick(v, (Integer) v.getTag());
}
}
}
public class ViewHolder extends RecyclerView.ViewHolder {
@Bind(R.id.btn_action)
Button btn_action;
public ViewHolder(View vi) {
super(vi);
ButterKnife.bind(this, vi);
}
}
@Override
public NewsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.pin_item_news_recyclerview, parent, false);
ViewHolder holder = new ViewHolder(v);
holder.btn_action.setOnClickListener(this);
return holder;
}
public void onBindViewHolder(final ViewHolder holder, int position) {
}
.............
}
这个我的Activity调用ClickListener
public class BusinessActivity extends AppCompatActivity implements OnRefreshListener, NewsAdapter.OnNewsItemClickListener {
@Bind(R.id.grid_view) RecyclerView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.business_home);
ButterKnife.bind(this);
adapter = new NewsAdapter(BusinessActivity.this, NewsList,
NewsArticleImagesList);
adapter.setOnNewsItemClickListener(this);
list.setHasFixedSize(true);
list.setAdapter(adapter);
}
@Override
public void onActionClick(View v, int position) {
HashMap<String, String> news = new HashMap<String, String>();
news= adapter.dataNews.get(position);
String btn_des = news.get(Variabel.KEY_BTN_DES);
if(btn_des.equals("Add News")) {
Bundle b = new Bundle();
Intent i;
i = new Intent(BusinessActivity.this, AddNewsActivity.class);
b.putString(Variabel.KEY_BUSINESS_NAME, business_name);
b.putString(Variabel.KEY_INFO_FULL, info_full);
b.putString(Variabel.KEY_SUB_INFO_FULL, sub_info_full);
b.putString(Variabel.KEY_ID_BUSINESS, _idBusiness);
i.putExtras(b);
startActivity(i);
}
}
}
那么如何解决呢?
答案 0 :(得分:0)
如果它响应点击事件,我猜它正常工作。 这可能只是需要时间。尝试单击一次并等待一段时间。 如果它正在响应那么它的工作正常。这可能是性能问题。 发布您的代码,以便我们可以更好地了解实际发生的情况。
尝试使用以下适配器,并根据需要进行任何更改。 您的“ViewHolder”类应该实现View.OnClickListener而不是适配器本身。
//package
import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {
private ArrayList<HashMap<String, String>> dataNews;
private Context context;
public NewsAdapter(Activity activity, ArrayList<HashMap<String, String>> News,
ArrayList<HashMap<String, String[]>> newsArticleImages) {
this.context = activity;
dataNews = News;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v;
ViewHolder vhItem;
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.pin_item_news_recyclerview, parent, false);
vhItem = new ViewHolder(v, viewType, context);
return vhItem;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// holder.btn_action.setText("Action");
}
@Override
public int getItemCount() {
return dataNews.size();
}
@Override
public int getItemViewType(int position) {
return 0;
}
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
int Holderid;
private OnNewsItemClickListener OnNewsItemClickListener;
Button btn_action;;
Context contxt;
public ViewHolder(View itemView, int ViewType, Context c) {
super(itemView);
contxt = c;
itemView.setClickable(true);
itemView.setOnClickListener(this);
btn_action = (Button) itemView.findViewById(R.id.btnDeleteGoalRecyclerItemActGoalList);
btn_action.setOnClickListener(this);
}
@Override
public void onClick(View v) {
try {
OnNewsItemClickListener = (OnNewsItemClickListener) contxt;
} catch (ClassCastException e) {
throw new ClassCastException(contxt.toString()
+ " must implement OnNewsItemClickListener");
}
final int viewId = v.getId();
if (viewId == R.id.btn_action) {
if (OnNewsItemClickListener != null) {
OnNewsItemClickListener.onActionClick(v, (Integer) v.getTag());
}
}
}
}
public interface OnNewsItemClickListener {
public void onActionClick(View v, int position);
}
}