我有两个活动,分别是MainActivity和AnotherActivity,每个活动都有自己的recyclerview。我想要实现的是,如果我处于MainActivity中,然后单击一个recyclerview单元格,那么我应该转到AnotherActivity,反之亦然。我遇到的问题是,我不知道如何知道当前正在运行的活动,因此我可以在以下情况下对其进行操作:如果正在运行的活动是MainActivity,则应重定向到AnotherActivity。
MainActivity.java
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
String[] list = {"COLLECTIONS", "NOW PLAYING", "ADD PLAYLIST", "LOVE SONGS"};
public int activityNum = 0;
public final static String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView= (RecyclerView) findViewById(R.id.my_recycler_view);
RecyclerAdapter adapter=new RecyclerAdapter("main", list, list, MainActivity.this);
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
Log.i(TAG, TAG);
}
}
AnotherActivity.java
public class AnotherActvity extends AppCompatActivity {
RecyclerView recyclerView;
String[] list = {"Iridescent", "Bohemian Rhapsody", "November Rain", "I Love You"};
public int activityNum = 1;
public final static String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another);
recyclerView= (RecyclerView) findViewById(R.id.my_recycler_view);
RecyclerAdapter adapter=new RecyclerAdapter("another", list, list, AnotherActvity.this);
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(AnotherActvity.this));
Log.i(TAG, "AnotherActvity");
}
}
RecyclerAdapter.java
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerViewHolder> {
LayoutInflater inflater;
String page;
String[] menu_list;
String[] song_title;
int returnVar = 0;
public RecyclerAdapter(String page, String[] menu_list, String[] menu_icons, Context context) {
inflater= LayoutInflater.from(context);
this.menu_list = menu_list;
this.page = page;
}
public RecyclerAdapter(String page, String[] song_title, String[] song_artist, String[] song_duration, Context context) {
inflater=LayoutInflater.from(context);
this.song_title = song_title;
}
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = null;
RecyclerViewHolder viewHolder = null;
if(page == "main") {
view = inflater.inflate(R.layout.main_list, parent, false);
viewHolder = new RecyclerViewHolder(view);
}else if(page == "another"){
view = inflater.inflate(R.layout.another_list, parent, false);
viewHolder = new RecyclerViewHolder(view);
}
return viewHolder;
}
@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
if(page == "main") {
holder.item.setText(menu_list[position]);
holder.imageView.setTag(holder);
}else if(page == "another") {
holder.item.setText(menu_list[position]);
holder.imageView.setTag(holder);
}
}
public void setLength(int returnVar) {
this.returnVar = returnVar;
}
public int getLength() {
return this.returnVar;
}
@Override
public int getItemCount() {
if(page == "main") {
setLength(menu_list.length);
}else if(page == "another") {
setLength(menu_list.length);
}
return getLength();
}
}
RecyclerViewHolder.java
public class RecyclerViewHolder extends RecyclerView.ViewHolder {
TextView item;
ImageView imageView;
boolean lala = false;
public RecyclerViewHolder(final View itemView) {
super(itemView);
item = (TextView) itemView.findViewById(R.id.list_title);
imageView = (ImageView) itemView.findViewById(R.id.list_avatar);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
}
最佳答案
就像在onCreateViewHolder
方法中根据page
String扩展布局一样,因此只需向String类型的RecyclerViewHolder
添加一个参数并传递page
即可:
public RecyclerViewHolder(final View itemView,final String page) {
super(itemView);
...
@Override
public void onClick(View v) {
if(page.equals("main")){
// start MainActivity Activity here
}else{
// start AnotherActvity Activity here
}
}
}
在
page
中创建对象时,将RecyclerViewHolder
传递给onCreateViewHolder
构造函数:viewHolder = new RecyclerViewHolder(view,page);
注意:
使用
String.equals
或equalsIgnoreCase
代替==
来比较字符串值,当前==
和RecyclerViewHolder
中的onBindViewHolder
用于比较字符串