recyclerView使用

Posted ocean123123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了recyclerView使用相关的知识,希望对你有一定的参考价值。

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyHolder> {
    private List<Device> list;
    private OnItemClickListen onItemClickListen;
    public RecyclerViewAdapter(List<Device> list) {
        this.list = list;

    }
    public class MyHolder extends RecyclerView.ViewHolder {
        private TextView textView1;
        private TextView textView2;
        private TextView textView3;
        private View view;
        public MyHolder(@NonNull View itemView) {
            super(itemView);
            view = itemView;
            textView1=itemView.findViewById(R.id.text1);
            textView2=itemView.findViewById(R.id.text2);
            textView3=itemView.findViewById(R.id.text3);
        }
    }


    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.listitem, viewGroup, false);
        final MyHolder myHolder = new MyHolder(view);
        myHolder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int adapterPosition = myHolder.getAdapterPosition();
                Device device = list.get(adapterPosition);
                onItemClickListen.itemClick(device);
            }
        });
        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {
        Device device = list.get(i);
        myHolder.textView1.setText(device.getPower());
        myHolder.textView2.setText(device.getSpeed());
        myHolder.textView3.setText(device.getTemp());

    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    public interface OnItemClickListen {
        void itemClick(Device device);
    }
    public void setListener(OnItemClickListen onItemClickListen) {
        this.onItemClickListen = onItemClickListen;
    }
}
public class BlankFragment2 extends Fragment{
    private MyHandler myHandler;
    private MyBroadCastReceiver myBroadCastReceiver;
    private FragmentActivity activity;
    private ArrayList<Integer> arrayList;
    private ArrayList<Device> arrayList2;
    private RecyclerView recyclerView;
    private LinearLayoutManager linearLayoutManager;
    private RecyclerViewAdapter adapter;

    public BlankFragment2() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
       View view= inflater.inflate(R.layout.fragment_blank_fragment2, container, false);
        recyclerView = view.findViewById(R.id.fragment2recyclerView);
        linearLayoutManager = new LinearLayoutManager(activity);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
    adapter = new RecyclerViewAdapter(arrayList2);
                recyclerView.setAdapter(adapter);
      adapter.setListener(new RecyclerViewAdapter.OnItemClickListen() {
            @Override
            public void itemClick(Device device) {
                Toast.makeText(activity, "点击了项目", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(activity, Main4Activity.class);
                intent.putExtra("device",device);
                startActivity(intent);
            }
        });
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        arrayList = new ArrayList<>();
        arrayList2 = new ArrayList<>();
        activity = getActivity();
        myHandler = new MyHandler(this);
        myBroadCastReceiver = new MyBroadCastReceiver();
        IntentFilter intentFilter = new IntentFilter("devicedata");
        activity.registerReceiver(myBroadCastReceiver, intentFilter);


    }




    public class MyBroadCastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {

            if (intent.getAction().equals("devicedata")) {

                int[] value = intent.getIntArrayExtra("value");
                Message message = new Message();
                message.what = 1;
                message.obj = value;
                myHandler.sendMessage(message);
            }
        }
    }

    public static class MyHandler extends Handler {
        WeakReference<Fragment> weakReference;
        BlankFragment2 blankFragment;

        public MyHandler(Fragment fragment) {
            weakReference = new WeakReference<>(fragment);
            blankFragment = (BlankFragment2) weakReference.get();
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                System.out.println("搜到信息");
                int[] value = (int[]) msg.obj;
                if (blankFragment.arrayList.size() != 0) {
                    System.out.println("输出arraylist尺寸"+blankFragment.arrayList.size());
                    if (blankFragment.arrayList.contains(value[0])) {
                        System.out.println("相同");
                        System.out.println("输出arraylist2尺寸"+blankFragment.arrayList2.size());
                        int index = blankFragment.arrayList.indexOf(value[0]);
                        Device device = blankFragment.arrayList2.get(index);

                        device.setPower(value[1] + "");
                        device.setSpeed(value[2] + "");
                        device.setTemp(value[3] + "");
                        blankFragment.adapter.notifyDataSetChanged();

                    } else {
                        System.out.println("不相同");
                        blankFragment.arrayList.add(value[0]);
                       Device device = new Device();
                       device.setNum(value[0]);
                       device.setPower(value[1] + "");
                       device.setSpeed(value[2] + "");
                       device.setTemp(value[3] + "");
                        blankFragment.arrayList2.add(device);
//                        blankFragment.adapter = new RecyclerViewAdapter(blankFragment.arrayList2);
//                        blankFragment.recyclerView.setAdapter(blankFragment.adapter);
                            blankFragment.adapter.notifyDataSetChanged();
                    }


                } else {
                    blankFragment.arrayList.add(value[0]);
                    Device device = new Device();
                    device.setNum(value[0]);
                   device.setPower(value[1] + "");
                   device.setSpeed(value[2] + "");
                    device.setTemp(value[3] + "");
                    blankFragment.arrayList2.add(device);
//                    blankFragment.adapter = new RecyclerViewAdapter(blankFragment.arrayList2);
//                    blankFragment.recyclerView.setAdapter(blankFragment.adapter);
                    blankFragment.adapter.notifyDataSetChanged();
                }
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        activity.unregisterReceiver(myBroadCastReceiver);
        myHandler.removeCallbacksAndMessages(null);
    }
}
public class Device implements Serializable {
    private String temp;
    private String power;
    private String speed;
    private int num;

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public String getTemp() {
        return temp;
    }

    public void setTemp(String temp) {
        this.temp = temp;
    }

    public String getPower() {
        return power;
    }

    public void setPower(String power) {
        this.power = power;
    }

    public String getSpeed() {
        return speed;
    }

    public void setSpeed(String speed) {
        this.speed = speed;
    }
}

 

以上是关于recyclerView使用的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Recyclerview Item Click 上打开新片段?

错误:E/RecyclerView:未连接适配器;跳过片段上的布局

Recyclerview 滚动在嵌套滚动视图中的片段中不起作用

Android:RecyclerView 不显示片段中的列表项

从片段类中的对话框添加项目到recyclerview。

如何在recyclerview不同的项目点击上打开几个不同的片段?