当前位置: 首页 > 面试题库 >

将RecyclerView CardView单击的项目数据传递到活动

颛孙钱青
2023-03-14
问题内容

我有一个关于将点击的Cardview数据传递到活动的问题,这里是全文:

  1. 我有一个称为“详细信息”的活动,该活动的布局中包含2个TextViews,分别是Title和Description。

  2. 我设置了一个片段(tab_1),其中包含recyclerview代码和item数据,其中的每个item都包含:title&description。

我想要的是 :

当用户单击项目时,它将打开“详细信息活动”,并更改“详细信息”布局标题(带有单击的项目标题)以及用于描述的内容。

我已经以创建另一个活动为例,并有意启动它,并借助Stackoverflow加上“ addOnTouchlistener”,我找到了实现它的方法。

那么,如何使它活着呢?我已经尝试了许多方法获得Stackoverflow上的可用答案,但是所有这些方法均无效,或者与我的请求无关。

这是我的文件:

itemsdata.java:

public class itemsdata {
int CatPic;
String title;
String Descr;
int Exapnd;
int expand_no;

tab_1.java(片段)

public class tab_1 extends Fragment implements SearchView.OnQueryTextListener {

private RecyclerView mRecyclerView;
public RecyclingViewAdapter adapter;
private Activity context;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View layout = inflater.inflate(R.layout.tab_1, container, false);
    mRecyclerView = (RecyclerView)layout.findViewById(R.id.recycler_view);
    mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener
            (getContext(), new RecyclerItemClickListener.OnItemClickListener() {

                @Override
                public void onItemClick(View view, int position) {
                    Intent i = new Intent(view.getContext(), DetailsActivity.class);
                    view.getContext().startActivity(i);
                }
            }));
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    adapter = new RecyclingViewAdapter(getActivity(),Listed());
    mRecyclerView.setAdapter(adapter);
    return layout;

}


@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.main, menu);

    final MenuItem item = menu.findItem(R.id.action_search);
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
    searchView.setOnQueryTextListener(this);
}

@Override
public boolean onQueryTextChange(String query) {
    final List<itemsdata> filteredModelList = filter(Listed(), query);
    adapter.animateTo(filteredModelList);
    mRecyclerView.scrollToPosition(0);
    return true;
}

@Override
public boolean onQueryTextSubmit(String query) {
    return true;
}

private List<itemsdata> filter(List<itemsdata> models, String query) {
    query = query.toLowerCase();

    final List<itemsdata> filteredModelList = new ArrayList<>();
    for (itemsdata model : models) {
        final String text = model.title.toLowerCase();
        if (text.contains(query)) {
            filteredModelList.add(model);
        }
    }
    return filteredModelList;
}

public List<itemsdata> Listed()
{
    //Titles Strings
    String sys_title1 = getString(R.string.system_item_title_1);
    String sys_title2 = getString(R.string.system_item_title_2);
    String sys_title3 = getString(R.string.system_item_title_3);

    //Description Strings
    String sys_descr1 = getString(R.string.system_item_desc_1);
    String sys_descr2 = getString(R.string.system_item_desc_2);
    String sys_descr3 = getString(R.string.system_item_desc_3);

    //Adding New Cards
    List<itemsdata> data = new ArrayList<>();

    //Categories Icons New Items ** Make It The Same
    int[] icons = {
            R.drawable.facebook_icon ,
            R.drawable.twitter_icon ,
            R.drawable.twitter_icon
    };

    //Expand Button New Items
    int[] expandbutton = {
            R.drawable.expanded ,
            R.drawable.expanded ,
            R.drawable.expanded
    };

    //UnExpand Button New Items
    int[] unexpandbutton = {
            R.drawable.ca_expand ,
            R.drawable.ca_expand ,
            R.drawable.ca_expand
    };

    //Titles New Items
    String[] titles = {
            sys_title1 ,
            sys_title2 ,
            sys_title3
    };

    //Description New Items
    String[] Description = {
            sys_descr1 ,
            sys_descr2 ,
            sys_descr3
    };


    for(int i = 0;i<titles.length && i < icons.length  && i < Description.length && i < unexpandbutton.length && i < expandbutton.length  ; i++)
    {
        itemsdata current = new itemsdata();
        current.CatPic = icons[i];
        current.title = titles[i];
        current.Descr = Description[i];
        current.expand_no = unexpandbutton[i];
        current.Exapnd = expandbutton[i];
        data.add(current);
    }
    return data;
}

}

