gpt4 book ai didi

java - RecyclerView 列表和下一个 Activity Android 的 Intent

转载 作者:搜寻专家 更新时间:2023-11-01 07:54:29 25 4
gpt4 key购买 nike

我使用 RecyclerView 创建了一个列表,我希望如果用户触摸该列表中的某个项目,它会将用户重定向到另一个 activity。注意我在 tab 中有 list。如何做到这一点?到目前为止,这是我尝试过的方法。

  1. 类列表适配器

公共(public)类 ListAdapter 扩展 RecyclerView.Adapter 实现 View.OnClickListener{

List<ListName> mItems;
public ListAdapter(FragmentActivity c) {
super();
mItems = new ArrayList<ListName>();

ListName nature = new ListName();
nature.setName("Jose Mourinho");
nature.setDes("Chelsea Football Club");
mItems.add(nature);

nature = new ListName();
nature.setName("Louis van Gaal");
nature.setDes("Manchester United");
mItems.add(nature);

nature = new ListName();
nature.setName("Arsene Wenger");
nature.setDes("Arsenal");
mItems.add(nature);

nature = new ListName();
nature.setName("Manuel Pellegrini");
nature.setDes("Manchester City");
mItems.add(nature);

nature = new ListName();
nature.setName("Brendan Rodgers");
nature.setDes("Liverpool CF");
mItems.add(nature);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.recyler_view_list, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
ListName nature = mItems.get(i);
viewHolder.tvName.setText(nature.getName());
viewHolder.tvDes.setText(nature.getDes());
}
@Override
public int getItemCount() {

return mItems.size();
}

@Override
public void onClick(View v) {

}

class ViewHolder extends RecyclerView.ViewHolder{

public TextView tvName;
public TextView tvDes;
public ViewHolder(View itemView) {
super(itemView);
tvName = (TextView)itemView.findViewById(R.id.txtTitle);
tvDes = (TextView)itemView.findViewById(R.id.txtSubTitle);
}
}

}
  1. 类 ListName 。该类用于设置和获取名称和描述

      public class ListName {
    private String mName;
    private String mDes;

    public String getName() {
    return mName;
    }
    public void setName(String name) {
    this.mName = name;
    }
    public String getDes() {
    return mDes;
    }
    public void setDes(String des) {
    this.mDes = des;
    }

    }
  2. Tab1 类。这是 Tab1 的 fragment 类。

     public class Tab1 extends Fragment {

    RecyclerView mRecyclerView;
    RecyclerView.LayoutManager mLayoutManager;
    RecyclerView.Adapter mAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.tab_1, container, false);
    final FragmentActivity c = getActivity();
    final RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
    LinearLayoutManager layoutManager = new LinearLayoutManager(c);
    recyclerView.setLayoutManager(layoutManager);

    new Thread(new Runnable() {
    @Override
    public void run() {
    final ListAdapter adapter = new ListAdapter(c);
    c.runOnUiThread(new Runnable() {
    @Override
    public void run() {
    recyclerView.setAdapter(adapter);
    }
    });
    }
    }).start();

    return view;
    }
    }
  3. 类 ViewPagerAdapter 。此类是选项卡的适配器

     public class ViewPagerAdapter extends FragmentStatePagerAdapter {

    CharSequence Titles[];
    int NumbOfTabs;


    public ViewPagerAdapter(FragmentManager fm, CharSequence mTitles[], int mNumbOfTabsumb) {
    super(fm);

    this.Titles = mTitles;
    this.NumbOfTabs = mNumbOfTabsumb;

    }

    @Override
    public Fragment getItem(int position) {

    if (position == 0) // if the position is 0 we are returning the First tab
    {
    Tab1 tab1 = new Tab1();
    return tab1;
    } else // As we are having 2 tabs if the position is now 0 it must be 1 so we are returning second tab
    {
    Tab2 tab2 = new Tab2();
    return tab2;
    }

    }

    @Override
    public CharSequence getPageTitle(int position) {
    return Titles[position];
    }

    //此方法返回标签条的标签数

    @Override
    public int getCount() {
    return NumbOfTabs;
    }
    }

最佳答案

在您的 Viewholder 中的 itemView 上放置一个 setOnClickListener 并编写代码以转到另一个 Activity ( Intent )

class ViewHolder extends RecyclerView.ViewHolder
{

public TextView tvName;
public TextView tvDes;

public ViewHolder(View itemView)
{
super(itemView);
tvName = (TextView)itemView.findViewById(R.id.txtTitle);
tvDes = (TextView)itemView.findViewById(R.id.txtSubTitle);

itemView..setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(getActivity().getApplicationContext(), ActivityName.class);
startActivity(intent); //or startActivityForResult(REQUEST, intent);
}
});

}
}

关于java - RecyclerView 列表和下一个 Activity Android 的 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30058106/

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