@Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { List<Event> events = Event.listAll(Event.class); List<ViewModel> items = new ArrayList<>(); for (Event event : events) { items.add( new ViewModel( event.eventName, "http://matrixthefest.org/app_posters/" + event.eventPoster)); } // Inflate the layout for this fragment RecyclerView recyclerView = new RecyclerView(getActivity()); RecyclerViewAdapter adapter; adapter = new RecyclerViewAdapter(items); // recyclerView.setAdapter(new YourRecyclerAdapter(getActivity())); adapter.setOnItemClickListener( new RecyclerViewAdapter.OnItemClickListener() { @Override public void onItemClick(View view, ViewModel viewModel) { DetailActivity.navigate( (AppCompatActivity) getActivity(), view.findViewById(R.id.image), viewModel); } }); recyclerView.setAdapter(adapter); recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); return recyclerView; }
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.recycler_activity); myRecyclerView = (RecyclerView) findViewById(R.id.myrecyclerview); myRecyclerView.setHasFixedSize(true); myRecyclerViewAdapter = new RecyclerViewAdapter(this); myRecyclerViewAdapter.setOnItemClickListener(this); myRecyclerView.setAdapter(myRecyclerViewAdapter); mLayoutManager = new GridLayoutManager(this, 2); myRecyclerView.setLayoutManager(mLayoutManager); nameField = (EditText) findViewById(R.id.namefield); nameField.setText(location_name); btnAdd = (Button) findViewById(R.id.addbutton); btnAdd.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { String newName = nameField.getText().toString(); if (!newName.equals("")) { if (myRecyclerViewAdapter.getItemCount() > 1) { myRecyclerViewAdapter.add(1, newName); nameField.setText(""); } else { myRecyclerViewAdapter.add(0, newName); nameField.setText(""); } } } }); }
@Nullable @Override public View onCreateView( LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragment1, container, false); recyclerView = findViewById(R.id.list); headerView = findViewById(R.id.header); footerView = findViewById(R.id.footer); list = getData(15); adapter = new RecyclerViewAdapter(); adapter.setData(list); recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); recyclerView.setAdapter(adapter); headerView.setOnRefreshListener(this); footerView.setOnLoadListener(this); return view; }
private void initView() { mToolbar = (Toolbar) this.findViewById(R.id.tool_bar); mDrawerLayout = (DrawerLayout) this.findViewById(R.id.drawer_layout); mNavigationView = (NavigationView) this.findViewById(R.id.navigation_view); // 初始化ToolBar setSupportActionBar(mToolbar); ActionBar actionBar = getSupportActionBar(); getSupportActionBar().setHomeButtonEnabled(true); // 设置返回键可用 // actionBar.setHomeAsUpIndicator(android.R.drawable.ic_dialog_alert); actionBar.setDisplayHomeAsUpEnabled(true); // 开启应用默认打开DrawerLayout // mDrawerLayout.openDrawer(mNavigationView); // mNavigationView.setNavigationItemSelectedListener(naviListener); ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle( this, mDrawerLayout, mToolbar, R.string.app_name, R.string.app_name); mDrawerToggle.syncState(); mDrawerLayout.setDrawerListener(mDrawerToggle); mRecyclerView = (MyRecView) findViewById(R.id.recycler_view); mRecyclerView.setLayoutManager(new LinearLayoutManager(mRecyclerView.getContext())); /* mRecyclerView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { //不是item的onTouch item onclick的时候不会执行到这 Log.d("ZYS", "onTouch"); // if(canclick) // return super.onTouch(v,event); return !canclick; } });*/ RecyclerViewAdapter mAdapter = new RecyclerViewAdapter(this); mAdapter.setOnItemClickLitener( new RecyclerViewAdapter.OnItemClickLitener() { CardView v; // 可以吧canclick相关判断转移到onitemtouch中,当返回true这些click事件不会被触发因为click等都基于onTouchEvent, // 而ontouch先于onTouchEvent,如果ontouch返回true则不执行onTouchEvent @Override public void onItemClick(View view, int position) { Log.d("ZYS", "onItemClick"); if (canclick) { canclick = false; v = (CardView) view; Log.d("ZYS", "Y:" + v.getTop() + " X:" + v.getLeft()); int[] pos = new int[2]; v.getLocationInWindow(pos); Log.d("ZYS", "getLocationInWindow:" + pos[0] + "," + pos[1]); // v.setCardBackgroundColor(MainActivity.this.getResources().getColor(R.color.colorAccent)); // Toast.makeText(MainActivity.this, position + " click " + view, // Toast.LENGTH_SHORT).show(); // 开启Fragment事务 FragmentTransaction transaction = fragmentManager.beginTransaction(); if (of2 == null) { // 如果MessageFragment为空,则创建一个并添加到界面上 of2 = new oFragment2(); of2.setttop(v.getTop(), "Thing " + position); transaction.add(R.id.content, of2); } else { // 如果MessageFragment不为空,则直接将它显示出来 of2.setttop(v.getTop(), "Thing " + position); transaction.show(of2); } transaction.commit(); verticalRun(); selectedFragment = of2; } } @Override public boolean onItemLongClick(View view, int position) { if (canclick) { // canclick=false; v = (CardView) view; v.setCardBackgroundColor( MainActivity.this.getResources().getColor(R.color.colorPrimary)); // Toast.makeText(MainActivity.this, position + " long click", // Toast.LENGTH_SHORT).show(); } return true; }; }); mAdapter.setOnItemTouchLitener( new RecyclerViewAdapter.OnItemTouchLitener() { @Override public boolean onItemTouch(View view, MotionEvent event) { int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: Log.d("ZYS", "2:onTouch action:ACTION_DOWN"); // view.getParent().requestDisallowInterceptTouchEvent(true); return false; case MotionEvent.ACTION_MOVE: Log.d("ZYS", "2:onTouch action:ACTION_MOVE"); break; case MotionEvent.ACTION_UP: Log.d("ZYS", "2:onTouch action:ACTION_UP"); break; case MotionEvent.ACTION_CANCEL: Log.d("ZYS", "2:onTouch action:ACTION_CANCEL"); break; } return false; } }); mRecyclerView.setAdapter(mAdapter); }