详细活动:

public class DetailsActivity extends AppCompatActivity{

TextView title;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.details);
    title = (TextView)findViewById(R.id.details_title);
}

编辑:我已经做到了,我添加了一个打开片段的按钮,并在适配器中传递了数据,但是我希望通过tab_1.java而不是适配器来访问它,我的意思是我想单击该项目以打开片段,而不是按钮,这里是我的适配器代码的快照(我已将其添加到OnBindViewHolder中)

我已经设置了OnClick并实现了Vew.setOnClick ..etc,但是当我单击该项目时,什么也没有发生。

 @Override
public void onBindViewHolder(final MyRecycleViewHolder holder, int position) {

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(v.getContext(),DetailsActivity.class);
            v.getContext().startActivity(i);
        }
    });

    //Referencing Data
    final itemsdata currentobject = mdata.get(position);
    //Referencing Items
    holder.ProbTitle.setText(currentobject.title);
    holder.ProbDescr.setText(currentobject.Descr);
    holder.CategoryPic.setImageResource(currentobject.CatPic);
    holder.ExpandButton.setImageResource(currentobject.Exapnd);
    holder.ExpandNoButton.setImageResource(currentobject.expand_no);
        //What Happen When You Click Expand Button .
    holder.ExpandButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(v.getContext(), DetailsActivity.class);
            i.putExtra("TitleKey",holder.ProbTitle.getText().toString());
            v.getContext().startActivity(i);
            }
        }
    );

public static class MyRecycleViewHolder extends RecyclerView.ViewHolder
{



    SwipeLayout swipeLayout;
    //Defining Items .
    TextView ProbTitle;
    ImageButton ExpandButton;
    TextView ProbDescr;
    ImageButton ExpandNoButton;
    ImageView CategoryPic;
    /*
    TextView Card_Star;
    TextView Card_UnStar;
    */
    TextView Card_Share;

    //Referencing Resources
    public MyRecycleViewHolder(final View itemView) {
        super(itemView);
        ProbTitle = (TextView) itemView.findViewById(R.id.prob_title);
        CategoryPic = (ImageView) itemView.findViewById(R.id.cat_pic);
        ProbDescr = (TextView) itemView.findViewById(R.id.prob_descr);
        ExpandButton = (ImageButton) itemView.findViewById(R.id.expand_button);
        ExpandNoButton = (ImageButton) itemView.findViewById(R.id.expand_no_button);
        /*
        Card_Star = (TextView) itemView.findViewById(R.id.card_star);
        Card_UnStar = (TextView) itemView.findViewById(R.id.card_unstar);
        */
        Card_Share = (TextView) itemView.findViewById(R.id.card_share);
        swipeLayout = (SwipeLayout) itemView.findViewById(R.id.swipe);

    }

问题答案:

Interface在包含的适配器内部创建一个methods。在实现适配器时,这些方法将在您的活动中实现,您可以执行所需的任何操作。

    public class Adapter extends RecyclerView.Adapter<MyRecycleViewHolder> {

    public interface Callbacks {
        public void onButtonClicked(String titleKey);
    }

    private Callbacks mCallbacks;

    public Adapter() {

    }

    @Override
    public MyRecycleViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_details, null);
        return new MyRecycleViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final MyRecycleViewHolder holder, final int i) {

        holder.ExpandButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCallbacks != null) {
                    mCallbacks.onButtonClicked(holder.ProbTitle.getText().toString());
                }
            }
        });

    }


    @Override
    public int getItemCount() {
        return;
    }

    public void setCallbacks(Callbacks callbacks) {
        this.mCallbacks = callbacks;
    }
}


 类似资料: