我想在recyclerView中实现Onclick项目要打开新活动,我可以将数据从一个活动传递到另一个活动但是在代码中无法找到实现的位置点击onclicklistener如果有人可以自由地帮助我编辑并给予意图详细信息的.class
因为我第一次在firebase上从firebase获取数据,所以任何帮助都会受到赞赏。
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import com.dekhou.provissionstore.model.Movie;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.squareup.picasso.Picasso;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private FloatingActionButton fab;
ScaleAnimation shrinkAnim;
private RecyclerView mRecyclerView;
private StaggeredGridLayoutManager mLayoutManager;
private TextView tvNoMovies,tvNoMovies1;
private Context context;
//Getting reference to Firebase Database
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference mDatabaseReference = database.getReference();
private static final String userId = "01";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//Initializing our Recyclerview
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
tvNoMovies = (TextView) findViewById(R.id.tv_no_movies);
//tvNoMovies1 = (TextView) findViewById(R.id.addresstxt);
//scale animation to shrink floating actionbar
shrinkAnim = new ScaleAnimation(1.15f, 0f, 1.15f, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
if (mRecyclerView != null) {
//to enable optimization of recyclerview
mRecyclerView.setHasFixedSize(true);
}
//using staggered grid pattern in recyclerview
mLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(mLayoutManager);
//Say Hello to our new FirebaseUI android Element, i.e., FirebaseRecyclerAdapter
FirebaseRecyclerAdapter<Movie,MovieViewHolder> adapter = new FirebaseRecyclerAdapter<Movie, MovieViewHolder>(
Movie.class,
R.layout.movie_board_item,
MovieViewHolder.class,
//referencing the node where we want the database to store the data from our Object
mDatabaseReference.child("users").child(userId).child("shops").getRef()
) {
@Override
protected void populateViewHolder(MovieViewHolder viewHolder, final Movie model, final int position) {
if(tvNoMovies.getVisibility()== View.VISIBLE){
tvNoMovies.setVisibility(View.GONE);
}
viewHolder.tvMovieName.setText(model.getMovieName());
viewHolder.address.setText(model.getAddress());
viewHolder.phone.setText(model.getPhone());
viewHolder.lit.setText(model.getLit());
viewHolder.lon.setText(model.getLon());
Picasso.with(MainActivity.this).load(model.getMoviePoster()).into(viewHolder.ivMoviePoster);
// all your stuff
}
};
mRecyclerView.setAdapter(adapter);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_container, new AddMovieFragment())
.addToBackStack(null)
.commit();
//animation being used to make floating actionbar disappear
shrinkAnim.setDuration(400);
fab.setAnimation(shrinkAnim);
shrinkAnim.start();
shrinkAnim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
//changing floating actionbar visibility to gone on animation end
fab.setVisibility(View.GONE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
});
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.menu_map) {
Intent intent = new Intent(this, MapsActivity.class);
this.startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_slideshow) {
Intent cinemaIntent = new Intent(this, MapsActivity.class);
startActivity(cinemaIntent);
} else if (id == R.id.nav_manage) {
Intent cinemaIntent = new Intent(this, Get_Data.class);
startActivity(cinemaIntent);
} else if (id == R.id.nav_share) {
try {
//try to open page in facebook native app.
String uri = "fb://page/" + "293069792077"; //Cutsom URL
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);
}catch (ActivityNotFoundException ex){
//facebook native app isn't available, use browser.
String uri = "http://touch.facebook.com/pages/x/" + "293069792077"; //Normal URL
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(i);
}
} else if (id == R.id.nav_send) {
Intent cinemaIntent = new Intent(this, About_Us.class);
startActivity(cinemaIntent);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
//ViewHolder for our Firebase UI
public static class MovieViewHolder extends RecyclerView.ViewHolder{
TextView tvMovieName,address,phone,lit,lon;
ImageView ivMoviePoster;
public MovieViewHolder(View v) {
super(v);
tvMovieName = (TextView) v.findViewById(R.id.tv_name);
address = (TextView) v.findViewById(R.id.address1);
phone = (TextView) v.findViewById(R.id.tv_phone);
lit = (TextView) v.findViewById(R.id.geo);
lon = (TextView) v.findViewById(R.id.geo2);
ivMoviePoster = (ImageView) v.findViewById(R.id.iv_movie_poster);
}
}
}
答案 0 :(得分:1)
将onClickListener添加到populateViewHolder中ViewHolder的itemView并启动Activity。如下例所示。
@Override
protected void populateViewHolder(MovieViewHolder viewHolder, final Movie model, final int position) {
if(tvNoMovies.getVisibility()== View.VISIBLE){
tvNoMovies.setVisibility(View.GONE);
}
viewHolder.tvMovieName.setText(model.getMovieName());
viewHolder.address.setText(model.getAddress());
viewHolder.phone.setText(model.getPhone());
viewHolder.lit.setText(model.getLit());
viewHolder.lon.setText(model.getLon());
Picasso.with(MainActivity.this).load(model.getMoviePoster()).into(viewHolder.ivMoviePoster);
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, DetailActivity.class);
//intent.putExtra("model", model);
intent.putExtra("latitude", model.getLit());
intent.putExtra("longitude", model.getLon());
startActivity(intent);
}
});
// all your stuff
}
答案 1 :(得分:1)
尝试在此sh2
中添加onClickListener
populateViewHolder