码迷,mamicode.com
首页 > 其他好文 > 详细

recyclerView使用

时间:2020-04-24 17:23:56      阅读:69      评论:0      收藏:0      [点我收藏+]

标签:containe   callback   device   rom   static   back   cas   zab   dataset   

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使用

标签:containe   callback   device   rom   static   back   cas   zab   dataset   

原文地址:https://www.cnblogs.com/Ocean123123/p/12768801.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!