Android recyclerView can';不要单击项目
我不能在我的recyclerview上单击任何项目,我认为我的代码很好 主要活动Android recyclerView can';不要单击项目,android,android-recyclerview,onclicklistener,Android,Android Recyclerview,Onclicklistener,我不能在我的recyclerview上单击任何项目,我认为我的代码很好 主要活动 public class Appetizer extends AppCompatActivity { public static final int CONNECTION_TIMEOUT=10000; public static final int READ_TIMEOUT=15000; private RecyclerView recyclerView; private Adap
public class Appetizer extends AppCompatActivity {
public static final int CONNECTION_TIMEOUT=10000;
public static final int READ_TIMEOUT=15000;
private RecyclerView recyclerView;
private AdapterFood mAdapter;
List<DataFood> data=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_appetizer);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
new AsyncFetch().execute();
}
private class AsyncFetch extends AsyncTask<String,String,String>{
ProgressDialog pdLoading = new ProgressDialog(Appetizer.this);
HttpURLConnection conn;
URL url = null;
@Override
protected void onPreExecute(){
super.onPreExecute();
pdLoading.setMessage("\tLoading...");
pdLoading.setCancelable(false);
pdLoading.show();
}
@Override
protected String doInBackground(String... params) {
try{
url = new URL("http://kelompokdua.hol.es/private_html/shAppetizer.php");
} catch (MalformedURLException e) {
e.printStackTrace();
return e.toString();
}
try {
conn = (HttpURLConnection)url.openConnection();
conn.setReadTimeout(READ_TIMEOUT);
conn.setConnectTimeout(CONNECTION_TIMEOUT);
conn.setRequestMethod("GET");
conn.setDoOutput(true);
} catch (IOException e1) {
e1.printStackTrace();
return e1.toString();
}
try {
int response_code = conn.getResponseCode();
// Check if successful connection made
if (response_code == HttpURLConnection.HTTP_OK) {
// Read data sent from server
InputStream input = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
// Pass data to onPostExecute method
return (result.toString());
} else {
return ("unsuccessful");
}
} catch (IOException e) {
e.printStackTrace();
return e.toString();
} finally {
conn.disconnect();
}
}
@Override
protected void onPostExecute(String result) {
//this method will be running on UI thread
pdLoading.dismiss();
List<DataFood> data=new ArrayList<>();
pdLoading.dismiss();
try {
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONArray innerArray = jArray.getJSONArray(i);
for (int j = 0; j < innerArray.length(); j++){
JSONObject json_data = innerArray.getJSONObject(j);
// JSONArray foodArray= json_data.getJSONArray("");
// for (int j=0;j<foodArray.length();j++){
DataFood foodData = new DataFood();
//JSONObject gambar = json_data.getJSONObject("gambar");
foodData.foodImage= json_data.getString("gambar");
foodData.foodName= json_data.getString("nama");
foodData.foodId= json_data.getInt("id");
foodData.price= json_data.getInt("harga");
data.add(foodData);
}}
// }
recyclerView = (RecyclerView)findViewById(R.id.lvaptzr);
LinearLayoutManager layoutManager=new LinearLayoutManager(Appetizer.this);
recyclerView.setLayoutManager(layoutManager);
//recyclerView.setLayoutManager(new LinearLayoutManager(Appetizer.this));
mAdapter = new AdapterFood(Appetizer.this,data);
recyclerView.setAdapter(mAdapter);
} catch (JSONException e) {
Toast.makeText(Appetizer.this,e.toString(),Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}}
}
public-class开胃菜扩展活动{
公共静态最终int连接\u超时=10000;
公共静态最终整型读取超时=15000;
私人回收站;
私人调适食品公司;
列表数据=新的ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局、活动和开胃菜);
LinearLayoutManager llm=新的LinearLayoutManager(本);
llm.设置方向(线性布局管理器.垂直);
新建AsyncFetch().execute();
}
私有类AsyncFetch扩展了AsyncTask{
ProgressDialog pdLoading=新的ProgressDialog(开胃菜,这个);
httpurl连接连接;
URL=null;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pdLoading.setMessage(“\t加载…”);
pdLoading.setCancelable(假);
pdLoading.show();
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
试一试{
url=新url(“http://kelompokdua.hol.es/private_html/shAppetizer.php");
}捕获(格式错误){
e、 printStackTrace();
返回e.toString();
}
试一试{
conn=(HttpURLConnection)url.openConnection();
conn.setReadTimeout(读取超时);
连接设置连接超时(连接超时);
conn.setRequestMethod(“GET”);
连接设置输出(真);
}捕获(IOE1异常){
e1.printStackTrace();
返回e1.toString();
}
试一试{
int response_code=conn.getResponseCode();
//检查连接是否成功
if(response\u code==HttpURLConnection.HTTP\u OK){
//读取从服务器发送的数据
InputStream input=conn.getInputStream();
BufferedReader reader=新的BufferedReader(新的InputStreamReader(输入));
StringBuilder结果=新建StringBuilder();
弦线;
而((line=reader.readLine())!=null){
结果。追加(行);
}
//将数据传递给onPostExecute方法
返回(result.toString());
}否则{
返回(“未成功”);
}
}捕获(IOE异常){
e、 printStackTrace();
返回e.toString();
}最后{
连接断开();
}
}
@凌驾
受保护的void onPostExecute(字符串结果){
//此方法将在UI线程上运行
pdLoading.disclose();
列表数据=新的ArrayList();
pdLoading.disclose();
试一试{
JSONArray jArray=新JSONArray(结果);
for(int i=0;isetOnClickListener应该位于RecyclerView的onBindViewHolder方法内部
@Override
public void onBindViewHolder(RecyclerViewHolder holder, final int position) {
myholder.textFoodname.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DataFood clickedDataItem = data.get(position);
Intent intent = new Intent(context, Order.class);
intent.putExtra("nama", data.get(position).foodName);
intent.putExtra("harga", data.get(position).price);
intent.putExtra("gambar", data.get(position).foodImage);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
Toast.makeText(v.getContext(),"clicked" + clickedDataItem.foodName, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(v.getContext(),"ga",Toast.LENGTH_SHORT).show();
}
});
}
试试下面一个
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
final Myholder myholder = (Myholder) holder;
DataFood current = data.get(position);
myholder.textFoodname.setText(current.foodName);
myholder.textPrice.setText("Rp. " + current.price);
myholder.textId.setText(String.valueOf(current.foodId));
Glide.with(context).load(current.foodImage).asBitmap()
.into(myholder.ivFood);
myholder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DataFood clickedDataItem = data.get(position);
Intent intent = new Intent(context, Order.class);
intent.putExtra("nama", data.get(position).foodName);
intent.putExtra("harga", data.get(position).price);
intent.putExtra("gambar", data.get(position).foodImage);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
Toast.makeText(v.getContext(), "clicked" + clickedDataItem.foodName, Toast.LENGTH_LONG).show();
}
});
}
并在Myholder类中更新此内容
class Myholder extends RecyclerView.ViewHolder {
TextView textFoodname, textPrice, textId;
ImageView ivFood;
CardView cardView;
public Myholder(View view) {
super(view);
textFoodname = (TextView) view.findViewById(R.id.textFoodname);
ivFood = (ImageView) view.findViewById(R.id.ivFood);
textId = (TextView) view.findViewById(R.id.textid);
textPrice = (TextView) view.findViewById(R.id.textPrice);
cardView = (CardView) itemView.findViewById(R.id.cv);
}
}
您已设置2个可单击项,因此无法单击该项
Just remove one clickable from layout which is not using it, keep only the one which used in layout identifer:
android:clickable="true"
注意:在您的情况下,请删除所有
如果您使用recyclerView,您应该尝试“回调接口”
AdapterFood.class
public interface OnItemClickLitener {
void onItemClick(View view, int position);
}
private OnItemClickLitener mOnItemClickLitener;
public void setOnItemClickListener(OnItemClickLitener mOnItemClickLitener) {
this.mOnItemClickLitener = mOnItemClickLitener;
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mOnItemClickLitener. onItemClick();
}
}
Myholder
class Myholder extends RecyclerView.ViewHolder {
TextView textFoodname, textPrice, textId;
ImageView ivFood;
CardView cardView;
public Myholder(View view) {
super(view);
textFoodname = (TextView) view.findViewById(R.id.textFoodname);
ivFood = (ImageView) view.findViewById(R.id.ivFood);
textId = (TextView) view.findViewById(R.id.textid);
textPrice = (TextView) view.findViewById(R.id.textPrice);
cardView = (CardView) itemView.findViewById(R.id.cv);
}
}
mAdapter.setOnItemClickListener(new CourseListAdp.OnItemClickLitener() {
@Override
public void onItemClick(View view, int position) {
//your work
int pos = position;
if (pos != RecyclerView.NO_POSITION){
**********
**********
}
}
}
开胃菜
class Myholder extends RecyclerView.ViewHolder {
TextView textFoodname, textPrice, textId;
ImageView ivFood;
CardView cardView;
public Myholder(View view) {
super(view);
textFoodname = (TextView) view.findViewById(R.id.textFoodname);
ivFood = (ImageView) view.findViewById(R.id.ivFood);
textId = (TextView) view.findViewById(R.id.textid);
textPrice = (TextView) view.findViewById(R.id.textPrice);
cardView = (CardView) itemView.findViewById(R.id.cv);
}
}
mAdapter.setOnItemClickListener(new CourseListAdp.OnItemClickLitener() {
@Override
public void onItemClick(View view, int position) {
//your work
int pos = position;
if (pos != RecyclerView.NO_POSITION){
**********
**********
}
}
}
希望能帮助您更改您的布局文件(从布局中删除可单击项):
Change Youyr Layout File(remove clickable from yourlayout):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:background="#CCC"
>
<android.support.v7.widget.CardView
android:id="@+id/cv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_gravity="center"
android:id="@+id/ivFood"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:scaleType="fitXY"
app:srcCompat="@mipmap/ic_launcher" />
<TextView
android:id="@+id/textid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toEndOf="@id/ivFood"
android:layout_toRightOf="@id/ivFood"
android:clickable="true"
android:text="id"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#666" />
<TextView
android:id="@+id/textFoodname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toLeftOf="@+id/textPrice"
android:layout_toRightOf="@id/ivFood"
android:text="food name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="price"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/colorAccent" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Change Your Adapter :
public class AdapterFood extends RecyclerView.Adapter<AdapterFood.CustomViewHolder> {
private Context context;
private LayoutInflater inflater;
List<DataFood> data = Collections.emptyList();
public AdapterFood(Context context, List<DataFood> data) {
this.context = context;
inflater = LayoutInflater.from(context);
this.data = data;
}
@Override
public AdapterFood.CustomViewHolder onCreateViewHolder(ViewGroup parent, int i) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
CustomViewHolder viewHolder = new CustomViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
DataFood current = data.get(position);
holder.textFoodname.setText(current.foodName);
holder.textPrice.setText("Rp. " + current.price);
holder.textId.setText(String.valueOf(current.foodId));
// Log.d("IMAGE_URL", "http://kelompokdua.hol.es/pic/" + current.foodImage);
Picasso.with(context).load("http://kelompokdua.hol.es/pic/" + current.foodImage).into(myholder.ivFood);
Glide.with(context).load(current.foodImage).asBitmap()
.placeholder(R.mipmap.ic_launcher).dontAnimate()
.error(R.mipmap.minus)
.into(myholder.ivFood);
}
@Override
public int getItemCount() {
return data.size();
}
public class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView textFoodname, textPrice, textId;
ImageView ivFood;
public CustomViewHolder(View itemView) {
super(itemView);
textFoodname = (TextView) itemView.findViewById(R.id.textFoodname);
ivFood = (ImageView) itemView.findViewById(R.id.ivFood);
textId = (TextView) itemView.findViewById(R.id.textid);
textPrice = (TextView) itemView.findViewById(R.id.textPrice);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Toast.makeText(context, "ga" + getAdapterPosition(), Toast.LENGTH_SHORT).show();
int pos = getAdapterPosition();
if (pos != RecyclerView.NO_POSITION) {
DataFood clickedDataItem = data.get(pos);
Intent intent = new Intent(context, Order.class);
intent.putExtra("nama", data.get(pos).foodName);
intent.putExtra("harga", data.get(pos).price);
intent.putExtra("gambar", data.get(pos).foodImage);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
Toast.makeText(v.getContext(), "clicked" + clickedDataItem.foodName, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(v.getContext(), "ga", Toast.LENGTH_SHORT).show();
}
}
}
更改适配器:
公共类AdapterFood扩展了RecyclerView.Adapter{
私人语境;
私人充气机;
List data=Collections.emptyList();
公共适配器食品(上下文、列表数据){
this.context=上下文;
充气器=充气器。从(上下文);
这个数据=数据;
}
@凌驾
public AdapterFood.CustomViewHolder onCreateViewHolder(视图组父级,int i){
View=LayoutFlater.from(parent.getContext()).flate(R.layout.list_项,parent,false);
CustomViewHolder viewHolder=新CustomViewHolder(视图);
返回视图持有者;
}
@凌驾
公共无效onBindViewHolder(CustomViewHolder,int位置){
DataFood current=data.get(位置);
holder.textFoodname.setText(当前的.foodName);
holder.textPrice.setText(“卢比”+当前价格);
holder.textId.setText(String.valueOf(current.foodId));
//Log.d(“图像URL”http://kelompokdua.hol.es/pic/“+当前食品价格);
毕加索.with(context).load(“http://kelompokdua.hol.es/pic/“+current.foodImage).插入(myholder.ivFood);
Glide.with(context.load(current.foodImage.asBitmap())
.placeholder(R.mipmap.ic_launcher).dontAnimate()
.错误(R.mipmap.减号)
.into(myholder.ivFood);
}
@凌驾
public int getItemCount(){
返回data.size();
}
公共类CustomViewHolder扩展了RecyclerView.ViewHolder实现了View.OnClickListener{
TextView textFoodname、textPrice、textId;
食物的图像视图;
公共CustomViewHolder(查看项目视图){
超级(项目视图);
textFoodname=(TextView)itemView.findViewById(R.id.textFoodname);
ivFood=(ImageView)itemView.findViewById(R.id.ivFood);
textId=(TextView)itemView.findViewById(R.id.textId);
textPrice=(TextView)itemView.findViewById(R.id.textPrice);
setOnClickListener(这个);
}
@凌驾
公共void onClick(视图v){
Toast.makeText(上下文,“ga”+getAdapterPosition(),Toast.LENGTH_SHORT).show();
Change Youyr Layout File(remove clickable from yourlayout):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:background="#CCC"
>
<android.support.v7.widget.CardView
android:id="@+id/cv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_gravity="center"
android:id="@+id/ivFood"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:scaleType="fitXY"
app:srcCompat="@mipmap/ic_launcher" />
<TextView
android:id="@+id/textid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toEndOf="@id/ivFood"
android:layout_toRightOf="@id/ivFood"
android:clickable="true"
android:text="id"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#666" />
<TextView
android:id="@+id/textFoodname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toLeftOf="@+id/textPrice"
android:layout_toRightOf="@id/ivFood"
android:text="food name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="price"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/colorAccent" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Change Your Adapter :
public class AdapterFood extends RecyclerView.Adapter<AdapterFood.CustomViewHolder> {
private Context context;
private LayoutInflater inflater;
List<DataFood> data = Collections.emptyList();
public AdapterFood(Context context, List<DataFood> data) {
this.context = context;
inflater = LayoutInflater.from(context);
this.data = data;
}
@Override
public AdapterFood.CustomViewHolder onCreateViewHolder(ViewGroup parent, int i) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
CustomViewHolder viewHolder = new CustomViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
DataFood current = data.get(position);
holder.textFoodname.setText(current.foodName);
holder.textPrice.setText("Rp. " + current.price);
holder.textId.setText(String.valueOf(current.foodId));
// Log.d("IMAGE_URL", "http://kelompokdua.hol.es/pic/" + current.foodImage);
Picasso.with(context).load("http://kelompokdua.hol.es/pic/" + current.foodImage).into(myholder.ivFood);
Glide.with(context).load(current.foodImage).asBitmap()
.placeholder(R.mipmap.ic_launcher).dontAnimate()
.error(R.mipmap.minus)
.into(myholder.ivFood);
}
@Override
public int getItemCount() {
return data.size();
}
public class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView textFoodname, textPrice, textId;
ImageView ivFood;
public CustomViewHolder(View itemView) {
super(itemView);
textFoodname = (TextView) itemView.findViewById(R.id.textFoodname);
ivFood = (ImageView) itemView.findViewById(R.id.ivFood);
textId = (TextView) itemView.findViewById(R.id.textid);
textPrice = (TextView) itemView.findViewById(R.id.textPrice);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Toast.makeText(context, "ga" + getAdapterPosition(), Toast.LENGTH_SHORT).show();
int pos = getAdapterPosition();
if (pos != RecyclerView.NO_POSITION) {
DataFood clickedDataItem = data.get(pos);
Intent intent = new Intent(context, Order.class);
intent.putExtra("nama", data.get(pos).foodName);
intent.putExtra("harga", data.get(pos).price);
intent.putExtra("gambar", data.get(pos).foodImage);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
Toast.makeText(v.getContext(), "clicked" + clickedDataItem.foodName, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(v.getContext(), "ga", Toast.LENGTH_SHORT).show();
}
}
}