Click Listener上的RecyclerView不起作用 [英] RecyclerView on Click Listener Not working

查看:46
本文介绍了Click Listener上的RecyclerView不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我想单击回收站视图时,它应该打开另一个正在获取的活动数据,但是问题是当我单击回收站视图时,它什么也没显示. 我尝试了RecyclerView.OnItemTouchListener.但还没有成功. 您能找出问题出在哪里吗,我要去哪里

When i want to click recycler view then it should open another Activity Data is fetching but the problem is when i click on recycleview it displays nothing. I tried RecyclerView.OnItemTouchListener. but no Success yet. Can You please figure it out whats the issue,where i am going wrong

 public class RestaurantAdapter extends RecyclerView.Adapter<RestaurantAdapter.MyViewHolder> {
 List<AllRestaurant> restaurantList;
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.listitem, parent, false);

        return new MyViewHolder(itemView);
    }
    public class MyViewHolder extends RecyclerView.ViewHolder {
        //public TextView long1, medium, small;
        TextView t1,t2,t3,t4,t5;
        ImageView logo;

        public MyViewHolder(View view) {
            super(view);

            t1=(TextView)view.findViewById(R.id.t1);
            t2=(TextView)view.findViewById(R.id.t2);
            t3=(TextView)view.findViewById(R.id.t3);
            t4=(TextView)view.findViewById(R.id.t4);
            t5=(TextView)view.findViewById(R.id.t5);
            logo=(ImageView) view.findViewById(R.id.image);
         //   final AllRestaurant items = getItem(position);
            //long1.setText(items.getRestaurent_id());
            // final NotifyUsers notifyUsers=new NotifyUsers(context);
            // username.setText(items.getResponse_by());


            // little.setText(items.g);

        }
    }
    public RestaurantAdapter(List<AllRestaurant> restaurantList) {
        this.restaurantList = restaurantList;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        AllRestaurant restaurant = restaurantList.get(position);

        RestaurentAddress restaurentAddress=restaurant.getRestaurentAddress();
        RestaurentInfo restaurentInfo=restaurant.getRestaurentInfo();
        RestaurentSetting restaurentSetting=restaurant.getRestaurentSetting();
        User user=restaurant.getUser();
        Zone zone=restaurant.getZone();


        holder. t1.setText(restaurentInfo.getRestaurent_name());
        holder.t2.setText(restaurentAddress.getRestaurent_address1());
        holder.   t3.setText("min_pur:"+restaurentSetting.getMinimum_purcase());
        holder. t4.setText("Del In Min:"+restaurentSetting.getEstimate_delivery_time());
        String imagename=restaurentInfo.getRestaurent_logo();

        Bitmap bitmap=getImage(imagename);
        holder.logo.setImageBitmap(bitmap);
holder.itemView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        getItemId(position);
    }
});
    /*    long1.setText(restaurentInfo.getRestaurent_name());
        little.setText(restaurentAddress.getCity_name());*/
    }

    @Override
    public int getItemCount() {
        return restaurantList.size();
    }
  public Bitmap getImage(String name){
    Bitmap bitmap=null;
    String url="http://ibookstay.com/oops/web/img/uploads/res_log/"+name;
    OkhttpClient clientOkHttp=new OkhttpClient();
    try {
        byte image[]=new byte[0];
      //   image=clientOkHttp.execute(url).get();
        image = clientOkHttp.execute(url).get();
        if (image != null && image.length > 0){
            bitmap  = BitmapFactory.decodeByteArray(image, 0, image.length);
           // imageView.setImageBitmap(bitmap);
           // tv.setText("Total btytes download: "+ image.length);
        }
    }
    catch (InterruptedException e)
    {
        e.printStackTrace();
    }
    catch (ExecutionException e)
    {
        e.printStackTrace();
    }
    return  bitmap;
}
}

这是我的片段文件:

public class RestaurantFragment extends Fragment {
    RecyclerView recyclerView;
    LinkedList<AllRestaurant> allRestaurants;
    RestaurantAdapter allRestaurantAdapter;
    MethodModel methodModel;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.restaurantlist, container, false);
        methodModel=new MethodModel(getActivity());
        allRestaurants=methodModel.getAllRestaurant();
        allRestaurantAdapter=new RestaurantAdapter(allRestaurants);
        recyclerView=(RecyclerView)view.findViewById(R.id.recycler_view);

        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL));

// set the adapter
        recyclerView.setAdapter(allRestaurantAdapter);
        return view;
    }
}

布局文件:

<?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:orientation="vertical"
    android:background="#d3d3d3"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="desktop.oopsfood.Navigation"
    tools:showIn="@layout/app_bar_navigation">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="1">

        <EditText

            android:layout_width="wrap_content"
            android:layout_height="55dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:ems="10"
            android:hint="Search For Location"
            android:id="@+id/locationget"
            android:background="@drawable/rounded_border_edittext"
            android:gravity="center"
            android:layout_weight="0.99">

        </EditText>
        <requestFocus />

            android:shadowRadius="2.0"
            android:layout_weight=".3" />




    </LinearLayout>
<!--    <ListView
        android:layout_marginTop="5dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/list"
        android:dividerHeight="5dp"></ListView>-->
    <FrameLayout android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:id="@+id/container"

        />

</LinearLayout>

推荐答案

Using Interface you can solve your problem:

 public interface OnItemClickListener {
        public void onItemClick(View view, int position,  List<AllRestaurant> AllRestaurant);
    }

这样的适配器类中的实现接口.

Implement interface in your adapter class like this.

 public class MyViewHolder extends RecyclerView.ViewHolder  implements View.OnClickListener {
        //public TextView long1, medium, small;
        TextView t1,t2,t3,t4,t5;
        ImageView logo;

        public MyViewHolder(View view) {
            super(view);

            t1=(TextView)view.findViewById(R.id.t1);
            t2=(TextView)view.findViewById(R.id.t2);
            t3=(TextView)view.findViewById(R.id.t3);
            t4=(TextView)view.findViewById(R.id.t4);
            t5=(TextView)view.findViewById(R.id.t5);
            logo=(ImageView) view.findViewById(R.id.image);
            //   final AllRestaurant items = getItem(position);
            //long1.setText(items.getRestaurent_id());
            // final NotifyUsers notifyUsers=new NotifyUsers(context);
            // username.setText(items.getResponse_by());


            // little.setText(items.g);

        }

        @Override
        public void onClick(View v) {
            if (mItemClickListener != null) {
                mItemClickListener.onItemClick(v, getPosition(), getData());
            }
        }
    }

然后这样打电话:

mRecyclerView.setAdapter(adapter);
 adapter.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(View view, int position, List<UserDetails> userDetailsList) {
                Toast.makeText(getActivity(), "index --> " + position, Toast.LENGTH_SHORT).show();

            }

        });

这篇关于Click Listener上的RecyclerView不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