gpt4 book ai didi

android - 使用来自两个 Activity 的不同回收者 View 在 Activity 之间切换

转载 作者:行者123 更新时间:2023-11-30 01:36:09 25 4
gpt4 key购买 nike

我有两个 Activity ,分别是 MainActivity 和 AnotherActivity,每个 Activity 都有自己的回收 View 。我想要实现的是,如果我在 MainActivity 中并单击 recyclerview 单元格,那么我应该转到 AnotherActivity,反之亦然。我遇到的问题是我不知道如何判断当前正在运行的 Activity ,以便我可以在 if 条件下操作它,其中如果正在运行的 Activity 是 MainActivity,那么我应该重定向到 AnotherActivity。

主 Activity .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);
}
}

另一个 Activity .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");
}
}

回收适配器.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中多加一个参数,传入页面:

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
}

}
}

并在onCreateViewHolder中创建对象时将page传递给RecyclerViewHolder构造函数:

viewHolder = new RecyclerViewHolder(view,page);

注意:使用 String.equalsequalsIgnoreCase 而不是 == 来比较当前的字符串值 == 用于 RecyclerViewHolderonBindViewHolder 用于比较字符串

关于android - 使用来自两个 Activity 的不同回收者 View 在 Activity 之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35092073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com